|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { mkdtempSync, readFileSync, rmSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { describe, expect, it } from 'vitest' |
| 6 | + |
| 7 | +const workflow = readFileSync( |
| 8 | + new URL('../.github/workflows/migrations.yml', import.meta.url), |
| 9 | + 'utf8' |
| 10 | +) |
| 11 | +const step = workflow.match(/^ {8}run: \|\r?\n((?: {10}.*(?:\r?\n|$)|\r?\n)+)/m)?.[1] |
| 12 | +if (!step) throw new Error('Migration workflow must contain its schema application shell step') |
| 13 | +const script = step.replace(/^ {10}/gm, '') |
| 14 | + |
| 15 | +/** Execute the checked-in shell with fake database commands and an isolated scratch log. */ |
| 16 | +function runMigration(env: Record<string, string> = {}) { |
| 17 | + const directory = mkdtempSync(join(tmpdir(), 'migration-workflow-')) |
| 18 | + try { |
| 19 | + return spawnSync( |
| 20 | + 'bash', |
| 21 | + [ |
| 22 | + '-e', |
| 23 | + '-c', |
| 24 | + `bun() { |
| 25 | + printf 'COMMAND: %s\n' "$*" |
| 26 | + case "$2" in |
| 27 | + db:push) printf '%s\n' "$PUSH_OUTPUT"; return "$PUSH_EXIT" ;; |
| 28 | + ./scripts/apply-dev-workspace-file-size-cutover.ts) return "$CUTOVER_EXIT" ;; |
| 29 | + ./scripts/migrate.ts) return "$MIGRATE_EXIT" ;; |
| 30 | + *) return 99 ;; |
| 31 | + esac |
| 32 | + } |
| 33 | + ${script.replaceAll('/tmp/db-push.log', '"$MIGRATION_TEST_LOG"')}`, |
| 34 | + ], |
| 35 | + { |
| 36 | + encoding: 'utf8', |
| 37 | + env: { |
| 38 | + PATH: process.env.PATH, |
| 39 | + DATABASE_URL: 'postgresql://example.invalid/unused', |
| 40 | + MIGRATION_DATABASE_URL: '', |
| 41 | + ENVIRONMENT: 'dev', |
| 42 | + MIGRATION_TEST_LOG: join(directory, 'push.log'), |
| 43 | + PUSH_EXIT: '0', |
| 44 | + PUSH_OUTPUT: 'Changes applied', |
| 45 | + CUTOVER_EXIT: '0', |
| 46 | + MIGRATE_EXIT: '0', |
| 47 | + ...env, |
| 48 | + }, |
| 49 | + } |
| 50 | + ) |
| 51 | + } finally { |
| 52 | + rmSync(directory, { recursive: true, force: true }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +describe('migration workflow exit propagation', () => { |
| 57 | + it('fails when post-schema initialization fails before tee succeeds', () => { |
| 58 | + const result = runMigration({ |
| 59 | + PUSH_EXIT: '42', |
| 60 | + PUSH_OUTPUT: 'Changes applied\nDATABASE_URL is required to initialize search vectors', |
| 61 | + }) |
| 62 | + expect(result.status).toBe(42) |
| 63 | + expect(result.stdout).toContain('DATABASE_URL is required') |
| 64 | + expect(result.stdout).not.toContain( |
| 65 | + 'COMMAND: run ./scripts/apply-dev-workspace-file-size-cutover.ts' |
| 66 | + ) |
| 67 | + }) |
| 68 | + |
| 69 | + it('runs the dev cutover only after a successful schema push', () => { |
| 70 | + const result = runMigration() |
| 71 | + expect(result.status).toBe(0) |
| 72 | + expect(result.stdout).toContain('COMMAND: run db:push --force') |
| 73 | + expect(result.stdout).toContain( |
| 74 | + 'COMMAND: run ./scripts/apply-dev-workspace-file-size-cutover.ts' |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + it('still rejects drizzle interactive failures that exit zero', () => { |
| 79 | + const result = runMigration({ PUSH_OUTPUT: 'Interactive prompts require a TTY terminal' }) |
| 80 | + expect(result.status).toBe(1) |
| 81 | + expect(result.stdout).not.toContain( |
| 82 | + 'COMMAND: run ./scripts/apply-dev-workspace-file-size-cutover.ts' |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it('propagates dev cutover failures', () => { |
| 87 | + expect(runMigration({ CUTOVER_EXIT: '43' }).status).toBe(43) |
| 88 | + }) |
| 89 | + |
| 90 | + it('keeps versioned migration failures fatal outside dev', () => { |
| 91 | + const result = runMigration({ ENVIRONMENT: 'staging', MIGRATE_EXIT: '44' }) |
| 92 | + expect(result.status).toBe(44) |
| 93 | + expect(result.stdout).toContain('COMMAND: run ./scripts/migrate.ts') |
| 94 | + expect(result.stdout).not.toContain('COMMAND: run db:push') |
| 95 | + }) |
| 96 | + |
| 97 | + it('fails before invoking commands when no database URL is configured', () => { |
| 98 | + const result = runMigration({ DATABASE_URL: '' }) |
| 99 | + expect(result.status).toBe(1) |
| 100 | + expect(result.stdout).not.toContain('COMMAND:') |
| 101 | + }) |
| 102 | +}) |
0 commit comments