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