diff --git a/backend/src/db/queries.ts b/backend/src/db/queries.ts index 26a5f7e0..8bf3a0c3 100644 --- a/backend/src/db/queries.ts +++ b/backend/src/db/queries.ts @@ -209,7 +209,8 @@ export function updateRepoBranch(db: Database, id: number, branch: string): void } export function deleteRepo(db: Database, id: number): void { + db.prepare('DELETE FROM schedule_runs WHERE repo_id = ?').run(id) + db.prepare('DELETE FROM schedule_jobs WHERE repo_id = ?').run(id) const stmt = db.prepare('DELETE FROM repos WHERE id = ?') stmt.run(id) } - diff --git a/backend/src/db/schedules.ts b/backend/src/db/schedules.ts index 91d13ab5..7e5bb7a1 100644 --- a/backend/src/db/schedules.ts +++ b/backend/src/db/schedules.ts @@ -200,13 +200,13 @@ export function updateScheduleJob(db: Database, repoId: number, jobId: number, i } export function deleteScheduleJob(db: Database, repoId: number, jobId: number): boolean { + db.prepare('DELETE FROM schedule_runs WHERE repo_id = ? AND job_id = ?').run(repoId, jobId) const stmt = db.prepare('DELETE FROM schedule_jobs WHERE repo_id = ? AND id = ?') const result = stmt.run(repoId, jobId) return result.changes > 0 } export function deleteScheduleJobsByRepo(db: Database, repoId: number): number { - // Delete runs first in case FK enforcement is off (e.g. during migration) db.prepare('DELETE FROM schedule_runs WHERE repo_id = ?').run(repoId) const stmt = db.prepare('DELETE FROM schedule_jobs WHERE repo_id = ?') const result = stmt.run(repoId) @@ -216,16 +216,16 @@ export function deleteScheduleJobsByRepo(db: Database, repoId: number): number { export function cleanupOrphanedSchedules(db: Database): { orphanedJobs: number; orphanedRuns: number } { const runStmt = db.prepare(` DELETE FROM schedule_runs - WHERE repo_id NOT IN (SELECT id FROM repos) + WHERE (repo_id != ? AND repo_id NOT IN (SELECT id FROM repos)) OR job_id NOT IN (SELECT id FROM schedule_jobs) `) - const orphanedRuns = runStmt.run().changes + const orphanedRuns = runStmt.run(ASSISTANT_REPO_ID).changes const jobStmt = db.prepare(` DELETE FROM schedule_jobs - WHERE repo_id NOT IN (SELECT id FROM repos) + WHERE repo_id != ? AND repo_id NOT IN (SELECT id FROM repos) `) - const orphanedJobs = jobStmt.run().changes + const orphanedJobs = jobStmt.run(ASSISTANT_REPO_ID).changes return { orphanedJobs, orphanedRuns } } diff --git a/backend/test/db/queries.test.ts b/backend/test/db/queries.test.ts index d909817d..62a359be 100644 --- a/backend/test/db/queries.test.ts +++ b/backend/test/db/queries.test.ts @@ -258,18 +258,31 @@ describe('Database Queries', () => { }) describe('deleteRepo', () => { - it('should delete repo by ID', () => { - const stmt = { + it('should delete repo schedules before deleting repo by ID', () => { + const deleteRunsStmt = { + run: vi.fn().mockReturnValue({ changes: 2 }) + } + const deleteJobsStmt = { run: vi.fn().mockReturnValue({ changes: 1 }) } - mockDb.prepare.mockReturnValue(stmt) + const deleteRepoStmt = { + run: vi.fn().mockReturnValue({ changes: 1 }) + } + mockDb.prepare + .mockReturnValueOnce(deleteRunsStmt) + .mockReturnValueOnce(deleteJobsStmt) + .mockReturnValueOnce(deleteRepoStmt) db.deleteRepo(mockDb, 1) - expect(mockDb.prepare).toHaveBeenCalledWith( + expect(mockDb.prepare).toHaveBeenNthCalledWith(1, 'DELETE FROM schedule_runs WHERE repo_id = ?') + expect(deleteRunsStmt.run).toHaveBeenCalledWith(1) + expect(mockDb.prepare).toHaveBeenNthCalledWith(2, 'DELETE FROM schedule_jobs WHERE repo_id = ?') + expect(deleteJobsStmt.run).toHaveBeenCalledWith(1) + expect(mockDb.prepare).toHaveBeenNthCalledWith(3, 'DELETE FROM repos WHERE id = ?' ) - expect(stmt.run).toHaveBeenCalledWith(1) + expect(deleteRepoStmt.run).toHaveBeenCalledWith(1) }) }) diff --git a/backend/test/db/schedules-assistant.test.ts b/backend/test/db/schedules-assistant.test.ts index 4c271988..045fb703 100644 --- a/backend/test/db/schedules-assistant.test.ts +++ b/backend/test/db/schedules-assistant.test.ts @@ -3,6 +3,7 @@ import { Database } from 'bun:sqlite' import { migrate } from '../../src/db/migration-runner' import { allMigrations } from '../../src/db/migrations' import { + cleanupOrphanedSchedules, listAllScheduleJobsWithRepos, listAllScheduleRuns, } from '../../src/db/schedules' @@ -101,4 +102,14 @@ describe('assistant repo (repo_id=0) in global aggregate queries', () => { expect(run.repoName).toBe('Assistant') expect(run.repoPath).toBe('assistant') }) + + it('cleanupOrphanedSchedules keeps assistant schedules while deleting real repo orphans', () => { + db.exec('DELETE FROM repos WHERE id = 1') + + const result = cleanupOrphanedSchedules(db) + + expect(result).toEqual({ orphanedJobs: 1, orphanedRuns: 1 }) + expect(listAllScheduleJobsWithRepos(db).map((job) => job.repoId)).toEqual([0]) + expect(listAllScheduleRuns(db, {}).map((run) => run.repoId)).toEqual([0]) + }) }) diff --git a/backend/test/db/schedules.test.ts b/backend/test/db/schedules.test.ts index 6371765b..771e69b3 100644 --- a/backend/test/db/schedules.test.ts +++ b/backend/test/db/schedules.test.ts @@ -173,6 +173,27 @@ describe('schedule database queries', () => { }) }) + it('deletes schedule runs before deleting a schedule job', () => { + const deleteRunsStmt = { + run: vi.fn().mockReturnValue({ changes: 2 }), + } + const deleteJobStmt = { + run: vi.fn().mockReturnValue({ changes: 1 }), + } + + mockDb.prepare + .mockReturnValueOnce(deleteRunsStmt) + .mockReturnValueOnce(deleteJobStmt) + + const deleted = schedulesDb.deleteScheduleJob(mockDb, 42, 7) + + expect(mockDb.prepare).toHaveBeenNthCalledWith(1, 'DELETE FROM schedule_runs WHERE repo_id = ? AND job_id = ?') + expect(deleteRunsStmt.run).toHaveBeenCalledWith(42, 7) + expect(mockDb.prepare).toHaveBeenNthCalledWith(2, 'DELETE FROM schedule_jobs WHERE repo_id = ? AND id = ?') + expect(deleteJobStmt.run).toHaveBeenCalledWith(42, 7) + expect(deleted).toBe(true) + }) + it('returns null when updating metadata for a missing run', () => { const selectStmt = { get: vi.fn().mockReturnValue(undefined), diff --git a/frontend/src/components/schedules/PromptTab.tsx b/frontend/src/components/schedules/PromptTab.tsx index 56184daa..49f7b1da 100644 --- a/frontend/src/components/schedules/PromptTab.tsx +++ b/frontend/src/components/schedules/PromptTab.tsx @@ -17,6 +17,77 @@ type PromptTabProps = { onNewTemplate: () => void } +type PromptTemplateCardProps = { + template: PromptTemplate + selected?: boolean + onApply?: (template: PromptTemplate) => void + onEdit: (template: PromptTemplate) => void + onDelete: (templateId: number) => void +} + +export function PromptTemplateCard({ template, selected = false, onApply, onEdit, onDelete }: PromptTemplateCardProps) { + const cardClassName = `w-full rounded-xl border-2 p-4 text-left transition-all ${ + selected + ? 'border-primary bg-primary/10 ring-2 ring-primary/30' + : 'border-border bg-card hover:bg-accent/40' + }` + + const content = ( + <> +
+ + {template.category} + + + {template.cadenceHint} + +
+
+

{template.title}

+

{template.description}

+
+

{template.suggestedDescription}

+ {selected && ( +
+ +
+ )} + + ) + + return ( +
+ {onApply ? ( + + ) : ( +
{content}
+ )} +
+ + +
+
+ ) +} + export function PromptTab({ prompt, onPromptChange, @@ -50,56 +121,14 @@ export function PromptTab({ const isSelected = selectedPromptTemplateId === template.id return ( -
- -
- - -
-
+ ) })} diff --git a/frontend/src/components/schedules/PromptsTab.tsx b/frontend/src/components/schedules/PromptsTab.tsx index 20333610..16f93a14 100644 --- a/frontend/src/components/schedules/PromptsTab.tsx +++ b/frontend/src/components/schedules/PromptsTab.tsx @@ -3,12 +3,12 @@ import type { CreatePromptTemplateRequest } from '@opencode-manager/shared/types import type { PromptDialog } from '@/hooks/useScheduleUrlState' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' -import { Badge } from '@/components/ui/badge' -import { Loader2, FileText, Plus, Upload, Pencil, Trash2 } from 'lucide-react' +import { Loader2, FileText, Plus, Upload } from 'lucide-react' import { usePromptTemplates, useDeletePromptTemplate } from '@/hooks/usePromptTemplates' import { parseMarkdownTemplate } from '@/lib/schedules/markdownTemplate' import { PromptTemplateDialog } from './PromptTemplateDialog' import { DeleteDialog } from '@/components/ui/delete-dialog' +import { PromptTemplateCard } from './PromptTab' interface PromptsTabProps { promptDialog: PromptDialog @@ -130,41 +130,12 @@ export function PromptsTab({ promptDialog, templateId, onNew, onEdit, onDelete, ) : (
{templates.map((template) => ( -
-
- - {template.category} - - - {template.cadenceHint} - -
-
-

{template.title}

-

{template.description}

-
-

{template.suggestedDescription}

-
- - -
-
+ onEdit(item.id)} + onDelete={onDelete} + /> ))}
)}