feat: impl automatic database migrations (exp)

This commit is contained in:
thecodrr
2020-12-05 15:26:54 +05:00
parent 7a07ed0e51
commit fc7c1001e6
4 changed files with 159 additions and 97 deletions

View File

@@ -1,5 +1,6 @@
import Hashes from "jshashes";
import { CHECK_IDS, sendCheckUserStatusEvent } from "../common.js";
import { migrations } from "../migrations.js";
const md5 = new Hashes.MD5();
const invalidKeys = ["user", "t", "lastBackupTime"];
@@ -131,26 +132,22 @@ export default class Backup {
];
await Promise.all(
collections.map(async (collection) => {
const collectionIndex = data[collection];
if (!collectionIndex) return;
collections.map(async (collectionId) => {
let collection = data[collectionId];
if (!collection) return;
if (!Array.isArray(collectionIndex)) {
let migrationFunction = migrations[version][collection];
if (!migrationFunction)
migrationFunction = migrations[CURRENT_BACKUP_VERSION][collection];
await migrationFunction(this._db, collectionIndex);
return;
if (!Array.isArray(collection)) {
collection = [collectionId];
}
await Promise.all(
collectionIndex.map(async (id) => {
collection.map(async (id) => {
const item = data[id];
if (!item) return;
let migrationFunction = migrations[version][collection];
let migrationFunction = migrations[version][collectionId];
if (!migrationFunction)
migrationFunction =
migrations[CURRENT_BACKUP_VERSION][collection];
migrations[CURRENT_BACKUP_VERSION][collectionId];
await migrationFunction(this._db, item);
})
);
@@ -179,88 +176,3 @@ export default class Backup {
}
}
}
const migrations = {
handleDeleted: async function (db, collection, item) {
if (item.deleted) {
await db[collection]._collection.addItem(item);
return true;
}
return false;
},
0: {
notes: async function (db, item) {
if (await migrations.handleDeleted(db, "notes", item)) return;
const contentId = item.content.delta;
const notebook = item.notebook;
delete item.content;
delete item.notebook;
item.contentId = contentId;
item.remote = true;
if (notebook) item.notebooks = [notebook];
await db.notes.add(item);
},
delta: async function (db, item) {
if (await migrations.handleDeleted(db, "content", item)) return;
item.data = item.data.ops;
item.type = "delta";
await db.content.add(item);
},
trash: async function (db, item) {
if (await migrations.handleDeleted(db, "trash", item)) return;
item.itemType = item.type;
item.type = "trash";
if (item.itemType === "note") {
item.contentId = item.content.delta;
delete item.content;
}
await db.trash.add(item);
},
text: function () {},
},
2: {
notes: async function (db, item) {
if (await migrations.handleDeleted(db, "notes", item)) return;
// notebook -> notebooks
const notebook = item.notebook;
delete item.notebook;
item.remote = true;
if (notebook) item.notebooks = [notebook];
await db.notes.add({ ...item, remote: true });
},
},
3: {
notes: async function (db, item) {
if (await migrations.handleDeleted(db, "notes", item)) return;
await db.notes.add({ ...item, remote: true });
},
notebooks: async function (db, item) {
if (await migrations.handleDeleted(db, "notebooks", item)) return;
await db.notebooks.add(item);
},
tags: async function (db, item) {
if (await migrations.handleDeleted(db, "tags", item)) return;
await db.tags.merge(item);
},
colors: async function (db, item) {
if (await migrations.handleDeleted(db, "colors", item)) return;
await db.colors.merge(item);
},
trash: async function (db, item) {
if (await migrations.handleDeleted(db, "trash", item)) return;
await db.trash.add(item);
},
content: async function (db, item) {
if (await migrations.handleDeleted(db, "content", item)) return;
await db.content.add(item);
},
settings: async function (db, item) {
db.settings.merge(item);
},
},
};