Skip to content

feat(scripts): extend revert/verify vocabulary + node-level AST API - #328

Merged
pyramation merged 3 commits into
mainfrom
feat/scripts-extended-inverses
Jul 31, 2026
Merged

feat(scripts): extend revert/verify vocabulary + node-level AST API#328
pyramation merged 3 commits into
mainfrom
feat/scripts-extended-inverses

Conversation

@pyramation

@pyramation pyramation commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Summary

Extends @pgsql/scripts in three directions, keeping the never-guess contract (un-derivable → -- revert not derivable: <reason> + warning; inverses are AST-built and deparsed, never string-templated; reverse topo order):

1. Easy-win vocabulary — matviews/CTAS, FDW family (server/foreign table/user mapping), collation/aggregate/binary operator/cast, publication/subscription/statistics/event trigger/rule, ATTACH PARTITIONDETACH PARTITION, ALTER DEFAULT PRIVILEGES GRANTREVOKE, SECURITY LABELIS NULL, plus a pg_enum verify for ADD VALUE (no revert — Postgres has no DROP VALUE).

2. Stateless groups — inverses that need no prior state because the statement carries both sides:

  • RenameStmt / AlterObjectSchemaStmt: swap old/new name (or schema) — tables, columns, indexes, sequences, views, types, functions, schemas. SET SCHEMA on an unqualified name warns (original schema unknown).
  • GRANT ALL: revert stays REVOKE ALL; verify expands to the object type's concrete privilege list (ALL_PRIVILEGES per objtype; sequences now use has_sequence_privilege).
  • Long-tail creates: FDW, conversion, access method, transform, operator class/family, text search configuration/dictionary/parser/template, tablespace → matching DROP + catalog existence checks (pg_foreign_data_wrapper, pg_conversion, pg_am, pg_transform, pg_opclass/pg_opfamily, pg_ts_*, pg_tablespace).

3. Node-level API — the per-statement layer under revertFor/verifyFor, for consumers that compose inverses at the AST level (semantic diff driver, bundle reconcile, migration gen) without round-tripping through deparsed text:

invertStatement(facts: StatementFacts): Node[] | null  // wrapped stmt nodes, [] = nothing to revert, null = not derivable
existenceCheck(facts: StatementFacts): Node[] | null   // SelectStmt check nodes, [] = nothing to check, null = not derivable

Both return null instead of guessing — including partially underivable multi-command ALTER TABLEs (no silent partial inverses). revertFor/verifyFor remain the text-level conveniences on top.

Two deparser fixes surfaced by round-tripping the generated inverses: ALTER TYPE/DOMAIN ... RENAME now dot-joins qualified names (was comma-joining the List), and GRANT/REVOKE ... ON SEQUENCE now emits the SEQUENCE keyword.

87 exact-output tests; SUPPORTED_STATEMENTS/README updated. Part of the Phase 4 follow-up (constructive-planning#1329).

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

New mechanical inverses and existence checks: materialized views /
CREATE TABLE AS, foreign servers / foreign tables / user mappings,
collations, aggregates, binary operators, casts, publications,
subscriptions, extended statistics, event triggers, rules,
ATTACH PARTITION (-> DETACH), ALTER DEFAULT PRIVILEGES GRANT (-> REVOKE)
and SECURITY LABEL (-> IS NULL). ALTER TYPE ADD VALUE gains a pg_enum
verify check (no revert: Postgres has no DROP VALUE). Prefix operators
and DEFAULT PRIVILEGES REVOKE warn instead of guessing.
@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

…ong-tail creates, node-level API

- RenameStmt / AlterObjectSchemaStmt inverses (the statement carries both sides)
- GRANT ALL verify expansion to the object type's concrete privilege list
- FDW, conversion, access method, transform, opclass/opfamily, text search
  objects and tablespaces: DROP inverses + catalog existence checks
- has_sequence_privilege for sequence grants
- deparser: dot-join qualified names in ALTER TYPE/DOMAIN RENAME; emit
  SEQUENCE keyword in GRANT/REVOKE ON SEQUENCE
- node-level API: invertStatement(facts) -> Node[] | null and
  existenceCheck(facts) -> Node[] | null under revertFor/verifyFor
@devin-ai-integration devin-ai-integration Bot changed the title feat(scripts): extend revert/verify vocabulary (matviews, FDW, defines, replication, partitions, default privileges, security labels) feat(scripts): extend revert/verify vocabulary + node-level AST API Jul 31, 2026
@pyramation
pyramation merged commit d8250aa 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