-
Notifications
You must be signed in to change notification settings - Fork 58
fix: defer new table inline FK when it depends on a new unique constraint (#506) #507
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
Merged
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions
24
testdata/diff/create_table/issue_506_fk_depends_on_new_unique/diff.sql
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,24 @@ | ||
| CREATE TABLE IF NOT EXISTS child ( | ||
| id uuid, | ||
| parent_id uuid, | ||
| tenant text NOT NULL, | ||
| CONSTRAINT child_pkey PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS child2 ( | ||
| id uuid, | ||
| parent2_id uuid, | ||
| tenant text NOT NULL, | ||
| CONSTRAINT child2_pkey PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| ALTER TABLE parent | ||
| ADD CONSTRAINT parent_id_tenant_key UNIQUE (id, tenant); | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS parent2_id_tenant_key ON parent2 (id, tenant); | ||
|
|
||
| ALTER TABLE child | ||
| ADD CONSTRAINT child_parent_id_tenant_fkey FOREIGN KEY (parent_id, tenant) REFERENCES parent (id, tenant); | ||
|
|
||
| ALTER TABLE child2 | ||
| ADD CONSTRAINT child2_parent2_id_tenant_fkey FOREIGN KEY (parent2_id, tenant) REFERENCES parent2 (id, tenant); |
26 changes: 26 additions & 0 deletions
26
testdata/diff/create_table/issue_506_fk_depends_on_new_unique/new.sql
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,26 @@ | ||
| CREATE TABLE parent ( | ||
| id uuid PRIMARY KEY, | ||
| tenant text NOT NULL, | ||
| UNIQUE (id, tenant) | ||
| ); | ||
|
|
||
| CREATE TABLE child ( | ||
| id uuid PRIMARY KEY, | ||
| parent_id uuid, | ||
| tenant text NOT NULL, | ||
| FOREIGN KEY (parent_id, tenant) REFERENCES parent (id, tenant) | ||
| ); | ||
|
|
||
| CREATE TABLE parent2 ( | ||
| id uuid PRIMARY KEY, | ||
| tenant text NOT NULL | ||
| ); | ||
|
|
||
| CREATE UNIQUE INDEX parent2_id_tenant_key ON parent2 (id, tenant); | ||
|
|
||
| CREATE TABLE child2 ( | ||
| id uuid PRIMARY KEY, | ||
| parent2_id uuid, | ||
| tenant text NOT NULL, | ||
| FOREIGN KEY (parent2_id, tenant) REFERENCES parent2 (id, tenant) | ||
| ); |
9 changes: 9 additions & 0 deletions
9
testdata/diff/create_table/issue_506_fk_depends_on_new_unique/old.sql
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,9 @@ | ||
| CREATE TABLE parent ( | ||
| id uuid PRIMARY KEY, | ||
| tenant text NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE parent2 ( | ||
| id uuid PRIMARY KEY, | ||
| tenant text NOT NULL | ||
| ); |
72 changes: 72 additions & 0 deletions
72
testdata/diff/create_table/issue_506_fk_depends_on_new_unique/plan.json
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,72 @@ | ||
| { | ||
| "version": "1.0.0", | ||
| "pgschema_version": "1.12.0", | ||
| "created_at": "1970-01-01T00:00:00Z", | ||
| "source_fingerprint": { | ||
| "hash": "a2e9edaed4b13aa629d2ed847ffd01634bf02251aaa187e810dc64ecc17497e1" | ||
| }, | ||
| "groups": [ | ||
| { | ||
| "steps": [ | ||
| { | ||
| "sql": "CREATE TABLE IF NOT EXISTS child (\n id uuid,\n parent_id uuid,\n tenant text NOT NULL,\n CONSTRAINT child_pkey PRIMARY KEY (id)\n);", | ||
| "type": "table", | ||
| "operation": "create", | ||
| "path": "public.child" | ||
| }, | ||
| { | ||
| "sql": "CREATE TABLE IF NOT EXISTS child2 (\n id uuid,\n parent2_id uuid,\n tenant text NOT NULL,\n CONSTRAINT child2_pkey PRIMARY KEY (id)\n);", | ||
| "type": "table", | ||
| "operation": "create", | ||
| "path": "public.child2" | ||
| }, | ||
| { | ||
| "sql": "ALTER TABLE parent\nADD CONSTRAINT parent_id_tenant_key UNIQUE (id, tenant);", | ||
| "type": "table.constraint", | ||
| "operation": "create", | ||
| "path": "public.parent.parent_id_tenant_key" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "steps": [ | ||
| { | ||
| "sql": "CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS parent2_id_tenant_key ON parent2 (id, tenant);", | ||
| "type": "table.index", | ||
| "operation": "create", | ||
| "path": "public.parent2.parent2_id_tenant_key" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "steps": [ | ||
| { | ||
| "sql": "SELECT \n COALESCE(i.indisvalid, false) as done,\n CASE \n WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total\n ELSE 0\n END as progress\nFROM pg_class c\nLEFT JOIN pg_index i ON c.oid = i.indexrelid\nLEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid\nWHERE c.relname = 'parent2_id_tenant_key';", | ||
| "directive": { | ||
| "type": "wait", | ||
| "message": "Creating index parent2_id_tenant_key" | ||
| }, | ||
| "type": "table.index", | ||
| "operation": "create", | ||
| "path": "public.parent2.parent2_id_tenant_key" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "steps": [ | ||
| { | ||
| "sql": "ALTER TABLE child\nADD CONSTRAINT child_parent_id_tenant_fkey FOREIGN KEY (parent_id, tenant) REFERENCES parent (id, tenant);", | ||
| "type": "table.constraint", | ||
| "operation": "create", | ||
| "path": "public.child.child_parent_id_tenant_fkey" | ||
| }, | ||
| { | ||
| "sql": "ALTER TABLE child2\nADD CONSTRAINT child2_parent2_id_tenant_fkey FOREIGN KEY (parent2_id, tenant) REFERENCES parent2 (id, tenant);", | ||
| "type": "table.constraint", | ||
| "operation": "create", | ||
| "path": "public.child2.child2_parent2_id_tenant_fkey" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
36 changes: 36 additions & 0 deletions
36
testdata/diff/create_table/issue_506_fk_depends_on_new_unique/plan.sql
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,36 @@ | ||
| CREATE TABLE IF NOT EXISTS child ( | ||
| id uuid, | ||
| parent_id uuid, | ||
| tenant text NOT NULL, | ||
| CONSTRAINT child_pkey PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS child2 ( | ||
| id uuid, | ||
| parent2_id uuid, | ||
| tenant text NOT NULL, | ||
| CONSTRAINT child2_pkey PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| ALTER TABLE parent | ||
| ADD CONSTRAINT parent_id_tenant_key UNIQUE (id, tenant); | ||
|
|
||
| CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS parent2_id_tenant_key ON parent2 (id, tenant); | ||
|
|
||
| -- pgschema:wait | ||
| SELECT | ||
| COALESCE(i.indisvalid, false) as done, | ||
| CASE | ||
| WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total | ||
| ELSE 0 | ||
| END as progress | ||
| FROM pg_class c | ||
| LEFT JOIN pg_index i ON c.oid = i.indexrelid | ||
| LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid | ||
| WHERE c.relname = 'parent2_id_tenant_key'; | ||
|
|
||
| ALTER TABLE child | ||
| ADD CONSTRAINT child_parent_id_tenant_fkey FOREIGN KEY (parent_id, tenant) REFERENCES parent (id, tenant); | ||
|
|
||
| ALTER TABLE child2 | ||
| ADD CONSTRAINT child2_parent2_id_tenant_fkey FOREIGN KEY (parent2_id, tenant) REFERENCES parent2 (id, tenant); |
58 changes: 58 additions & 0 deletions
58
testdata/diff/create_table/issue_506_fk_depends_on_new_unique/plan.txt
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,58 @@ | ||
| Plan: 2 to add, 2 to modify. | ||
|
|
||
| Summary by type: | ||
| tables: 2 to add, 2 to modify | ||
|
|
||
| Tables: | ||
| + child | ||
| + child_parent_id_tenant_fkey (constraint) | ||
| + child2 | ||
| + child2_parent2_id_tenant_fkey (constraint) | ||
| ~ parent | ||
| + parent_id_tenant_key (constraint) | ||
| ~ parent2 | ||
| + parent2_id_tenant_key (index) | ||
|
|
||
| DDL to be executed: | ||
| -------------------------------------------------- | ||
|
|
||
| -- Transaction Group #1 | ||
| CREATE TABLE IF NOT EXISTS child ( | ||
| id uuid, | ||
| parent_id uuid, | ||
| tenant text NOT NULL, | ||
| CONSTRAINT child_pkey PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS child2 ( | ||
| id uuid, | ||
| parent2_id uuid, | ||
| tenant text NOT NULL, | ||
| CONSTRAINT child2_pkey PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| ALTER TABLE parent | ||
| ADD CONSTRAINT parent_id_tenant_key UNIQUE (id, tenant); | ||
|
|
||
| -- Transaction Group #2 | ||
| CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS parent2_id_tenant_key ON parent2 (id, tenant); | ||
|
|
||
| -- Transaction Group #3 | ||
| -- pgschema:wait | ||
| SELECT | ||
| COALESCE(i.indisvalid, false) as done, | ||
| CASE | ||
| WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total | ||
| ELSE 0 | ||
| END as progress | ||
| FROM pg_class c | ||
| LEFT JOIN pg_index i ON c.oid = i.indexrelid | ||
| LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid | ||
| WHERE c.relname = 'parent2_id_tenant_key'; | ||
|
|
||
| -- Transaction Group #4 | ||
| ALTER TABLE child | ||
| ADD CONSTRAINT child_parent_id_tenant_fkey FOREIGN KEY (parent_id, tenant) REFERENCES parent (id, tenant); | ||
|
|
||
| ALTER TABLE child2 | ||
| ADD CONSTRAINT child2_parent2_id_tenant_fkey FOREIGN KEY (parent2_id, tenant) REFERENCES parent2 (id, tenant); |
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.