2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2022-10-17 22:43:26 +05:00
|
|
|
import { sendMigrationProgressEvent } from "../common";
|
2022-10-17 22:36:53 +05:00
|
|
|
import { migrateCollection, migrateItem } from "../migrations";
|
2020-12-06 10:52:00 +05:00
|
|
|
|
|
|
|
|
class Migrator {
|
2022-10-26 10:52:27 +05:00
|
|
|
async migrate(db, collections, get, version, restore = false) {
|
2021-07-06 12:13:35 +05:00
|
|
|
for (let collection of collections) {
|
|
|
|
|
if (!collection.index || !collection.dbCollection) continue;
|
2022-10-17 22:36:53 +05:00
|
|
|
|
2022-10-17 22:43:26 +05:00
|
|
|
if (collection.dbCollection.collectionName)
|
|
|
|
|
sendMigrationProgressEvent(
|
|
|
|
|
db.eventManager,
|
|
|
|
|
collection.dbCollection.collectionName,
|
|
|
|
|
0,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
2022-10-17 22:36:53 +05:00
|
|
|
await migrateCollection(collection.dbCollection, version);
|
|
|
|
|
|
|
|
|
|
const index = (await collection.index()) || [];
|
|
|
|
|
for (var i = 0; i < index.length; ++i) {
|
|
|
|
|
let id = index[i];
|
2022-11-09 12:26:33 +05:00
|
|
|
let item = get(id, collection.dbCollection.collectionName);
|
2021-07-06 12:13:35 +05:00
|
|
|
if (!item) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-12-06 10:52:00 +05:00
|
|
|
|
2022-10-17 22:43:26 +05:00
|
|
|
if (collection.dbCollection.collectionName)
|
|
|
|
|
sendMigrationProgressEvent(
|
|
|
|
|
db.eventManager,
|
|
|
|
|
collection.dbCollection.collectionName,
|
|
|
|
|
index.length,
|
|
|
|
|
i + 1
|
|
|
|
|
);
|
|
|
|
|
|
2022-10-12 20:33:37 +05:00
|
|
|
// check if item is permanently deleted or just a soft delete
|
2021-07-06 12:13:35 +05:00
|
|
|
if (item.deleted && !item.type) {
|
|
|
|
|
await collection.dbCollection?._collection?.addItem(item);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-10-26 10:52:27 +05:00
|
|
|
|
2022-10-17 22:36:53 +05:00
|
|
|
const itemId = item.id;
|
2022-10-26 10:52:27 +05:00
|
|
|
const migrated = await migrateItem(
|
2022-10-12 10:39:47 +05:00
|
|
|
item,
|
|
|
|
|
version,
|
2022-10-17 22:36:53 +05:00
|
|
|
item.type || collection.type || collection.dbCollection.type,
|
2022-10-12 10:39:47 +05:00
|
|
|
db
|
|
|
|
|
);
|
2020-12-29 14:08:48 +05:00
|
|
|
|
2022-10-26 10:52:27 +05:00
|
|
|
if (migrated || restore) {
|
|
|
|
|
if (collection.dbCollection.merge) {
|
|
|
|
|
await collection.dbCollection.merge(item);
|
|
|
|
|
} else if (collection.dbCollection.add) {
|
|
|
|
|
await collection.dbCollection.add(item);
|
|
|
|
|
}
|
2022-10-17 22:36:53 +05:00
|
|
|
|
2022-10-26 10:52:27 +05:00
|
|
|
// if id changed after migration, we need to delete the old one.
|
|
|
|
|
if (item.id !== itemId) {
|
|
|
|
|
await collection.dbCollection?._collection?.deleteItem(itemId);
|
|
|
|
|
}
|
2022-10-17 22:36:53 +05:00
|
|
|
}
|
2021-07-06 12:13:35 +05:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-06 10:52:00 +05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default Migrator;
|