-
Notifications
You must be signed in to change notification settings - Fork 58
feat(dump): qualify same-schema type references under --qualify-schema (#493) #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+540
−87
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b856f1
feat(dump): qualify same-schema type references under --qualify-schem…
tianzhou dbae465
fix(dump): handle quoted target schema + regenerate plan fingerprints…
tianzhou 2463734
fix(diff): strip typmod in type-dependency key matching (#493)
tianzhou 0f5b5dd
fix(dump): qualify same-schema array element types (#493)
tianzhou e6e4043
fix(dump): qualify same-schema user-defined array types in domains, c…
tianzhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package dump | ||
|
|
||
| // Positive integration coverage for `dump --qualify-schema` type references (#493). | ||
| // | ||
| // Unlike the builder-level tests in internal/diff (which hand the builder a | ||
| // pre-qualified IR), this test drives the whole pipeline: pgdump SQL → embedded | ||
| // database → inspector → IR → dump. It proves the inspector now *preserves* the | ||
| // schema of same-schema user-defined type references for the four "Mechanism A" | ||
| // slices (column type, domain base type, composite attribute, aggregate state | ||
| // type), so --qualify-schema can emit them fully qualified — while the default | ||
| // (smart-qualification) dump keeps them bare. | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/pgplex/pgschema/testutil" | ||
| ) | ||
|
|
||
| const qualifySchemaSetupSQL = ` | ||
| CREATE TYPE color AS ENUM ('r', 'g', 'b'); | ||
| -- column type: same-schema enum (scalar and array) | ||
| CREATE TABLE swatch ( | ||
| id integer PRIMARY KEY, | ||
| shade color, | ||
| shades color[] | ||
| ); | ||
| -- domain base type: same-schema enum (scalar and array) | ||
| CREATE DOMAIN color_domain AS color; | ||
| CREATE DOMAIN color_list AS color[]; | ||
| -- composite attribute: same-schema enum (scalar and array, plus a built-in that must stay bare) | ||
| CREATE TYPE money_amount AS ( | ||
| amount numeric, | ||
| currency color, | ||
| palette color[] | ||
| ); | ||
| -- aggregate state type: same-schema composite | ||
| CREATE TYPE acc AS (n integer); | ||
| CREATE FUNCTION acc_add(acc, integer) RETURNS acc | ||
| LANGUAGE sql IMMUTABLE AS $$ SELECT ROW(($1).n + $2)::acc $$; | ||
| CREATE AGGREGATE mysum(integer) (SFUNC = acc_add, STYPE = acc); | ||
| ` | ||
|
|
||
| func TestDumpCommand_QualifySchemaTypeReferences(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("Skipping integration test in short mode") | ||
| } | ||
|
|
||
| embeddedPG := testutil.SetupPostgres(t) | ||
| defer embeddedPG.Stop() | ||
|
|
||
| conn, host, port, dbname, user, password := testutil.ConnectToPostgres(t, embeddedPG) | ||
| defer conn.Close() | ||
|
|
||
| if _, err := conn.ExecContext(context.Background(), qualifySchemaSetupSQL); err != nil { | ||
| t.Fatalf("Failed to set up schema: %v", err) | ||
| } | ||
|
|
||
| baseConfig := func(qualify bool) *DumpConfig { | ||
| return &DumpConfig{ | ||
| Host: host, | ||
| Port: port, | ||
| DB: dbname, | ||
| User: user, | ||
| Password: password, | ||
| Schema: "public", | ||
| MultiFile: false, | ||
| File: "", | ||
| QualifySchema: qualify, | ||
| } | ||
| } | ||
|
|
||
| // --qualify-schema: same-schema type references are fully qualified. | ||
| qualified, err := ExecuteDump(baseConfig(true)) | ||
| if err != nil { | ||
| t.Fatalf("qualified dump failed: %v", err) | ||
| } | ||
| for _, want := range []string{ | ||
| "shade public.color", // column type | ||
| "shades public.color[]", // column type: same-schema array | ||
| "AS public.color;", // domain base type: scalar (CREATE DOMAIN ... AS public.color) | ||
| "AS public.color[]", // domain base type: array (CREATE DOMAIN ... AS public.color[]) | ||
| "currency public.color", // composite attribute: scalar | ||
| "palette public.color[]", // composite attribute: array | ||
| "STYPE = public.acc", // aggregate state type | ||
| } { | ||
| if !strings.Contains(qualified, want) { | ||
| t.Errorf("qualified dump missing %q\n---\n%s", want, qualified) | ||
| } | ||
| } | ||
| // Built-in types must never be schema-qualified. | ||
| if strings.Contains(qualified, "pg_catalog.") { | ||
| t.Errorf("qualified dump must not qualify built-in types with pg_catalog:\n%s", qualified) | ||
| } | ||
| if !strings.Contains(qualified, "amount numeric") { | ||
| t.Errorf("qualified dump should keep built-in composite attr type bare (amount numeric):\n%s", qualified) | ||
| } | ||
|
|
||
| // Default (smart qualification): the same references stay bare. | ||
| def, err := ExecuteDump(baseConfig(false)) | ||
| if err != nil { | ||
| t.Fatalf("default dump failed: %v", err) | ||
| } | ||
| for _, want := range []string{ | ||
| "shade color", | ||
| "shades color[]", | ||
| "AS color;", // domain base type: scalar | ||
| "AS color[]", // domain base type: array | ||
| "currency color", | ||
| "palette color[]", | ||
| "STYPE = acc", | ||
| } { | ||
| if !strings.Contains(def, want) { | ||
| t.Errorf("default dump missing bare form %q\n---\n%s", want, def) | ||
| } | ||
| } | ||
| if strings.Contains(def, "public.color") || strings.Contains(def, "public.acc") { | ||
| t.Errorf("default dump must not qualify same-schema type references:\n%s", def) | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.