mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 06:59:31 +01:00
core: add migration start/finished events
This commit is contained in:
committed by
Abdullah Atta
parent
7dc581b9f3
commit
a315473ea4
@@ -108,6 +108,8 @@ export const EVENTS = {
|
||||
databaseCollectionInitiated: "db:collectionInitiated",
|
||||
appRefreshRequested: "app:refreshRequested",
|
||||
migrationProgress: "migration:progress",
|
||||
migrationStarted: "migration:start",
|
||||
migrationFinished: "migration:finished",
|
||||
noteRemoved: "note:removed",
|
||||
tokenRefreshed: "token:refreshed",
|
||||
userUnauthorized: "user:unauthorized",
|
||||
|
||||
@@ -60,6 +60,7 @@ import {
|
||||
isDeleted
|
||||
} from "../types.js";
|
||||
import { logger } from "../logger.js";
|
||||
import { EV, EVENTS } from "../common.js";
|
||||
|
||||
// type FilteredKeys<T, U> = {
|
||||
// [P in keyof T]: T[P] extends U ? P : never;
|
||||
@@ -308,7 +309,14 @@ export async function initializeDatabase<Schema>(
|
||||
db,
|
||||
provider: migrationProvider
|
||||
});
|
||||
const needsMigration = await migrator
|
||||
.getMigrations()
|
||||
.then((m) => m.some((m) => !m.executedAt));
|
||||
if (!needsMigration) return db;
|
||||
|
||||
EV.publish(EVENTS.migrationStarted);
|
||||
const { error, results } = await migrator.migrateToLatest();
|
||||
EV.publish(EVENTS.migrationFinished);
|
||||
|
||||
if (error)
|
||||
throw error instanceof Error ? error : new Error(JSON.stringify(error));
|
||||
|
||||
Reference in New Issue
Block a user