feat(transform): statement dependency graph + granularity restructuring (atomic ↔ object ↔ consolidated) - #325
Merged
Conversation
…ng (atomic/object/consolidated)
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two modules to
@pgsql/transformthat let a DDL script be rewritten between equivalent shapes without changing the schema it deploys — the substrate for exporting pgpm modules at different human-readability levels.graph.ts—buildStatementGraph(facts): turnsclassifyStatementsoutput into a typed statement dependency graph:hard(must resolve at CREATE time),fk(hard, but the edges FK-inlining must respect), orlate(PL/pgSQL body references — call-time bound, never ordering constraints, legitimately cyclic);restructure.ts—restructureSql(sql, { granularity })with three levels:atomic: explodeCREATE TABLEinto bare create + oneADD COLUMNper column + oneADD CONSTRAINTper constraint (column-level PK/UNIQUE/FK promoted to table-level; DEFAULT/NOT NULL/IDENTITY stay inline on the column);object: fold columns and same-table constraints back into theCREATE TABLE; cross-object statements stay separate;consolidated: additionally inline FKs as table constraints when the graph proves the referenced table can be ordered first.Fold safety is a union-find merge over the statement graph: folding an
ALTERinto itsCREATEmerges their nodes and unions their dependencies, and a candidate fold is rejected iff it would put the merged node on a cycle. This is what makes mutual FKs degrade gracefully — the first FK inlines, the second is provably cycle-forming and stays anALTER TABLE(with a warning). Output is re-emitted in the graph's stable topo order, so forward references are repaired for free.Tests cover fold/explode round-trips, self-FKs, mutual-FK cycle rejection, late-edge (PL/pgSQL) cycles, and forward-reference reordering.
Link to Devin session: https://app.devin.ai/sessions/562099f8b3d94877b5ab57ec8bb90bec
Requested by: @pyramation