Skip to content

migrate-ts: --allow adopt-view emits illegal CREATE OR REPLACE VIEW for structural view changes (fails at apply) #239

Closed

Description

@dmealing

Summary

meta migrate --allow adopt-view emits CREATE OR REPLACE VIEW for a view that is both being fingerprint-adopted and structurally changed (column rename / reorder / mid-list insert) in the same migration. Postgres rejects that DDL at apply time, aborting the migration on any DB that already holds the prior view.

Reproduces on 0.19.1 through 0.20.3 (the relevant forward-emit paths are unchanged across those versions).

Root cause

The normal view-diff path is correct: pushViewUpdate (migrate-ts/src/diff/index.ts:604) routes through viewReplaceIsLegal(expected.columns, actual.columns) — legal (append-only) → replace-view; illegal (removed/renamed/reordered/retyped) → drop-view + create-view.

The adopt-view branch bypasses that check. When a DB view carries no fingerprint (created before view fingerprinting), the adopt path pushes a replace-view change unconditionally:

  • diff/index.ts:~562-566 — if (a.fingerprint === undefined) { changes.push({ kind: "replace-view", view: v, ... }) ... }
  • diff/index.ts:~583 — the fingerprint-mismatch case, same unconditional replace-view push.

Neither call runs viewReplaceIsLegal, even though the old view (a / restore) is in hand. emit/postgres.ts:75 then renders every replace-view as CREATE OR REPLACE VIEW (orReplace = true) — which is correct only under the invariant that replace-view is emitted solely for legal replaces. The adopt branch violates that invariant.

Postgres constraint: CREATE OR REPLACE VIEW may only append columns at the end of the existing column list; it cannot rename, reorder, or mid-insert. Such a change fails with e.g. ERROR: cannot change name of view column "old_name" to "new_name".

The down-migration path already handles this correctly (renderRestoreView → viewReplaceIsLegal(restore.columns, current.columns), emit/postgres.ts:386). It's specifically the forward adopt-view branch that skips the check.

Why it's invisible until apply

meta verify --codegen and offline meta migrate don't touch a DB, and a freshly-provisioned DB that has no prior view lets CREATE OR REPLACE behave as a plain CREATE. The failure only surfaces when the migration is applied to a DB that already holds the prior view definition — i.e. a canonically-migrated / production DB. Because viewReplaceIsLegal "fails safe" (a wrong yes is not caught at plan time, only rejected by Postgres at apply time), the migration is emitted clean and dies on deploy.

This is most likely to bite exactly once per project: the first migration authored after upgrading into view-fingerprinting, when previously-unstamped views get adopted and happen to change shape in the same migration.

Minimal repro (synthetic)

  1. A DB holds a view with NO metaobjects fingerprint comment (created before view stamping):
    CREATE VIEW v_foo AS SELECT t.a AS a, t.src AS b FROM t;   -- output cols: (a, b)
  2. Metadata now defines v_foo with a structural change, e.g. a renamed output column:
    projection v_foo → output cols (a, c)   -- 'b' renamed to 'c'  (or: (a, x, b) mid-insert)
    
  3. meta migrate --slug adopt --dialect postgres --allow adopt-view
  4. Emitted up.sql (abbreviated):
    CREATE OR REPLACE VIEW "v_foo" AS SELECT t.a AS a, t.src AS c FROM t;
    COMMENT ON VIEW "v_foo" IS 'metaobjects:v1:sha256:...';
  5. Apply against the DB from step 1:
    ERROR: cannot change name of view column "b" to "c"
    
    (A pure end-append adoption succeeds — confirming the diagnosis is column-shape-specific.)

Expected

The adopt-view branch should make the same legal/illegal decision the normal path makes: run viewReplaceIsLegal(expected.columns, actual.columns) (the old columns are already available as a / restore) and emit drop-view + create-view when the adoption is not a legal OR-REPLACE, keeping CREATE OR REPLACE only for body-identical or pure-append adoptions.

Suggested fix

Route the adopt-view replace through pushViewUpdate (or inline the viewReplaceIsLegal guard) instead of pushing replace-view unconditionally at diff/index.ts:~564 and ~583. That reuses the existing, already-correct legality logic and keeps emit's invariant ("replace-view ⇒ legal OR-REPLACE") intact.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions