core: add support for localOnly migrations

This commit is contained in:
Abdullah Atta
2023-09-13 12:40:30 +05:00
parent 8dd36f7adf
commit 5906b24da1
4 changed files with 23 additions and 8 deletions

View File

@@ -476,7 +476,13 @@ async function deserializeItem(decryptedItem, version, database) {
deserialized.synced = true;
if (!deserialized.alg && !deserialized.cipher) {
await migrateItem(deserialized, version, deserialized.type, database);
await migrateItem(
deserialized,
version,
deserialized.type,
database,
"sync"
);
}
return deserialized;
}

View File

@@ -259,7 +259,7 @@ export default class Backup {
// since items in trash can have their own set of migrations,
// we have to run the migration again to account for that.
if (item.type === "trash" && item.itemType)
await migrateItem(item, version, item.itemType, this._db);
await migrateItem(item, version, item.itemType, this._db, "backup");
const collectionKey = itemTypeToCollectionKey[item.itemType || item.type];
if (collectionKey) {

View File

@@ -78,7 +78,8 @@ class Migrator {
item,
version,
item.type || collection.type || collection.dbCollection.type,
db
db,
"local"
);
if (migrated) {

View File

@@ -136,16 +136,24 @@ const migrations = [
{
version: 5.8,
items: {
all: (item) => {
delete item.remote;
return true;
all: (item, _db, migrationType) => {
if (migrationType === "local") {
delete item.remote;
return true;
}
}
}
},
{ version: 5.9, items: {} }
];
export async function migrateItem(item, version, type, database) {
export async function migrateItem(
item,
version,
type,
database,
migrationType
) {
let migrationStartIndex = migrations.findIndex((m) => m.version === version);
if (migrationStartIndex <= -1) {
throw new Error(
@@ -164,7 +172,7 @@ export async function migrateItem(item, version, type, database) {
? migration.items[type] || migration.items.all
: null;
if (!itemMigrator) continue;
if (await itemMigrator(item, database)) count++;
if (await itemMigrator(item, database, migrationType)) count++;
}
return count > 0;