import { Database } from 'bun:sqlite' import { afterEach, describe, expect, it, vi } from 'vitest' import { migrate, type Migration } from '../../src/db/migration-runner' import { allMigrations } from '../../src/db/migrations' import { logger } from '../../src/utils/logger' function makeMigration(id: string, up: () => void = vi.fn(), legacy?: Migration['legacy']): Migration { return { id, legacy, up, down: () => {} } } function createLegacyTable(db: Database, rows: Array<[number, string]>): void { db.run('CREATE TABLE schema_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at INTEGER NOT NULL)') rows.forEach(([version, name]) => { db.prepare('INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, 0)').run(version, name) }) } function appliedIds(db: Database): string[] { return (db.prepare('SELECT id FROM applied_migrations ORDER BY id').all() as Array<{ id: string }>).map((row) => row.id) } describe('migrate', () => { afterEach(() => { vi.restoreAllMocks() }) it('applies pending migrations in list order and records them by id', () => { const db = new Database(':memory:') const order: string[] = [] migrate(db, [ makeMigration('202610061345-second', () => order.push('second')), makeMigration('202610051200-first', () => order.push('first')), ]) expect(order).toEqual(['second', 'first']) expect(appliedIds(db)).toEqual(['202610051200-first', '202610061345-second']) }) it('does not run a migration twice', () => { const db = new Database(':memory:') const up = vi.fn() migrate(db, [makeMigration('202610061345-once', up)]) migrate(db, [makeMigration('202610061345-once', up)]) expect(up).toHaveBeenCalledTimes(1) }) it('runs a migration whose old version number another branch already used', () => { const db = new Database(':memory:') createLegacyTable(db, [[28, 'remote-devices']]) const up = vi.fn() migrate(db, [makeMigration('202610061345-schedule-workspace-mode', up)]) expect(up).toHaveBeenCalledTimes(1) }) it('adopts numbered migrations recorded in the old version-keyed table', () => { const db = new Database(':memory:') createLegacyTable(db, [[15, 'schedule-worktree-isolation']]) const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {}) const up = vi.fn() migrate(db, [makeMigration('015-schedule-worktree-isolation', up, { version: 15, name: 'schedule-worktree-isolation' })]) expect(up).not.toHaveBeenCalled() expect(appliedIds(db)).toEqual(['015-schedule-worktree-isolation']) expect(warnSpy).not.toHaveBeenCalled() }) it('warns and skips when a legacy version was recorded under a different name', () => { const db = new Database(':memory:') createLegacyTable(db, [[15, 'repos-add-name']]) const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {}) const up = vi.fn() migrate(db, [makeMigration('015-schedule-worktree-isolation', up, { version: 15, name: 'schedule-worktree-isolation' })]) expect(up).not.toHaveBeenCalled() expect(warnSpy).toHaveBeenCalledWith( expect.stringContaining('Migration version 15 is recorded as "repos-add-name" but the code defines "schedule-worktree-isolation"'), ) }) it('rejects duplicate ids before touching the database', () => { const db = new Database(':memory:') const up = vi.fn() expect(() => migrate(db, [makeMigration('202610061345-a', up), makeMigration('202610061345-a', up)])).toThrow('Duplicate migration id') expect(up).not.toHaveBeenCalled() }) it('rolls back and stops when a migration fails', () => { const db = new Database(':memory:') vi.spyOn(logger, 'error').mockImplementation(() => {}) const later = vi.fn() expect(() => migrate(db, [ makeMigration('202610061345-broken', () => { db.run('CREATE TABLE partial (id INTEGER)') throw new Error('boom') }), makeMigration('202610061346-later', later), ])).toThrow('boom') expect(later).not.toHaveBeenCalled() expect(appliedIds(db)).toEqual([]) expect(db.prepare("SELECT name FROM sqlite_master WHERE name = 'partial'").get()).toBeFalsy() }) }) describe('allMigrations', () => { it('uses timestamp ids for every migration added after the numbered ones', () => { const unnumbered = allMigrations.filter((migration) => !migration.legacy) expect(unnumbered.length).toBeGreaterThan(0) unnumbered.forEach((migration) => { expect(migration.id).toMatch(/^\d{12}-[a-z0-9]+(?:-[a-z0-9]+)*$/) }) }) })