From fe88cfe106f01c27ab5553589b9cef1e7b821ce7 Mon Sep 17 00:00:00 2001 From: fullex <0xfullex@gmail.com> Date: Fri, 21 Nov 2025 23:46:51 +0800 Subject: [PATCH] feat: initialize database in app startup and enhance DbService - Added an init method to DbService for database initialization, ensuring it is called before migrations. - Updated the migrateDb and migrateSeed methods to check if the database is initialized, improving error handling. - Called dbService.init() in the app's whenReady event to ensure proper database setup during startup. --- src/main/data/db/DbService.ts | 34 ++++++++++++++++--- .../data/migration/v2/core/MigrationEngine.ts | 1 + src/main/index.ts | 1 + 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/data/db/DbService.ts b/src/main/data/db/DbService.ts index 7e0213cbb1..8a7edb6f33 100644 --- a/src/main/data/db/DbService.ts +++ b/src/main/data/db/DbService.ts @@ -45,7 +45,6 @@ class DbService { connection: { url: pathToFileURL(path.join(app.getPath('userData'), DB_NAME)).href }, casing: 'snake_case' }) - this.isInitialized = true logger.info('Database connection initialized', { dbPath: path.join(app.getPath('userData'), DB_NAME) }) @@ -68,6 +67,26 @@ class DbService { return DbService.instance } + /** + * Initialize the database + * @throws {Error} If database initialization fails + */ + public async init(): Promise { + if (this.isInitialized) { + logger.warn('Database already initialized, do not need initialize again!') + return + } + + try { + // Configure WAL mode on first database operation + await this.configureWAL() + this.isInitialized = true + } catch (error) { + logger.error('Database initialization failed', error as Error) + throw error + } + } + /** * Configure WAL mode for better concurrency performance * Called once during the first database operation @@ -93,10 +112,11 @@ class DbService { * @throws {Error} If migration fails */ public async migrateDb(): Promise { - try { - // Configure WAL mode on first database operation - await this.configureWAL() + if (!this.isInitialized) { + throw new Error('Database is not initialized, please call init() first!') + } + try { const migrationsFolder = this.getMigrationsFolder() await migrate(this.db, { migrationsFolder }) @@ -113,7 +133,7 @@ class DbService { */ public getDb(): DbType { if (!this.isInitialized) { - throw new Error('Database is not initialized') + throw new Error('Database is not initialized, please call init() first!') } return this.db } @@ -131,6 +151,10 @@ class DbService { * @throws {Error} If seed migration fails */ public async migrateSeed(seedName: keyof typeof Seeding): Promise { + if (!this.isInitialized) { + throw new Error('Database is not initialized, please call init() first!') + } + try { const Seed = Seeding[seedName] if (!Seed) { diff --git a/src/main/data/migration/v2/core/MigrationEngine.ts b/src/main/data/migration/v2/core/MigrationEngine.ts index 428c0633b1..1b004d38e7 100644 --- a/src/main/data/migration/v2/core/MigrationEngine.ts +++ b/src/main/data/migration/v2/core/MigrationEngine.ts @@ -59,6 +59,7 @@ export class MigrationEngine { /** * Check if migration is needed */ + //TODO 不能仅仅判断数据库,如果是全新安装,而不是升级上来的用户,其实并不需要迁移,但是按现在的逻辑,还是会进行迁移,这不正确 async needsMigration(): Promise { const db = dbService.getDb() const status = await db.select().from(appStateTable).where(eq(appStateTable.key, MIGRATION_V2_STATUS)).get() diff --git a/src/main/index.ts b/src/main/index.ts index dc6e927a82..efeef3f9d1 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -132,6 +132,7 @@ if (!app.requestSingleInstanceLock()) { // Some APIs can only be used after this event occurs. app.whenReady().then(async () => { // First of all, init & migrate the database + await dbService.init() await dbService.migrateDb() await dbService.migrateSeed('preference')