diff --git a/.github/workflows/schema-drift.yml b/.github/workflows/schema-drift.yml index 7ccb7b0..43e4121 100644 --- a/.github/workflows/schema-drift.yml +++ b/.github/workflows/schema-drift.yml @@ -38,7 +38,7 @@ jobs: - name: Build the expected schema from committed migrations env: DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres - run: yarn db:migrate + run: yarn db:drift:seed-expected - name: Compare production against it env: EXPECTED_DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres diff --git a/package.json b/package.json index b5cb229..5b45ee5 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "db:generate": "drizzle-kit generate", "db:migrate": "drizzle-kit migrate", "db:drift": "tsx scripts/schema-drift/check.ts", + "db:drift:seed-expected": "tsx scripts/schema-drift/apply-migrations.ts", "db:push": "drizzle-kit push" }, "dependencies": { diff --git a/scripts/schema-drift/apply-migrations.ts b/scripts/schema-drift/apply-migrations.ts new file mode 100644 index 0000000..a6de431 --- /dev/null +++ b/scripts/schema-drift/apply-migrations.ts @@ -0,0 +1,30 @@ +/** + * Build the "expected" schema for drift comparison using the exact same migration + * mechanism production uses (app/lib/db/migrations.ts -> public.__clawproxy_migrations), + * rather than `drizzle-kit migrate` which tracks in a different table + * (drizzle.__drizzle_migrations) that production never creates. + */ +import postgres from 'postgres'; + +import { applyCommittedMigrations } from '../../app/lib/db/migrations'; + +async function main() { + const url = process.env.DATABASE_URL; + if (!url) { + console.error('DATABASE_URL is required'); + process.exit(2); + } + + const sql = postgres(url, { max: 1 }); + try { + await applyCommittedMigrations(sql); + console.log('[schema-drift] applied committed migrations to the expected database'); + } finally { + await sql.end({ timeout: 5 }); + } +} + +main().catch((error) => { + console.error('[schema-drift] failed to apply migrations:', error); + process.exit(2); +}); diff --git a/scripts/schema-drift/collect.ts b/scripts/schema-drift/collect.ts index 3cb5e30..e35ad38 100644 --- a/scripts/schema-drift/collect.ts +++ b/scripts/schema-drift/collect.ts @@ -38,8 +38,12 @@ export async function collectSnapshot(databaseUrl: string): Promise` - SELECT hash FROM drizzle.__drizzle_migrations ORDER BY created_at, id + SELECT hash FROM public.__clawproxy_migrations ORDER BY applied_at, name `; return {