mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(schedules): clean up orphaned schedule records when repo is deleted (#255)
* fix(schedules): clean up orphaned schedule records and add schedule URL state management * refactor(schedules): consolidate schedule tab state into useScheduleUrlState * fix(schedules): cascade delete schedule runs/jobs and preserve assistant schedules in orphan cleanup * refactor(schedules): replace deleteJobsByRepo with prepareRepoDelete, add lightweight ID query, remove dead mock * refactor(schedules): consolidate PromptTemplateDialog form state into single object
This commit is contained in:
1 parent
70cb62b77b
commit
cc89a524c4
24 files changed
+1938
-327
No files matched your search
@@ -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)
|
||||
}
|
||||
|
||||
@@ -116,6 +116,12 @@ export function listScheduleJobsByRepo(db: Database, repoId: number): ScheduleJo
|
||||
return rows.map(rowToScheduleJob)
|
||||
}
|
||||
|
||||
export function listScheduleJobIdsByRepo(db: Database, repoId: number): number[] {
|
||||
const stmt = db.prepare('SELECT id FROM schedule_jobs WHERE repo_id = ? ORDER BY created_at DESC')
|
||||
const rows = stmt.all(repoId) as Array<{ id: number }>
|
||||
return rows.map((row) => row.id)
|
||||
}
|
||||
|
||||
export function listEnabledScheduleJobs(db: Database): ScheduleJob[] {
|
||||
const stmt = db.prepare('SELECT * FROM schedule_jobs WHERE enabled = 1 ORDER BY id ASC')
|
||||
const rows = stmt.all() as ScheduleJobRow[]
|
||||
@@ -200,11 +206,29 @@ 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 cleanupOrphanedSchedules(db: Database): { orphanedJobs: number; orphanedRuns: number } {
|
||||
const runStmt = db.prepare(`
|
||||
DELETE FROM schedule_runs
|
||||
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(ASSISTANT_REPO_ID).changes
|
||||
|
||||
const jobStmt = db.prepare(`
|
||||
DELETE FROM schedule_jobs
|
||||
WHERE repo_id != ? AND repo_id NOT IN (SELECT id FROM repos)
|
||||
`)
|
||||
const orphanedJobs = jobStmt.run(ASSISTANT_REPO_ID).changes
|
||||
|
||||
return { orphanedJobs, orphanedRuns }
|
||||
}
|
||||
|
||||
export function updateScheduleJobRunState(db: Database, repoId: number, jobId: number, values: { lastRunAt: number; nextRunAt?: number | null }): void {
|
||||
const stmt = db.prepare('UPDATE schedule_jobs SET last_run_at = ?, next_run_at = ?, updated_at = ? WHERE repo_id = ? AND id = ?')
|
||||
stmt.run(values.lastRunAt, values.nextRunAt ?? null, Date.now(), repoId, jobId)
|
||||
|
||||
Reference in new issue
Block a user