mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(schedules): cascade delete schedule runs/jobs and preserve assistant schedules in orphan cleanup
This commit is contained in:
1 parent
6ae3d07b6c
commit
6fc7ec6213
7 files changed
+144
-98
No files matched your search
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -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])
|
||||
})
|
||||
})
|
||||
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user