/** * A generated `DROP COLUMN` has to be as guarded as the `ADD COLUMN` beside it * (stacksjs/stacks#2895). * * The generator guarded the column it adds and not the column it drops, in the * same pass. These files are committed, and a corpus is replayed from empty in * CI, in a container entrypoint, and on any rebuild, so a bare drop only ever * applied to the one database that still had the column and failed everywhere * else, permanently. It was also written during `migrate --force` on the * machine where it did apply, so it was green for its author and red for * everyone else. * * Two things were wrong, and either alone leaves the bug in place: the rewrite * did not cover `DROP COLUMN`, and the `touchable` gate in * `normalizePostgresMigrationSql` did not list it either, so a file whose only * statement is a drop never reached the transform at all. That is the shape the * generator actually writes. * * Postgres only, which is what this transform runs on. The issue proposed the * same change for SQLite on the grounds that 3.35+ supports it; it does not, * and the last test here is that claim turned into an executable fact so the * next person does not have to re-learn it. SQLite's corpus is reconciled * against the live schema instead, in `preprocessSqliteMigrations`. */ import { Database } from 'bun:sqlite' import { describe, expect, it } from 'bun:test' import { idempotentSql, normalizePostgresMigrationSql } from '../src/migrations' /** What the generator writes, header and all. */ const GENERATED_DROP = `-- qb:generated -- @generated by \`buddy migrate:regenerate\` - edits will be overwritten ALTER TABLE "plan_drafts" DROP COLUMN "scratch_probe"; ` describe('a generated DROP COLUMN', () => { it('is guarded, so the corpus replays from empty', () => { expect(idempotentSql('ALTER TABLE "plan_drafts" DROP COLUMN "scratch_probe";')) .toContain('DROP COLUMN IF EXISTS "scratch_probe"') }) it('is guarded even when the drop is the whole file', () => { // The gate, not the rewrite. This is the file the reproduction produces, // and before the gate listed DROP COLUMN it was returned untouched. expect(normalizePostgresMigrationSql(GENERATED_DROP)).toContain('DROP COLUMN IF EXISTS "scratch_probe"') }) it('leaves no bare drop behind', () => { // `toContain` on the guarded form would also pass if a second, unguarded // copy were emitted beside it. const out = normalizePostgresMigrationSql(GENERATED_DROP) expect(out).not.toMatch(/DROP\s+COLUMN\s+(?!IF\s+EXISTS)/i) }) it('is a fixed point, so migrate does not rewrite the file on every run', () => { // These are git-tracked files. A transform that is not a fixed point shows // up as an unexplained working-tree change after every `buddy migrate`. const once = normalizePostgresMigrationSql(GENERATED_DROP) expect(normalizePostgresMigrationSql(once)).toBe(once) expect(once).not.toMatch(/IF\s+EXISTS\s+IF\s+EXISTS/i) }) }) describe('what the drop guard must not disturb', () => { it('still guards ADD COLUMN', () => { expect(idempotentSql('ALTER TABLE "t" ADD COLUMN "a" varchar(255);')) .toContain('ADD COLUMN IF NOT EXISTS "a"') }) it('guards both when one file does both', () => { const out = idempotentSql('ALTER TABLE "t" ADD COLUMN "a" varchar(255);\nALTER TABLE "t" DROP COLUMN "b";') expect(out).toContain('ADD COLUMN IF NOT EXISTS "a"') expect(out).toContain('DROP COLUMN IF EXISTS "b"') }) it('does not touch DROP CONSTRAINT, which already had its own guard', () => { const out = idempotentSql('ALTER TABLE "t" DROP CONSTRAINT IF EXISTS "t_fk";') expect(out).toContain('DROP CONSTRAINT IF EXISTS "t_fk"') expect(out).not.toMatch(/DROP\s+COLUMN/i) }) it('preserves ON DELETE on a re-added foreign key', () => { /* * The issue's second, unconfirmed case: a regenerated constraint losing the * `ON DELETE CASCADE` the original declared, which turns a cascading delete * into a raising one. Not reproduced here, and this does not close it - but * it establishes that this transform is not where such a clause would be * lost, so the question belongs to whatever emits the statement. */ const fk = 'ALTER TABLE "tpa" ADD CONSTRAINT "tpa_plan_id_fk" FOREIGN KEY ("plan_id") REFERENCES "training_plans"("id") ON DELETE CASCADE;' expect(idempotentSql(fk)).toContain('ON DELETE CASCADE') }) }) describe('why this is the Postgres transform and not a shared one', () => { it('records that SQLite accepts neither guarded form', () => { /* * The claim in #2895 was that `DROP COLUMN IF EXISTS` works on SQLite 3.35 * and later. It does not, and neither does `ADD COLUMN IF NOT EXISTS`, so * applying this rewrite to the SQLite corpus would turn a replay failure * into a syntax error on every statement. Asserted against the engine * rather than stated, because a version bump could change it and this * should fail loudly if it ever does. */ const db = new Database(':memory:') db.run('CREATE TABLE t (id INTEGER, a TEXT)') expect(() => db.run('ALTER TABLE t DROP COLUMN IF EXISTS a')).toThrow() expect(() => db.run('ALTER TABLE t ADD COLUMN IF NOT EXISTS b TEXT')).toThrow() // The unguarded forms are what SQLite does accept, which is why its corpus // is reconciled against the live schema instead of rewritten. expect(() => db.run('ALTER TABLE t ADD COLUMN b TEXT')).not.toThrow() expect(() => db.run('ALTER TABLE t DROP COLUMN a')).not.toThrow() }) })