core: add migration start/finished events

This commit is contained in:
Abdullah Atta
2024-11-18 12:32:33 +05:00
committed by Abdullah Atta
parent 7dc581b9f3
commit a315473ea4
2 changed files with 10 additions and 0 deletions

View File

@@ -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",

View File

@@ -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));