Skip to content

feat(scripts): @pgsql/scripts — revert and verify generation from statement facts - #327

Merged
pyramation merged 2 commits into
mainfrom
feat/revert-verify-generation
Jul 31, 2026
Merged

feat(scripts): @pgsql/scripts — revert and verify generation from statement facts#327
pyramation merged 2 commits into
mainfrom
feat/revert-verify-generation

Conversation

@pyramation

@pyramation pyramation commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Summary

Phase 4 follow-up (refs constructive-io/constructive-planning#1329): a new package, @pgsql/scripts, derives revert and verify scripts from @pgsql/transform statement facts — the missing third leg next to the parser and deparser. Consumed by constructive-io/constructive#1562 so pgpm export --granularity stops emitting empty revert/verify files.

revertFor(facts: StatementFacts[]): GeneratedScript  // { sql, warnings }
verifyFor(facts: StatementFacts[]): GeneratedScript
  • revertFor — mechanical inverses in reverse topological order of buildStatementGraph(facts), so dependents drop before dependencies and no CASCADE is ever emitted. Inverses are built as AST nodes and deparsed (never string-templated): CREATE schema/table/view/index/sequence/type/domain/function/trigger/policy/extension/role → DROP (functions keep their input signature for overload safety); ADD COLUMN/ADD CONSTRAINTDROP; GRANTREVOKE; RLS enable/force → disable/no-force; COMMENT ON... IS NULL. Non-invertible statements (DML, dynamic SQL, ALTER ... SET with unknown prior state, unnamed constraints, REVOKE) emit -- revert not derivable: <reason> plus a warning — never guessed.
  • verifyFor — one raise-on-failure existence check per created object via SELECT 1/(CASE WHEN <exists> THEN 1 ELSE 0 END);: to_regclass/to_regprocedure/to_regtype where a reg* cast exists, catalog lookups (pg_policies, pg_trigger, pg_extension, pg_roles, pg_auth_members, information_schema.columns/table_constraints) for the rest, has_table_privilege/has_function_privilege/has_schema_privilege for grants.
  • The supported vocabulary is exported (SUPPORTED_STATEMENTS, SUPPORTED_NODE_TAGS) and documented as a table in the package README — consumers can check support programmatically instead of only hitting warnings.

Supporting changes in @pgsql/transform:

  • StatementFacts.stmt? now carries the raw parsed statement node so consumers can derive statements without a second parse.
  • Fixed CreateDomainStmt classification: the domain's name is domainname (typeName is its base type).

45 exact-output unit tests in packages/scripts/__tests__/invert.test.ts; @pgsql/scripts added to the CI test matrix.

Link to Devin session: https://app.devin.ai/sessions/7f032c8e1bf245eeac2def99fdcc1623
Requested by: @pyramation

@pyramation pyramation self-assigned this Jul 31, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Migration script generation gets its own package next to the parser and
deparser: @pgsql/scripts derives revert and verify scripts from
@pgsql/transform statement facts. Adds SUPPORTED_STATEMENTS /
SUPPORTED_NODE_TAGS documenting the supported vocabulary; anything else
warns instead of guessing.
@devin-ai-integration devin-ai-integration Bot changed the title feat(transform): revertFor/verifyFor — revert and verify generation from statement facts feat(scripts): @pgsql/scripts — revert and verify generation from statement facts Jul 31, 2026
@pyramation
pyramation merged commit 186729a into main Jul 31, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant