mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 06:59:31 +01:00
core: send db name with migration events
This commit is contained in:
@@ -253,7 +253,8 @@ class Database {
|
|||||||
|
|
||||||
await initializeDatabase(
|
await initializeDatabase(
|
||||||
this.sql().withTables(),
|
this.sql().withTables(),
|
||||||
new NNMigrationProvider()
|
new NNMigrationProvider(),
|
||||||
|
"notesnook"
|
||||||
);
|
);
|
||||||
await this.onInit(this.sql() as unknown as Kysely<RawDatabaseSchema>);
|
await this.onInit(this.sql() as unknown as Kysely<RawDatabaseSchema>);
|
||||||
await this.initCollections();
|
await this.initCollections();
|
||||||
|
|||||||
@@ -302,7 +302,8 @@ async function setupDatabase<Schema>(
|
|||||||
|
|
||||||
export async function initializeDatabase<Schema>(
|
export async function initializeDatabase<Schema>(
|
||||||
db: Kysely<Schema>,
|
db: Kysely<Schema>,
|
||||||
migrationProvider: MigrationProvider
|
migrationProvider: MigrationProvider,
|
||||||
|
name: string
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const migrator = new Migrator({
|
const migrator = new Migrator({
|
||||||
@@ -314,9 +315,9 @@ export async function initializeDatabase<Schema>(
|
|||||||
.then((m) => m.some((m) => !m.executedAt));
|
.then((m) => m.some((m) => !m.executedAt));
|
||||||
if (!needsMigration) return db;
|
if (!needsMigration) return db;
|
||||||
|
|
||||||
EV.publish(EVENTS.migrationStarted);
|
EV.publish(EVENTS.migrationStarted, name);
|
||||||
const { error, results } = await migrator.migrateToLatest();
|
const { error, results } = await migrator.migrateToLatest();
|
||||||
EV.publish(EVENTS.migrationFinished);
|
EV.publish(EVENTS.migrationFinished, name);
|
||||||
|
|
||||||
if (error)
|
if (error)
|
||||||
throw error instanceof Error ? error : new Error(JSON.stringify(error));
|
throw error instanceof Error ? error : new Error(JSON.stringify(error));
|
||||||
@@ -364,7 +365,7 @@ export async function createDatabase<Schema>(
|
|||||||
dialect: options.dialect(name, async () => {
|
dialect: options.dialect(name, async () => {
|
||||||
await db.connection().execute(async (db) => {
|
await db.connection().execute(async (db) => {
|
||||||
await setupDatabase(db, options);
|
await setupDatabase(db, options);
|
||||||
await initializeDatabase(db, options.migrationProvider);
|
await initializeDatabase(db, options.migrationProvider, name);
|
||||||
if (options.onInit) await options.onInit(db);
|
if (options.onInit) await options.onInit(db);
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
@@ -373,7 +374,7 @@ export async function createDatabase<Schema>(
|
|||||||
if (!options.skipInitialization)
|
if (!options.skipInitialization)
|
||||||
await db.connection().execute(async (db) => {
|
await db.connection().execute(async (db) => {
|
||||||
await setupDatabase(db, options);
|
await setupDatabase(db, options);
|
||||||
await initializeDatabase(db, options.migrationProvider);
|
await initializeDatabase(db, options.migrationProvider, name);
|
||||||
if (options.onInit) await options.onInit(db);
|
if (options.onInit) await options.onInit(db);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user