mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-29 05:51:26 +08:00
feat(agents): add automatic database schema synchronization
- Add schemaSyncer.ts with Drizzle Kit push integration - Integrate auto schema sync into BaseService.initialize() - Database schema now automatically updates on agent service startup - Users no longer need manual migration commands - Ensures schema consistency across app updates Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
parent
71ed94de31
commit
f90bda861f
@ -5,6 +5,7 @@ import { app } from 'electron'
|
||||
import path from 'path'
|
||||
|
||||
import * as schema from './database/schema'
|
||||
import { syncDatabaseSchema } from './schemaSyncer'
|
||||
|
||||
const logger = loggerService.withContext('BaseService')
|
||||
|
||||
@ -38,8 +39,8 @@ export abstract class BaseService {
|
||||
|
||||
BaseService.db = drizzle(BaseService.client, { schema })
|
||||
|
||||
// For new development, tables will be created by Drizzle Kit migrations
|
||||
// or can be created programmatically as needed
|
||||
// Auto-sync database schema on startup
|
||||
await syncDatabaseSchema()
|
||||
|
||||
BaseService.isInitialized = true
|
||||
logger.info('Agent database initialized successfully')
|
||||
|
||||
39
src/main/services/agents/schemaSyncer.ts
Normal file
39
src/main/services/agents/schemaSyncer.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { execSync } from 'child_process'
|
||||
import { loggerService } from '@logger'
|
||||
import path from 'path'
|
||||
|
||||
const logger = loggerService.withContext('SchemaSyncer')
|
||||
|
||||
/**
|
||||
* Synchronizes database schema using Drizzle Kit push command.
|
||||
* This automatically detects schema differences and applies necessary changes.
|
||||
*
|
||||
* Uses the existing drizzle.config.ts configuration to push schema changes
|
||||
* to the agents database on service startup.
|
||||
*/
|
||||
export async function syncDatabaseSchema(): Promise<void> {
|
||||
const configPath = path.join(process.cwd(), 'src/main/services/agents/drizzle.config.ts')
|
||||
|
||||
try {
|
||||
logger.info('Starting database schema synchronization...')
|
||||
|
||||
// Use drizzle-kit push to sync schema automatically
|
||||
const output = execSync(`npx drizzle-kit push --config ${configPath}`, {
|
||||
stdio: 'pipe',
|
||||
encoding: 'utf-8',
|
||||
cwd: process.cwd(),
|
||||
timeout: 30000 // 30 second timeout
|
||||
})
|
||||
|
||||
logger.info('Database schema synchronized successfully')
|
||||
|
||||
// Log output for debugging if needed
|
||||
if (output && output.trim()) {
|
||||
logger.debug('Drizzle Kit output:', output.trim())
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
logger.error('Schema synchronization failed:', error as Error)
|
||||
throw new Error(`Database schema sync failed: ${(error as Error).message}`)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user