diff --git a/src/main/services/agents/BaseService.ts b/src/main/services/agents/BaseService.ts index c06e501213..967e7d612e 100644 --- a/src/main/services/agents/BaseService.ts +++ b/src/main/services/agents/BaseService.ts @@ -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') diff --git a/src/main/services/agents/schemaSyncer.ts b/src/main/services/agents/schemaSyncer.ts new file mode 100644 index 0000000000..7ee86d8936 --- /dev/null +++ b/src/main/services/agents/schemaSyncer.ts @@ -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 { + 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}`) + } +} \ No newline at end of file