mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +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",
|
databaseCollectionInitiated: "db:collectionInitiated",
|
||||||
appRefreshRequested: "app:refreshRequested",
|
appRefreshRequested: "app:refreshRequested",
|
||||||
migrationProgress: "migration:progress",
|
migrationProgress: "migration:progress",
|
||||||
|
migrationStarted: "migration:start",
|
||||||
|
migrationFinished: "migration:finished",
|
||||||
noteRemoved: "note:removed",
|
noteRemoved: "note:removed",
|
||||||
tokenRefreshed: "token:refreshed",
|
tokenRefreshed: "token:refreshed",
|
||||||
userUnauthorized: "user:unauthorized",
|
userUnauthorized: "user:unauthorized",
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ import {
|
|||||||
isDeleted
|
isDeleted
|
||||||
} from "../types.js";
|
} from "../types.js";
|
||||||
import { logger } from "../logger.js";
|
import { logger } from "../logger.js";
|
||||||
|
import { EV, EVENTS } from "../common.js";
|
||||||
|
|
||||||
// type FilteredKeys<T, U> = {
|
// type FilteredKeys<T, U> = {
|
||||||
// [P in keyof T]: T[P] extends U ? P : never;
|
// [P in keyof T]: T[P] extends U ? P : never;
|
||||||
@@ -308,7 +309,14 @@ export async function initializeDatabase<Schema>(
|
|||||||
db,
|
db,
|
||||||
provider: migrationProvider
|
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();
|
const { error, results } = await migrator.migrateToLatest();
|
||||||
|
EV.publish(EVENTS.migrationFinished);
|
||||||
|
|
||||||
if (error)
|
if (error)
|
||||||
throw error instanceof Error ? error : new Error(JSON.stringify(error));
|
throw error instanceof Error ? error : new Error(JSON.stringify(error));
|
||||||
|
|||||||
Reference in New Issue
Block a user