mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-24 10:40:07 +08:00
Some checks failed
Auto I18N Weekly / Auto I18N (push) Has been cancelled
* refactor: optimize DatabaseManager and fix libsql crash issues Major improvements: - Created DatabaseManager singleton to centralize database connection management - Auto-initialize database in constructor (no manual initialization needed) - Removed all manual initialize() and ensureInitialized() calls (47 occurrences) - Simplified initialization logic (removed retry loops that could cause crashes) - Removed unused close() and reinitialize() methods - Reduced code from ~270 lines to 172 lines (-36%) Key changes: 1. DatabaseManager.ts (new file): - Singleton pattern with auto-initialization - State management (INITIALIZING, INITIALIZED, FAILED) - Windows compatibility fixes (empty file detection, intMode: 'number') - Simplified waitForInitialization() logic 2. BaseService.ts: - Removed static initialize() and ensureInitialized() methods - Simplified database/rawClient getters to use DatabaseManager 3. Service classes (AgentService, SessionService, SessionMessageService): - Removed all initialize() methods - Removed all ensureInitialized() calls - Services now work out of the box 4. Main entry points (index.ts, server.ts): - Removed explicit database initialization calls - Database initializes automatically on first access Benefits: - Fixes Windows libsql crashes by removing dangerous retry logic - Simpler API - no need to remember to call initialize() - Better separation of concerns - Cleaner codebase with 36% less code 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: wait for database initialization on app startup Issue: "Database is still initializing" error on startup Root cause: Synchronous database getter was called before async initialization completed Solution: - Explicitly wait for database initialization in main index.ts - Import DatabaseManager and call getDatabase() to ensure initialization is complete - This guarantees database is ready before any service methods are called Changes: - src/main/index.ts: Added explicit database initialization wait before API server check 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * refactor: use static import for getDatabaseManager - Move import to top of file for better code organization - Remove unnecessary dynamic import 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * refactor: streamline database access in service classes - Replaced direct database access with asynchronous calls to getDatabase() in various service classes (AgentService, SessionService, SessionMessageService). - Updated the main index.ts to utilize runAsyncFunction for API server initialization, ensuring proper handling of asynchronous database access. - Improved code organization and readability by consolidating database access logic. This change enhances the reliability of database interactions across the application and ensures that services are correctly initialized before use. * refactor: remove redundant logging in ApiServer initialization - Removed the logging statement for 'AgentService ready' during server initialization. - This change streamlines the startup process by eliminating unnecessary log entries. This update contributes to cleaner logs and improved readability during server startup. * refactor: change getDatabase method to synchronous return type - Updated the getDatabase method in DatabaseManager to return a synchronous LibSQLDatabase instance instead of a Promise. - This change simplifies the database access pattern, aligning with the current initialization logic. This refactor enhances code clarity and reduces unnecessary asynchronous handling in the database access layer. * refactor: simplify sessionMessageRepository by removing transaction handling - Removed transaction handling parameters from message persistence methods in sessionMessageRepository. - Updated database access to use a direct call to getDatabase() instead of passing a transaction client. - Streamlined the upsertMessage and persistExchange methods for improved clarity and reduced complexity. This refactor enhances code readability and simplifies the database interaction logic. --------- Co-authored-by: Claude <noreply@anthropic.com>
157 lines
4.3 KiB
TypeScript
157 lines
4.3 KiB
TypeScript
import { type Client, createClient } from '@libsql/client'
|
|
import { loggerService } from '@logger'
|
|
import type { LibSQLDatabase } from 'drizzle-orm/libsql'
|
|
import { drizzle } from 'drizzle-orm/libsql'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
import { dbPath } from '../drizzle.config'
|
|
import { MigrationService } from './MigrationService'
|
|
import * as schema from './schema'
|
|
|
|
const logger = loggerService.withContext('DatabaseManager')
|
|
|
|
/**
|
|
* Database initialization state
|
|
*/
|
|
enum InitState {
|
|
INITIALIZING = 'initializing',
|
|
INITIALIZED = 'initialized',
|
|
FAILED = 'failed'
|
|
}
|
|
|
|
/**
|
|
* DatabaseManager - Singleton class for managing libsql database connections
|
|
*
|
|
* Responsibilities:
|
|
* - Single source of truth for database connection
|
|
* - Thread-safe initialization with state management
|
|
* - Automatic migration handling
|
|
* - Safe connection cleanup
|
|
* - Error recovery and retry logic
|
|
* - Windows platform compatibility fixes
|
|
*/
|
|
export class DatabaseManager {
|
|
private static instance: DatabaseManager | null = null
|
|
|
|
private client: Client | null = null
|
|
private db: LibSQLDatabase<typeof schema> | null = null
|
|
private state: InitState = InitState.INITIALIZING
|
|
|
|
/**
|
|
* Get the singleton instance (database initialization starts automatically)
|
|
*/
|
|
public static async getInstance(): Promise<DatabaseManager> {
|
|
if (DatabaseManager.instance) {
|
|
return DatabaseManager.instance
|
|
}
|
|
|
|
const instance = new DatabaseManager()
|
|
await instance.initialize()
|
|
DatabaseManager.instance = instance
|
|
|
|
return instance
|
|
}
|
|
|
|
/**
|
|
* Perform the actual initialization
|
|
*/
|
|
public async initialize(): Promise<void> {
|
|
if (this.state === InitState.INITIALIZED) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
logger.info(`Initializing database at: ${dbPath}`)
|
|
|
|
// Ensure database directory exists
|
|
const dbDir = path.dirname(dbPath)
|
|
if (!fs.existsSync(dbDir)) {
|
|
logger.info(`Creating database directory: ${dbDir}`)
|
|
fs.mkdirSync(dbDir, { recursive: true })
|
|
}
|
|
|
|
// Check if database file is corrupted (Windows specific check)
|
|
if (fs.existsSync(dbPath)) {
|
|
const stats = fs.statSync(dbPath)
|
|
if (stats.size === 0) {
|
|
logger.warn('Database file is empty, removing corrupted file')
|
|
fs.unlinkSync(dbPath)
|
|
}
|
|
}
|
|
|
|
// Create client with platform-specific options
|
|
this.client = createClient({
|
|
url: `file:${dbPath}`,
|
|
// intMode: 'number' helps avoid some Windows compatibility issues
|
|
intMode: 'number'
|
|
})
|
|
|
|
// Create drizzle instance
|
|
this.db = drizzle(this.client, { schema })
|
|
|
|
// Run migrations
|
|
const migrationService = new MigrationService(this.db, this.client)
|
|
await migrationService.runMigrations()
|
|
|
|
this.state = InitState.INITIALIZED
|
|
logger.info('Database initialized successfully')
|
|
} catch (error) {
|
|
const err = error as Error
|
|
logger.error('Database initialization failed:', {
|
|
error: err.message,
|
|
stack: err.stack
|
|
})
|
|
|
|
// Clean up failed initialization
|
|
this.cleanupFailedInit()
|
|
|
|
// Set failed state
|
|
this.state = InitState.FAILED
|
|
throw new Error(`Database initialization failed: ${err.message || 'Unknown error'}`)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clean up after failed initialization
|
|
*/
|
|
private cleanupFailedInit(): void {
|
|
if (this.client) {
|
|
try {
|
|
// On Windows, closing a partially initialized client can crash
|
|
// Wrap in try-catch and ignore errors during cleanup
|
|
this.client.close()
|
|
} catch (error) {
|
|
logger.warn('Failed to close client during cleanup:', error as Error)
|
|
}
|
|
}
|
|
this.client = null
|
|
this.db = null
|
|
}
|
|
|
|
/**
|
|
* Get the database instance
|
|
* Automatically waits for initialization to complete
|
|
* @throws Error if database initialization failed
|
|
*/
|
|
public getDatabase(): LibSQLDatabase<typeof schema> {
|
|
return this.db!
|
|
}
|
|
|
|
/**
|
|
* Get the raw client (for advanced operations)
|
|
* Automatically waits for initialization to complete
|
|
* @throws Error if database initialization failed
|
|
*/
|
|
public async getClient(): Promise<Client> {
|
|
return this.client!
|
|
}
|
|
|
|
/**
|
|
* Check if database is initialized
|
|
*/
|
|
public isInitialized(): boolean {
|
|
return this.state === InitState.INITIALIZED
|
|
}
|
|
}
|