mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-07 05:39:05 +08:00
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.
This commit is contained in:
parent
e3bf63d7a0
commit
fe88cfe106
@ -45,7 +45,6 @@ class DbService {
|
|||||||
connection: { url: pathToFileURL(path.join(app.getPath('userData'), DB_NAME)).href },
|
connection: { url: pathToFileURL(path.join(app.getPath('userData'), DB_NAME)).href },
|
||||||
casing: 'snake_case'
|
casing: 'snake_case'
|
||||||
})
|
})
|
||||||
this.isInitialized = true
|
|
||||||
logger.info('Database connection initialized', {
|
logger.info('Database connection initialized', {
|
||||||
dbPath: path.join(app.getPath('userData'), DB_NAME)
|
dbPath: path.join(app.getPath('userData'), DB_NAME)
|
||||||
})
|
})
|
||||||
@ -68,6 +67,26 @@ class DbService {
|
|||||||
return DbService.instance
|
return DbService.instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the database
|
||||||
|
* @throws {Error} If database initialization fails
|
||||||
|
*/
|
||||||
|
public async init(): Promise<void> {
|
||||||
|
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
|
* Configure WAL mode for better concurrency performance
|
||||||
* Called once during the first database operation
|
* Called once during the first database operation
|
||||||
@ -93,10 +112,11 @@ class DbService {
|
|||||||
* @throws {Error} If migration fails
|
* @throws {Error} If migration fails
|
||||||
*/
|
*/
|
||||||
public async migrateDb(): Promise<void> {
|
public async migrateDb(): Promise<void> {
|
||||||
try {
|
if (!this.isInitialized) {
|
||||||
// Configure WAL mode on first database operation
|
throw new Error('Database is not initialized, please call init() first!')
|
||||||
await this.configureWAL()
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const migrationsFolder = this.getMigrationsFolder()
|
const migrationsFolder = this.getMigrationsFolder()
|
||||||
await migrate(this.db, { migrationsFolder })
|
await migrate(this.db, { migrationsFolder })
|
||||||
|
|
||||||
@ -113,7 +133,7 @@ class DbService {
|
|||||||
*/
|
*/
|
||||||
public getDb(): DbType {
|
public getDb(): DbType {
|
||||||
if (!this.isInitialized) {
|
if (!this.isInitialized) {
|
||||||
throw new Error('Database is not initialized')
|
throw new Error('Database is not initialized, please call init() first!')
|
||||||
}
|
}
|
||||||
return this.db
|
return this.db
|
||||||
}
|
}
|
||||||
@ -131,6 +151,10 @@ class DbService {
|
|||||||
* @throws {Error} If seed migration fails
|
* @throws {Error} If seed migration fails
|
||||||
*/
|
*/
|
||||||
public async migrateSeed(seedName: keyof typeof Seeding): Promise<void> {
|
public async migrateSeed(seedName: keyof typeof Seeding): Promise<void> {
|
||||||
|
if (!this.isInitialized) {
|
||||||
|
throw new Error('Database is not initialized, please call init() first!')
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const Seed = Seeding[seedName]
|
const Seed = Seeding[seedName]
|
||||||
if (!Seed) {
|
if (!Seed) {
|
||||||
|
|||||||
@ -59,6 +59,7 @@ export class MigrationEngine {
|
|||||||
/**
|
/**
|
||||||
* Check if migration is needed
|
* Check if migration is needed
|
||||||
*/
|
*/
|
||||||
|
//TODO 不能仅仅判断数据库,如果是全新安装,而不是升级上来的用户,其实并不需要迁移,但是按现在的逻辑,还是会进行迁移,这不正确
|
||||||
async needsMigration(): Promise<boolean> {
|
async needsMigration(): Promise<boolean> {
|
||||||
const db = dbService.getDb()
|
const db = dbService.getDb()
|
||||||
const status = await db.select().from(appStateTable).where(eq(appStateTable.key, MIGRATION_V2_STATUS)).get()
|
const status = await db.select().from(appStateTable).where(eq(appStateTable.key, MIGRATION_V2_STATUS)).get()
|
||||||
|
|||||||
@ -132,6 +132,7 @@ if (!app.requestSingleInstanceLock()) {
|
|||||||
// Some APIs can only be used after this event occurs.
|
// Some APIs can only be used after this event occurs.
|
||||||
app.whenReady().then(async () => {
|
app.whenReady().then(async () => {
|
||||||
// First of all, init & migrate the database
|
// First of all, init & migrate the database
|
||||||
|
await dbService.init()
|
||||||
await dbService.migrateDb()
|
await dbService.migrateDb()
|
||||||
await dbService.migrateSeed('preference')
|
await dbService.migrateSeed('preference')
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user