feat: configure WAL mode for improved database performance

- Introduced a new method to configure Write-Ahead Logging (WAL) mode for better concurrency during database operations.
- Ensured WAL mode is set only once, with error handling to fall back to default settings if configuration fails.
- Updated the migrateDb method to call the new configuration method on the first database operation.
This commit is contained in:
fullex 2025-11-21 16:36:15 +08:00
parent 1e4239d189
commit 55727e2adf

View File

@ -1,4 +1,5 @@
import { loggerService } from '@logger'
import { sql } from 'drizzle-orm'
import { drizzle } from 'drizzle-orm/libsql'
import { migrate } from 'drizzle-orm/libsql/migrator'
import { app } from 'electron'
@ -36,6 +37,7 @@ class DbService {
private static instance: DbService
private db: DbType
private isInitialized = false
private walConfigured = false
private constructor() {
try {
@ -66,12 +68,37 @@ class DbService {
return DbService.instance
}
/**
* Configure WAL mode for better concurrency performance
* Called once during the first database operation
*/
private async configureWAL(): Promise<void> {
if (this.walConfigured) {
return
}
try {
await this.db.run(
sql`PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA foreign_keys = ON`
)
this.walConfigured = true
logger.info('WAL mode configured for database')
} catch (error) {
logger.warn('Failed to configure WAL mode, using default journal mode', error as Error)
// Don't throw error, allow database to continue with default mode
}
}
/**
* Run database migrations
* @throws {Error} If migration fails
*/
public async migrateDb(): Promise<void> {
try {
// Configure WAL mode on first database operation
await this.configureWAL()
const migrationsFolder = this.getMigrationsFolder()
await migrate(this.db, { migrationsFolder })