import { ScratchDatabase, createScratchDatabase, } from './utils/migration-harness'; const BACKFILL = '20261021100000_queen_deactivated_backfill'; const TIMEOUT_MS = 240_000; describe('Queen deactivatedAt backfill migration (e2e)', () => { let db: ScratchDatabase | undefined; afterEach(async () => { await db?.drop(); db = undefined; }); it( 'dates every unavailable queen without deactivatedAt by her last colony end, else her last change, and leaves the rest alone', async () => { db = await createScratchDatabase(BACKFILL); await db.query( `INSERT INTO "Apiary" ("id", "name", "updatedAt") VALUES ('apiary-1', 'Stand', now())`, ); await db.query( `INSERT INTO "Colony" ("id", "code", "apiaryId", "colonyType", "updatedAt") VALUES ('colony-1', 'V-1', 'apiary-1', 'PRODUCTION_COLONY', now())`, ); await db.query( `INSERT INTO "Queen" ("id", "code", "status", "updatedAt", "deactivatedAt") VALUES ('q-dead-history', 'Q-1', 'DECEASED', '2026-09-01T00:00:00Z', NULL), ('q-sold-nohistory', 'Q-2', 'SOLD', '2026-08-15T00:00:00Z', NULL), ('q-lost-dated', 'Q-3', 'LOST', '2026-07-01T00:00:00Z', '2026-06-01T00:00:00Z'), ('q-active', 'Q-4', 'ACTIVE', '2026-09-01T00:00:00Z', NULL), ('q-replaced-open', 'Q-5', 'REPLACED', '2026-09-10T00:00:00Z', NULL)`, ); await db.query( `INSERT INTO "QueenColonyHistory" ("id", "queenId", "colonyId", "startDate", "endDate") VALUES ('h-1', 'q-dead-history', 'colony-1', '2025-04-01T00:00:00Z', '2025-10-01T00:00:00Z'), ('h-2', 'q-dead-history', 'colony-1', '2026-03-01T00:00:00Z', '2026-05-20T00:00:00Z'), ('h-3', 'q-active', 'colony-1', '2026-05-20T00:00:00Z', NULL), ('h-4', 'q-replaced-open', 'colony-1', '2026-01-01T00:00:00Z', NULL)`, ); await db.migrateThrough(BACKFILL); expect( await db.query( `SELECT "id", to_char("deactivatedAt", 'YYYY-MM-DD') AS "deactivatedOn" FROM "Queen" ORDER BY "id"`, ), ).toEqual([ { id: 'q-active', deactivatedOn: null }, { id: 'q-dead-history', deactivatedOn: '2026-05-20' }, { id: 'q-lost-dated', deactivatedOn: '2026-06-01' }, { id: 'q-replaced-open', deactivatedOn: '2026-09-10' }, { id: 'q-sold-nohistory', deactivatedOn: '2026-08-15' }, ]); }, TIMEOUT_MS, ); });