2022-07-07 13:17:55 +05:00
|
|
|
import { CURRENT_DATABASE_VERSION, EVENTS } from "../common";
|
2020-12-06 10:52:00 +05:00
|
|
|
import Migrator from "../database/migrator";
|
2020-12-05 15:28:54 +05:00
|
|
|
|
2020-12-05 15:26:54 +05:00
|
|
|
class Migrations {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {import("./index").default} db
|
|
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
2020-12-06 10:52:00 +05:00
|
|
|
this._migrator = new Migrator();
|
2022-07-07 13:17:55 +05:00
|
|
|
this._isMigrating = false;
|
2020-12-05 15:26:54 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async init() {
|
2021-02-12 10:15:37 +05:00
|
|
|
this.dbVersion =
|
2021-09-26 11:47:13 +05:00
|
|
|
(await this._db.storage.read("v")) || CURRENT_DATABASE_VERSION;
|
|
|
|
|
this._db.storage.write("v", this.dbVersion);
|
2020-12-05 15:26:54 +05:00
|
|
|
}
|
|
|
|
|
|
2020-12-06 10:52:00 +05:00
|
|
|
async migrate() {
|
2022-07-07 13:17:55 +05:00
|
|
|
if (this.dbVersion >= CURRENT_DATABASE_VERSION || this._isMigrating) return;
|
|
|
|
|
|
|
|
|
|
this._isMigrating = true;
|
|
|
|
|
this._db.eventManager.publish(EVENTS.databaseMigrating, {
|
|
|
|
|
from: this.dbVersion,
|
|
|
|
|
to: CURRENT_DATABASE_VERSION,
|
|
|
|
|
});
|
2020-12-05 15:26:54 +05:00
|
|
|
|
2020-12-06 10:52:00 +05:00
|
|
|
await this._db.notes.init();
|
|
|
|
|
const content = await this._db.content.all();
|
2020-12-05 15:26:54 +05:00
|
|
|
|
|
|
|
|
const collections = [
|
2021-10-28 13:37:55 +05:00
|
|
|
{ index: this._db.attachments.all, dbCollection: this._db.attachments },
|
2020-12-06 10:52:00 +05:00
|
|
|
{
|
|
|
|
|
index: this._db.notebooks.raw,
|
|
|
|
|
dbCollection: this._db.notebooks,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
index: this._db.tags.raw,
|
|
|
|
|
dbCollection: this._db.tags,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
index: this._db.colors.raw,
|
|
|
|
|
dbCollection: this._db.colors,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
index: this._db.trash.raw,
|
|
|
|
|
dbCollection: this._db.trash,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
index: content,
|
|
|
|
|
dbCollection: this._db.content,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
index: [this._db.settings.raw],
|
|
|
|
|
dbCollection: this._db.settings,
|
2021-02-02 12:24:54 +05:00
|
|
|
type: "settings",
|
2020-12-06 10:52:00 +05:00
|
|
|
},
|
2021-07-06 12:13:55 +05:00
|
|
|
{
|
|
|
|
|
index: this._db.notes.raw,
|
|
|
|
|
dbCollection: this._db.notes,
|
|
|
|
|
},
|
2020-12-05 15:26:54 +05:00
|
|
|
];
|
2020-12-06 10:52:00 +05:00
|
|
|
await this._migrator.migrate(collections, (item) => item, this.dbVersion);
|
2021-09-26 11:47:13 +05:00
|
|
|
await this._db.storage.write("v", CURRENT_DATABASE_VERSION);
|
2020-12-05 15:26:54 +05:00
|
|
|
|
2022-07-15 19:23:01 +05:00
|
|
|
setTimeout(() => {
|
|
|
|
|
this._db.eventManager.publish(EVENTS.databaseMigrated);
|
|
|
|
|
this.dbVersion = CURRENT_DATABASE_VERSION;
|
|
|
|
|
}, 5000);
|
2020-12-05 15:26:54 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default Migrations;
|