mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
feat: use single version for db & backup migration
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const CURRENT_VERSION = 3;
|
||||
import { CURRENT_DATABASE_VERSION } from "../common";
|
||||
|
||||
class Migrations {
|
||||
/**
|
||||
*
|
||||
@@ -6,7 +7,7 @@ class Migrations {
|
||||
*/
|
||||
constructor(db) {
|
||||
this._db = db;
|
||||
this.dbVersion = CURRENT_VERSION;
|
||||
this.dbVersion = CURRENT_DATABASE_VERSION;
|
||||
}
|
||||
|
||||
async init() {
|
||||
@@ -14,13 +15,13 @@ class Migrations {
|
||||
}
|
||||
|
||||
get _shouldMigrate() {
|
||||
return this.dbVersion < CURRENT_VERSION;
|
||||
return this.dbVersion < CURRENT_DATABASE_VERSION;
|
||||
}
|
||||
|
||||
_migrationFunction(collectionId) {
|
||||
let migrationFunction = migrations[version][collectionId];
|
||||
if (!migrationFunction)
|
||||
migrationFunction = migrations[CURRENT_VERSION][collectionId];
|
||||
migrationFunction = migrations[CURRENT_DATABASE_VERSION][collectionId];
|
||||
return migrationFunction;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,3 +16,5 @@ export const CHECK_IDS = {
|
||||
notebookAdd: "notebook:add",
|
||||
backupEncrypt: "backup:encrypt",
|
||||
};
|
||||
|
||||
export const CURRENT_DATABASE_VERSION = 3;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import Hashes from "jshashes";
|
||||
import { CHECK_IDS, sendCheckUserStatusEvent } from "../common.js";
|
||||
import {
|
||||
CHECK_IDS,
|
||||
sendCheckUserStatusEvent,
|
||||
CURRENT_DATABASE_VERSION,
|
||||
} from "../common.js";
|
||||
import { migrations } from "../migrations.js";
|
||||
const md5 = new Hashes.MD5();
|
||||
|
||||
const invalidKeys = ["user", "t", "lastBackupTime"];
|
||||
const validTypes = ["mobile", "web", "node"];
|
||||
const CURRENT_BACKUP_VERSION = 3;
|
||||
export default class Backup {
|
||||
/**
|
||||
*
|
||||
@@ -46,7 +49,7 @@ export default class Backup {
|
||||
await this._db.context.write("lastBackupTime", Date.now());
|
||||
|
||||
return JSON.stringify({
|
||||
version: CURRENT_BACKUP_VERSION,
|
||||
version: CURRENT_DATABASE_VERSION,
|
||||
type,
|
||||
date: Date.now(),
|
||||
data,
|
||||
@@ -83,13 +86,13 @@ export default class Backup {
|
||||
|
||||
_migrateBackup(backup) {
|
||||
const { version = 0 } = backup;
|
||||
if (version > CURRENT_BACKUP_VERSION)
|
||||
if (version > CURRENT_DATABASE_VERSION)
|
||||
throw new Error(
|
||||
"This backup was made from a newer version of Notesnook. Cannot migrate."
|
||||
);
|
||||
|
||||
switch (version) {
|
||||
case CURRENT_BACKUP_VERSION:
|
||||
case CURRENT_DATABASE_VERSION:
|
||||
case 2: {
|
||||
return backup;
|
||||
}
|
||||
@@ -114,7 +117,7 @@ export default class Backup {
|
||||
|
||||
async _migrateData(backup) {
|
||||
const { data, version = 0 } = backup;
|
||||
if (version > CURRENT_BACKUP_VERSION)
|
||||
if (version > CURRENT_DATABASE_VERSION)
|
||||
throw new Error(
|
||||
"This backup was made from a newer version of Notesnook. Cannot migrate."
|
||||
);
|
||||
@@ -147,7 +150,7 @@ export default class Backup {
|
||||
let migrationFunction = migrations[version][collectionId];
|
||||
if (!migrationFunction)
|
||||
migrationFunction =
|
||||
migrations[CURRENT_BACKUP_VERSION][collectionId];
|
||||
migrations[CURRENT_DATABASE_VERSION][collectionId];
|
||||
await migrationFunction(this._db, item);
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user