From e5fe479eb5f9bf9bed4a340be57b9fa496c9125e Mon Sep 17 00:00:00 2001 From: Ahmet Gedemenli Date: Wed, 20 Oct 2021 18:12:55 +0300 Subject: [PATCH] Revert&Adjust fkey tests --- .../citus_add_local_table_to_metadata.c | 3 +- .../expected/fkeys_between_local_ref.out | 65 ++++++++++++++++++- .../regress/sql/fkeys_between_local_ref.sql | 41 ++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c index 9bde3d928..bdc31e1a6 100644 --- a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c +++ b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c @@ -273,8 +273,7 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys, bool autoConve Oid shellRelationId = get_relname_relid(relationName, relationSchemaId); - /* mark the shell relation with autoConverted=false, as it was a user request */ - UpdatePartitionAutoConverted(shellRelationId, false); + UpdatePartitionAutoConverted(shellRelationId, autoConverted); /* * We converted every foreign key connected table in our subgraph diff --git a/src/test/regress/expected/fkeys_between_local_ref.out b/src/test/regress/expected/fkeys_between_local_ref.out index 87429f0f3..49fc9d100 100644 --- a/src/test/regress/expected/fkeys_between_local_ref.out +++ b/src/test/regress/expected/fkeys_between_local_ref.out @@ -103,6 +103,25 @@ BEGIN; ALTER TABLE reference_table_1 ADD CONSTRAINT fkey_9 FOREIGN KEY (col_1) REFERENCES local_table_1(col_1); ROLLBACK; ALTER TABLE partitioned_table_1 DROP CONSTRAINT fkey_8; +BEGIN; + -- now that we detached partitioned table from graph, succeeds + ALTER TABLE reference_table_1 ADD CONSTRAINT fkey_10 FOREIGN KEY (col_1) REFERENCES local_table_1(col_1); + -- show that we converted all 4 local tables in this schema to citus local tables + SELECT COUNT(*)=4 FROM citus_local_tables_in_schema; + ?column? +--------------------------------------------------------------------- + t +(1 row) + + -- dropping that column would undistribute those 4 citus local tables + ALTER TABLE local_table_1 DROP COLUMN col_1 CASCADE; + SELECT COUNT(*)=0 FROM citus_local_tables_in_schema; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +ROLLBACK; -- this actually attempts to convert local tables to citus local tables but errors out -- as citus doesn't support defining foreign keys via add column commands ALTER TABLE local_table_1 ADD COLUMN col_3 INT REFERENCES reference_table_1(col_1); @@ -309,7 +328,51 @@ BEGIN; -- show that we validate foreign key constraints, errors out INSERT INTO local_table_5 VALUES (300); -ERROR: insert or update on table "local_table_5_1518069" violates foreign key constraint "local_table_5_col_1_fkey1_1518069" +ERROR: insert or update on table "local_table_5_1518073" violates foreign key constraint "local_table_5_col_1_fkey1_1518073" +ROLLBACK; +BEGIN; + CREATE SCHEMA another_schema_fkeys_between_local_ref; + CREATE TABLE another_schema_fkeys_between_local_ref.local_table_6 (col_1 INT PRIMARY KEY); + -- first convert local tables to citus local tables in graph + ALTER TABLE local_table_2 ADD CONSTRAINT fkey_11 FOREIGN KEY (col_1) REFERENCES reference_table_1(col_1) ON DELETE CASCADE; + CREATE TABLE local_table_5 ( + col_1 INT UNIQUE REFERENCES another_schema_fkeys_between_local_ref.local_table_6(col_1) CHECK (col_1 > 0), + col_2 INT REFERENCES local_table_3(col_1), + FOREIGN KEY (col_1) REFERENCES local_table_5(col_1)); + -- Now show that we converted local_table_5 & 6 to citus local tables + -- as local_table_5 has foreign key to a citus local table too + SELECT logicalrelid::text AS tablename, partmethod, repmodel FROM pg_dist_partition + WHERE logicalrelid::text IN (SELECT tablename FROM pg_tables WHERE schemaname='fkeys_between_local_ref' UNION + SELECT 'another_schema_fkeys_between_local_ref.local_table_6') + ORDER BY tablename; + tablename | partmethod | repmodel +--------------------------------------------------------------------- + another_schema_fkeys_between_local_ref.local_table_6 | n | s + distributed_table | h | s + local_table_1 | n | s + local_table_2 | n | s + local_table_3 | n | s + local_table_4 | n | s + local_table_5 | n | s + reference_table_1 | n | t +(8 rows) + + DROP TABLE local_table_3 CASCADE; + DROP SCHEMA another_schema_fkeys_between_local_ref CASCADE; + -- now we shouldn't see local_table_5 since now it is not connected to any reference tables/citus local tables + -- and it's converted automatically + SELECT logicalrelid::text AS tablename, partmethod, repmodel, autoconverted FROM pg_dist_partition + WHERE logicalrelid::text IN (SELECT tablename FROM pg_tables WHERE schemaname='fkeys_between_local_ref') + ORDER BY tablename; + tablename | partmethod | repmodel | autoconverted +--------------------------------------------------------------------- + distributed_table | h | s | f + local_table_1 | n | s | t + local_table_2 | n | s | t + local_table_4 | n | s | t + reference_table_1 | n | t | f +(5 rows) + ROLLBACK; BEGIN; CREATE TABLE local_table_6 (col_1 INT PRIMARY KEY); diff --git a/src/test/regress/sql/fkeys_between_local_ref.sql b/src/test/regress/sql/fkeys_between_local_ref.sql index 0468246af..630cde968 100644 --- a/src/test/regress/sql/fkeys_between_local_ref.sql +++ b/src/test/regress/sql/fkeys_between_local_ref.sql @@ -87,6 +87,18 @@ ALTER TABLE reference_table_1 ADD CONSTRAINT fkey_9 FOREIGN KEY (col_1) REFERENC ROLLBACK; ALTER TABLE partitioned_table_1 DROP CONSTRAINT fkey_8; +BEGIN; + -- now that we detached partitioned table from graph, succeeds + ALTER TABLE reference_table_1 ADD CONSTRAINT fkey_10 FOREIGN KEY (col_1) REFERENCES local_table_1(col_1); + + -- show that we converted all 4 local tables in this schema to citus local tables + SELECT COUNT(*)=4 FROM citus_local_tables_in_schema; + + -- dropping that column would undistribute those 4 citus local tables + ALTER TABLE local_table_1 DROP COLUMN col_1 CASCADE; + SELECT COUNT(*)=0 FROM citus_local_tables_in_schema; +ROLLBACK; + -- this actually attempts to convert local tables to citus local tables but errors out -- as citus doesn't support defining foreign keys via add column commands ALTER TABLE local_table_1 ADD COLUMN col_3 INT REFERENCES reference_table_1(col_1); @@ -233,6 +245,35 @@ BEGIN; INSERT INTO local_table_5 VALUES (300); ROLLBACK; +BEGIN; + CREATE SCHEMA another_schema_fkeys_between_local_ref; + CREATE TABLE another_schema_fkeys_between_local_ref.local_table_6 (col_1 INT PRIMARY KEY); + + -- first convert local tables to citus local tables in graph + ALTER TABLE local_table_2 ADD CONSTRAINT fkey_11 FOREIGN KEY (col_1) REFERENCES reference_table_1(col_1) ON DELETE CASCADE; + + CREATE TABLE local_table_5 ( + col_1 INT UNIQUE REFERENCES another_schema_fkeys_between_local_ref.local_table_6(col_1) CHECK (col_1 > 0), + col_2 INT REFERENCES local_table_3(col_1), + FOREIGN KEY (col_1) REFERENCES local_table_5(col_1)); + + -- Now show that we converted local_table_5 & 6 to citus local tables + -- as local_table_5 has foreign key to a citus local table too + SELECT logicalrelid::text AS tablename, partmethod, repmodel FROM pg_dist_partition + WHERE logicalrelid::text IN (SELECT tablename FROM pg_tables WHERE schemaname='fkeys_between_local_ref' UNION + SELECT 'another_schema_fkeys_between_local_ref.local_table_6') + ORDER BY tablename; + + DROP TABLE local_table_3 CASCADE; + DROP SCHEMA another_schema_fkeys_between_local_ref CASCADE; + + -- now we shouldn't see local_table_5 since now it is not connected to any reference tables/citus local tables + -- and it's converted automatically + SELECT logicalrelid::text AS tablename, partmethod, repmodel, autoconverted FROM pg_dist_partition + WHERE logicalrelid::text IN (SELECT tablename FROM pg_tables WHERE schemaname='fkeys_between_local_ref') + ORDER BY tablename; +ROLLBACK; + BEGIN; CREATE TABLE local_table_6 (col_1 INT PRIMARY KEY); -- first convert local tables to citus local tables in graph