diff --git a/backend/src/db/queries.ts b/backend/src/db/queries.ts index ce06cc65..486429e4 100644 --- a/backend/src/db/queries.ts +++ b/backend/src/db/queries.ts @@ -281,6 +281,10 @@ export function updateRepoBranch(db: Database, id: number, branch: string): void } export function deleteRepo(db: Database, id: number): void { + if (id === ASSISTANT_REPO_ID) { + return + } + for (const table of TABLES_WITH_REPO_ID) { db.prepare(`DELETE FROM ${table} WHERE repo_id = ?`).run(id) } diff --git a/backend/src/db/schema.ts b/backend/src/db/schema.ts index b02f14f0..39b5749f 100644 --- a/backend/src/db/schema.ts +++ b/backend/src/db/schema.ts @@ -5,6 +5,7 @@ import { dirname } from 'path' import { migrate } from './migration-runner' import { allMigrations } from './migrations' import { ensureOpenCodeModelStateTable } from './model-state' +import { ensureAssistantRepo } from './queries' export function initializeDatabase(dbPath: string = './data/opencode.db'): Database { mkdirSync(dirname(dbPath), { recursive: true }) @@ -15,6 +16,7 @@ export function initializeDatabase(dbPath: string = './data/opencode.db'): Datab db.prepare('INSERT OR IGNORE INTO user_preferences (user_id, preferences, updated_at) VALUES (?, ?, ?)') .run('default', '{}', Date.now()) + ensureAssistantRepo(db) logger.info('Database initialized successfully') diff --git a/backend/test/db/queries.test.ts b/backend/test/db/queries.test.ts index 6c860d60..a2817fb6 100644 --- a/backend/test/db/queries.test.ts +++ b/backend/test/db/queries.test.ts @@ -282,6 +282,12 @@ describe('Database Queries', () => { ) expect(deleteRepoStmt.run).toHaveBeenCalledWith(1) }) + + it('should not delete the assistant repo', () => { + db.deleteRepo(mockDb, 0) + + expect(mockDb.prepare).not.toHaveBeenCalled() + }) }) describe('Database Schema', () => {