Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH] sql: make default constraint names be consistent
@ 2019-07-31 10:55 Nikita Pettik
  2019-08-01 16:42 ` [tarantool-patches] " n.pettik
  2019-08-02 11:01 ` Kirill Yukhin
  0 siblings, 2 replies; 3+ messages in thread
From: Nikita Pettik @ 2019-07-31 10:55 UTC (permalink / raw)
  To: tarantool-patches; +Cc: kostja, Nikita Pettik

If during creation of constraint its name is not specified, then it is
generated automatically. Occasionally, default names for each type of
constraint turn out to be different. This patch makes them follow the
same pattern: "shortcut"_unnamed_"table_name"_"ordinal_numb". For
instance: fk_unnamed_T1_1 or ck_unnamed_T1_3 etc
---

Branch: https://github.com/tarantool/tarantool/commits/np/fix-constraint-names

 src/box/sql/build.c                   |  9 ++++-----
 test/sql-tap/check.test.lua           | 32 ++++++++++++++++----------------
 test/sql-tap/fkey1.test.lua           |  2 +-
 test/sql-tap/fkey2.test.lua           | 16 ++++++++--------
 test/sql-tap/sql-errors.test.lua      |  4 ++--
 test/sql-tap/table.test.lua           | 16 ++++++++--------
 test/sql/checks.result                | 10 +++++-----
 test/sql/drop-table.result            |  2 +-
 test/sql/foreign-keys.result          |  3 +--
 test/sql/gh-2981-check-autoinc.result |  8 ++++----
 test/sql/types.result                 |  6 +++---
 11 files changed, 53 insertions(+), 55 deletions(-)

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index 0a6759e41..e02b7f864 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -724,8 +724,7 @@ sql_create_check_contraint(struct Parse *parser)
 	} else {
 		assert(! is_alter);
 		uint32_t ck_idx = ++parser->create_table_def.check_count;
-		name = tt_sprintf("CK_CONSTRAINT_%d_%s", ck_idx,
-				  space->def->name);
+		name = tt_sprintf("ck_unnamed_%s_%d", space->def->name, ck_idx);
 	}
 	size_t name_len = strlen(name);
 
@@ -1973,9 +1972,9 @@ sql_create_foreign_key(struct Parse *parse_context)
 	if (!is_alter) {
 		if (create_def->name.n == 0) {
 			constraint_name =
-				sqlMPrintf(db, "FK_CONSTRAINT_%d_%s",
-					       ++table_def->fkey_count,
-					       space->def->name);
+				sqlMPrintf(db, "fk_unnamed_%s_%d",
+					   space->def->name,
+					   ++table_def->fkey_count);
 		} else {
 			constraint_name =
 				sql_name_from_token(db, &create_def->name);
diff --git a/test/sql-tap/check.test.lua b/test/sql-tap/check.test.lua
index 7bd5f8177..79256ae67 100755
--- a/test/sql-tap/check.test.lua
+++ b/test/sql-tap/check.test.lua
@@ -55,7 +55,7 @@ test:do_catchsql_test(
         INSERT INTO t1 VALUES(6,7, 2);
     ]], {
         -- <check-1.3>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T1': x<5"
+        1, "Check constraint failed 'ck_unnamed_T1_1': x<5"
         -- </check-1.3>
     })
 
@@ -75,7 +75,7 @@ test:do_catchsql_test(
         INSERT INTO t1 VALUES(4,3, 2);
     ]], {
         -- <check-1.5>
-        1, "Check constraint failed 'CK_CONSTRAINT_2_T1': y>x"
+        1, "Check constraint failed 'ck_unnamed_T1_2': y>x"
         -- </check-1.5>
     })
 
@@ -147,7 +147,7 @@ test:do_catchsql_test(
         UPDATE t1 SET x=7 WHERE x==2
     ]], {
         -- <check-1.12>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T1': x<5"
+        1, "Check constraint failed 'ck_unnamed_T1_1': x<5"
         -- </check-1.12>
     })
 
@@ -167,7 +167,7 @@ test:do_catchsql_test(
         UPDATE t1 SET x=5 WHERE x==2
     ]], {
         -- <check-1.14>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T1': x<5"
+        1, "Check constraint failed 'ck_unnamed_T1_1': x<5"
         -- </check-1.14>
     })
 
@@ -319,7 +319,7 @@ test:do_catchsql_test(
         );
     ]], {
         -- <check-3.1>
-        1, "Failed to create check constraint 'CK_CONSTRAINT_1_T3': Subqueries are prohibited in a ck constraint definition"
+        1, "Failed to create check constraint 'ck_unnamed_T3_1': Subqueries are prohibited in a ck constraint definition"
         -- </check-3.1>
     })
 
@@ -344,7 +344,7 @@ test:do_catchsql_test(
         );
     ]], {
         -- <check-3.3>
-        1, "Failed to create check constraint 'CK_CONSTRAINT_1_T3': Can’t resolve field 'Q'"
+        1, "Failed to create check constraint 'ck_unnamed_T3_1': Can’t resolve field 'Q'"
         -- </check-3.3>
     })
 
@@ -368,7 +368,7 @@ test:do_catchsql_test(
         );
     ]], {
         -- <check-3.5>
-        1, "Failed to create check constraint 'CK_CONSTRAINT_1_T3': Field 'X' was not found in space 'T2' format"
+        1, "Failed to create check constraint 'ck_unnamed_T3_1': Field 'X' was not found in space 'T2' format"
         -- </check-3.5>
     })
 
@@ -413,7 +413,7 @@ test:do_catchsql_test(
         INSERT INTO t3 VALUES(111,222,333);
     ]], {
         -- <check-3.9>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T3': t3.x<25"
+        1, "Check constraint failed 'ck_unnamed_T3_1': t3.x<25"
         -- </check-3.9>
     })
 
@@ -484,7 +484,7 @@ test:do_catchsql_test(
         UPDATE t4 SET x=0, y=1;
     ]], {
         -- <check-4.6>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T4': x+y==11 OR x*y==12 OR x/y BETWEEN 5 AND 8 OR -x==y+10"
+        1, "Check constraint failed 'ck_unnamed_T4_1': x+y==11 OR x*y==12 OR x/y BETWEEN 5 AND 8 OR -x==y+10"
         -- </check-4.6>
     })
 
@@ -504,7 +504,7 @@ test:do_catchsql_test(
         UPDATE t4 SET x=0, y=2;
     ]], {
         -- <check-4.9>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T4': x+y==11 OR x*y==12 OR x/y BETWEEN 5 AND 8 OR -x==y+10"
+        1, "Check constraint failed 'ck_unnamed_T4_1': x+y==11 OR x*y==12 OR x/y BETWEEN 5 AND 8 OR -x==y+10"
         -- </check-4.9>
     })
 
@@ -516,7 +516,7 @@ test:do_catchsql_test(
         );
     ]], {
         -- <check-5.1>
-        1, "Failed to create check constraint 'CK_CONSTRAINT_1_T5': bindings are not allowed in DDL"
+        1, "Failed to create check constraint 'ck_unnamed_T5_1': bindings are not allowed in DDL"
         -- </check-5.1>
     })
 
@@ -528,7 +528,7 @@ test:do_catchsql_test(
         );
     ]], {
         -- <check-5.2>
-        1, "Failed to create check constraint 'CK_CONSTRAINT_1_T5': bindings are not allowed in DDL"
+        1, "Failed to create check constraint 'ck_unnamed_T5_1': bindings are not allowed in DDL"
         -- </check-5.2>
     })
 
@@ -581,7 +581,7 @@ test:do_catchsql_test(
         UPDATE OR FAIL t1 SET x=7-x, y=y+1;
     ]], {
         -- <check-6.5>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T1': x<5"
+        1, "Check constraint failed 'ck_unnamed_T1_1': x<5"
         -- </check-6.5>
     })
 
@@ -603,7 +603,7 @@ test:do_catchsql_test(
         INSERT OR ROLLBACK INTO t1 VALUES(8,40.0, 10);
     ]], {
         -- <check-6.7>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T1': x<5"
+        1, "Check constraint failed 'ck_unnamed_T1_1': x<5"
         -- </check-6.7>
     })
 
@@ -636,7 +636,7 @@ test:do_catchsql_test(
         REPLACE INTO t1 VALUES(6,7, 11);
     ]], {
         -- <check-6.12>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T1': x<5"
+        1, "Check constraint failed 'ck_unnamed_T1_1': x<5"
         -- </check-6.12>
     })
 
@@ -700,7 +700,7 @@ test:do_catchsql_test(
     7.3,
     " INSERT INTO t6 VALUES(11) ", {
         -- <7.3>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T6': myfunc(a)"
+        1, "Check constraint failed 'ck_unnamed_T6_1': myfunc(a)"
         -- </7.3>
     })
 
diff --git a/test/sql-tap/fkey1.test.lua b/test/sql-tap/fkey1.test.lua
index 7000ec0c4..d6ef473aa 100755
--- a/test/sql-tap/fkey1.test.lua
+++ b/test/sql-tap/fkey1.test.lua
@@ -217,7 +217,7 @@ test:do_catchsql_test(
         CREATE TABLE c1(a INT PRIMARY KEY REFERENCES p1(x));
     ]], {
         -- <fkey1-6.1>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_C1': referenced fields don't compose unique index"
+        1, "Failed to create foreign key constraint 'fk_unnamed_C1_1': referenced fields don't compose unique index"
         -- </fkey1-6.1>
     })
 
diff --git a/test/sql-tap/fkey2.test.lua b/test/sql-tap/fkey2.test.lua
index c44f1bf7f..264616d75 100755
--- a/test/sql-tap/fkey2.test.lua
+++ b/test/sql-tap/fkey2.test.lua
@@ -362,7 +362,7 @@ test:do_catchsql_test(
         UPDATE ab SET a = 5;
     ]], {
         -- <fkey2-3.2>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_EF': e!=5"
+        1, "Check constraint failed 'ck_unnamed_EF_1': e!=5"
         -- </fkey2-3.2>
     })
 
@@ -382,7 +382,7 @@ test:do_catchsql_test(
         UPDATE ab SET a = 5;
     ]], {
         -- <fkey2-3.4>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_EF': e!=5"
+        1, "Check constraint failed 'ck_unnamed_EF_1': e!=5"
         -- </fkey2-3.4>
     })
 
@@ -722,7 +722,7 @@ test:do_catchsql_test(
         CREATE TABLE c(x INT PRIMARY KEY REFERENCES p(c));
     ]], {
         -- <fkey2-7.1>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_C': foreign key refers to nonexistent field C"
+        1, "Failed to create foreign key constraint 'fk_unnamed_C_1': foreign key refers to nonexistent field C"
         -- </fkey2-7.1>
     })
 
@@ -733,7 +733,7 @@ test:do_catchsql_test(
         CREATE TABLE c(x  INT PRIMARY KEY REFERENCES v(y));
     ]], {
         -- <fkey2-7.2>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_C': referenced space can't be VIEW"
+        1, "Failed to create foreign key constraint 'fk_unnamed_C_1': referenced space can't be VIEW"
         -- </fkey2-7.2>
     })
 
@@ -748,7 +748,7 @@ test:do_catchsql_test(
         CREATE TABLE c(x TEXT PRIMARY KEY REFERENCES p(a));
     ]], {
         -- <fkey2-7.3>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_C': field collation mismatch"
+        1, "Failed to create foreign key constraint 'fk_unnamed_C_1': field collation mismatch"
         -- </fkey2-7.3>
     })
 
@@ -761,7 +761,7 @@ test:do_catchsql_test(
         CREATE TABLE c(x INT PRIMARY KEY REFERENCES p);
     ]], {
         -- <fkey2-7.4>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_C': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
+        1, "Failed to create foreign key constraint 'fk_unnamed_C_1': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
         -- </fkey2-7.4>
     })
 
@@ -1100,7 +1100,7 @@ test:do_catchsql_test(
         CREATE TABLE cc(a  INT PRIMARY KEY, b INT , FOREIGN KEY(a, b) REFERENCES pp(x, z));
     ]], {
         -- <fkey2-10.14>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_CC': foreign key refers to nonexistent field Z"
+        1, "Failed to create foreign key constraint 'fk_unnamed_CC_1': foreign key refers to nonexistent field Z"
         -- </fkey2-10.14>
     })
 
@@ -1162,7 +1162,7 @@ test:do_catchsql_test(
         CREATE TABLE t1(x INT PRIMARY KEY REFERENCES v);
     ]], {
         -- <fkey2-10.20>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_T1': referenced space can't be VIEW"
+        1, "Failed to create foreign key constraint 'fk_unnamed_T1_1': referenced space can't be VIEW"
         -- </fkey2-10.20>
     })
 
diff --git a/test/sql-tap/sql-errors.test.lua b/test/sql-tap/sql-errors.test.lua
index f452e3c6d..d11c5c597 100755
--- a/test/sql-tap/sql-errors.test.lua
+++ b/test/sql-tap/sql-errors.test.lua
@@ -112,7 +112,7 @@ test:do_catchsql_test(
 		CREATE TABLE t10(i INT PRIMARY KEY REFERENCES v0);
 	]], {
 		-- <sql-errors-1.10>
-		1,"Failed to create foreign key constraint 'FK_CONSTRAINT_1_T10': referenced space can't be VIEW"
+		1,"Failed to create foreign key constraint 'fk_unnamed_T10_1': referenced space can't be VIEW"
 		-- </sql-errors-1.10>
 	})
 
@@ -313,7 +313,7 @@ test:do_catchsql_test(
 		CREATE TABLE t27 (i INT PRIMARY KEY, CHECK(i < (SELECT * FROM t0)));
 	]], {
 		-- <sql-errors-1.27>
-		1,"Failed to create check constraint 'CK_CONSTRAINT_1_T27': Subqueries are prohibited in a ck constraint definition"
+		1,"Failed to create check constraint 'ck_unnamed_T27_1': Subqueries are prohibited in a ck constraint definition"
 		-- </sql-errors-1.27>
 	})
 
diff --git a/test/sql-tap/table.test.lua b/test/sql-tap/table.test.lua
index 40d7d6b7e..55187378b 100755
--- a/test/sql-tap/table.test.lua
+++ b/test/sql-tap/table.test.lua
@@ -791,7 +791,7 @@ test:do_catchsql_test(
         );
     ]], {
         -- <table-10.7>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_T6': foreign key refers to nonexistent field B"
+        1, "Failed to create foreign key constraint 'fk_unnamed_T6_1': foreign key refers to nonexistent field B"
         -- </table-10.7>
     })
 
@@ -820,7 +820,7 @@ test:do_catchsql_test(
         );
     ]], {
         -- <table-10.9>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_T6': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
+        1, "Failed to create foreign key constraint 'fk_unnamed_T6_1': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
         -- </table-10.9>
     })
 
@@ -835,7 +835,7 @@ test:do_test(
         ]]
     end, {
         -- <table-10.10>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_T6': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
+        1, "Failed to create foreign key constraint 'fk_unnamed_T6_1': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
         -- </table-10.10>
     })
 
@@ -848,7 +848,7 @@ test:do_test(
         ]]
     end, {
         -- <table-10.11>
-        1, "Failed to create foreign key constraint 'FK_CONSTRAINT_1_T6': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
+        1, "Failed to create foreign key constraint 'fk_unnamed_T6_1': number of columns in foreign key does not match the number of columns in the primary index of referenced table"
         -- </table-10.11>
     })
 
@@ -863,7 +863,7 @@ test:do_test(
         ]]
     end, {
         -- <table-10.12>
-        1, [[Failed to create foreign key constraint 'FK_CONSTRAINT_1_T6': unknown column X in foreign key definition]]
+        1, [[Failed to create foreign key constraint 'fk_unnamed_T6_1': unknown column X in foreign key definition]]
         -- </table-10.12>
     })
 
@@ -878,7 +878,7 @@ test:do_test(
         ]]
     end, {
         -- <table-10.13>
-        1, [[Failed to create foreign key constraint 'FK_CONSTRAINT_1_T6': unknown column X in foreign key definition]]
+        1, [[Failed to create foreign key constraint 'fk_unnamed_T6_1': unknown column X in foreign key definition]]
         -- </table-10.13>
     })
 
@@ -1221,7 +1221,7 @@ test:do_catchsql_test(
         INSERT INTO T21 VALUES(2, -1, 1);
     ]], {
         -- <table-21.3>
-        1, "Check constraint failed 'CK_CONSTRAINT_1_T21': B > 0"
+        1, "Check constraint failed 'ck_unnamed_T21_1': B > 0"
         -- </table-21.3>
     })
 
@@ -1231,7 +1231,7 @@ test:do_catchsql_test(
         INSERT INTO T21 VALUES(2, 1, -1);
     ]], {
         -- <table-21.4>
-        1, "Check constraint failed 'CK_CONSTRAINT_2_T21': C > 0"
+        1, "Check constraint failed 'ck_unnamed_T21_2': C > 0"
         -- </table-21.4>
     })
 
diff --git a/test/sql/checks.result b/test/sql/checks.result
index b20514993..4fdf23414 100644
--- a/test/sql/checks.result
+++ b/test/sql/checks.result
@@ -264,8 +264,8 @@ box.space._ck_constraint:select()
 --
 box.execute("CREATE TABLE w2 (s1 INT PRIMARY KEY, CHECK ((SELECT COUNT(*) FROM w2) = 0));")
 ---
-- error: 'Failed to create check constraint ''CK_CONSTRAINT_1_W2'': Subqueries are
-    prohibited in a ck constraint definition'
+- error: 'Failed to create check constraint ''ck_unnamed_W2_1'': Subqueries are prohibited
+    in a ck constraint definition'
 ...
 box.execute("DROP TABLE w2;")
 ---
@@ -276,7 +276,7 @@ box.execute("DROP TABLE w2;")
 --
 box.execute("CREATE TABLE t5(x INT PRIMARY KEY, y INT, CHECK( x*y < ? ));")
 ---
-- error: 'Failed to create check constraint ''CK_CONSTRAINT_1_T5'': bindings are not
+- error: 'Failed to create check constraint ''ck_unnamed_T5_1'': bindings are not
     allowed in DDL'
 ...
 -- Test trim CK constraint code correctness.
@@ -290,11 +290,11 @@ box.space._ck_constraint:select()[1].code
 ...
 box.execute("INSERT INTO t1 VALUES('1 a')")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T1'': x LIKE ''1  a'''
+- error: 'Check constraint failed ''ck_unnamed_T1_1'': x LIKE ''1  a'''
 ...
 box.execute("INSERT INTO t1 VALUES('1   a')")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T1'': x LIKE ''1  a'''
+- error: 'Check constraint failed ''ck_unnamed_T1_1'': x LIKE ''1  a'''
 ...
 box.execute("INSERT INTO t1 VALUES('1  a')")
 ---
diff --git a/test/sql/drop-table.result b/test/sql/drop-table.result
index 48b0010ac..cf19f0364 100644
--- a/test/sql/drop-table.result
+++ b/test/sql/drop-table.result
@@ -180,7 +180,7 @@ box.execute('CREATE TABLE t3(a INTEGER PRIMARY KEY);')
 --
 box.execute('CREATE TABLE t4(x INTEGER PRIMARY KEY REFERENCES t3, a INT UNIQUE, c TEXT REFERENCES t3);')
 ---
-- error: 'Failed to create foreign key constraint ''FK_CONSTRAINT_2_T4'': field type
+- error: 'Failed to create foreign key constraint ''fk_unnamed_T4_2'': field type
     mismatch'
 ...
 box.execute('DROP TABLE t3;')
diff --git a/test/sql/foreign-keys.result b/test/sql/foreign-keys.result
index f3cca5c80..e5acfedca 100644
--- a/test/sql/foreign-keys.result
+++ b/test/sql/foreign-keys.result
@@ -364,8 +364,7 @@ box.execute('SELECT row_count();')
 box.space._fk_constraint:select{}
 ---
 - - ['FK1', 518, 517, false, 'partial', 'cascade', 'set_null', [0], [0]]
-  - ['FK_CONSTRAINT_1_TC', 518, 517, false, 'full', 'set_null', 'no_action', [1],
-    [1]]
+  - ['fk_unnamed_TC_1', 518, 517, false, 'full', 'set_null', 'no_action', [1], [1]]
 ...
 box.execute('ALTER TABLE tc DROP CONSTRAINT fk1;')
 ---
diff --git a/test/sql/gh-2981-check-autoinc.result b/test/sql/gh-2981-check-autoinc.result
index f03858a7c..7073c5059 100644
--- a/test/sql/gh-2981-check-autoinc.result
+++ b/test/sql/gh-2981-check-autoinc.result
@@ -29,7 +29,7 @@ box.execute("insert into t1 values (18, null);")
 ...
 box.execute("insert into t1(s2) values (null);")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T1'': s1 <> 19'
+- error: 'Check constraint failed ''ck_unnamed_T1_1'': s1 <> 19'
 ...
 box.execute("insert into t2 values (18, null);")
 ---
@@ -37,7 +37,7 @@ box.execute("insert into t2 values (18, null);")
 ...
 box.execute("insert into t2(s2) values (null);")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T2'': s1 <> 19 AND s1 <> 25'
+- error: 'Check constraint failed ''ck_unnamed_T2_1'': s1 <> 19 AND s1 <> 25'
 ...
 box.execute("insert into t2 values (24, null);")
 ---
@@ -45,7 +45,7 @@ box.execute("insert into t2 values (24, null);")
 ...
 box.execute("insert into t2(s2) values (null);")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T2'': s1 <> 19 AND s1 <> 25'
+- error: 'Check constraint failed ''ck_unnamed_T2_1'': s1 <> 19 AND s1 <> 25'
 ...
 box.execute("insert into t3 values (9, null)")
 ---
@@ -53,7 +53,7 @@ box.execute("insert into t3 values (9, null)")
 ...
 box.execute("insert into t3(s2) values (null)")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T3'': s1 < 10'
+- error: 'Check constraint failed ''ck_unnamed_T3_1'': s1 < 10'
 ...
 box.execute("DROP TABLE t1")
 ---
diff --git a/test/sql/types.result b/test/sql/types.result
index 780a23d51..c4a979ea6 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -744,7 +744,7 @@ box.execute("CREATE TABLE t1 (id INT PRIMARY KEY, a BOOLEAN CHECK (a = true));")
 ...
 box.execute("INSERT INTO t1 VALUES (1, false);")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T1'': a = true'
+- error: 'Check constraint failed ''ck_unnamed_T1_1'': a = true'
 ...
 box.execute("INSERT INTO t1 VALUES (1, true);")
 ---
@@ -1595,11 +1595,11 @@ box.execute("CREATE TABLE t1 (id INT PRIMARY KEY, a INT CHECK (a > 1844674407370
 ...
 box.execute("INSERT INTO t1 VALUES (1, 18446744073709551611);")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T1'': a > 18446744073709551612'
+- error: 'Check constraint failed ''ck_unnamed_T1_1'': a > 18446744073709551612'
 ...
 box.execute("INSERT INTO t1 VALUES (1, -1);")
 ---
-- error: 'Check constraint failed ''CK_CONSTRAINT_1_T1'': a > 18446744073709551612'
+- error: 'Check constraint failed ''ck_unnamed_T1_1'': a > 18446744073709551612'
 ...
 box.space.T1:drop()
 ---
-- 
2.15.1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tarantool-patches] Re: [PATCH] sql: make default constraint names be consistent
  2019-07-31 10:55 [tarantool-patches] [PATCH] sql: make default constraint names be consistent Nikita Pettik
@ 2019-08-01 16:42 ` n.pettik
  2019-08-02 11:01 ` Kirill Yukhin
  1 sibling, 0 replies; 3+ messages in thread
From: n.pettik @ 2019-08-01 16:42 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Konstantin Osipov

I’ve added another one patch on branch according to
my message to you and Peter G.:

From 929aefb794f3604681b875ac5051dfd96a9280a3 Mon Sep 17 00:00:00 2001
From: Nikita Pettik <korablev@tarantool.org>
Date: Thu, 1 Aug 2019 19:31:52 +0300
Subject: [PATCH] sql: don't mangle name of unique constraint

If UNIQUE constraint is specified in CREATE TABLE statement and it has
given name, one is mangled with pattern "unique_%s_%d", where %s is
original name of constraint and %d - current iid. For instance:

CREATE TABLE t (id INT PRIMARY KEY, a INT CONSTRAINT i1 UNIQUE);

This statement results in secondary index creation with name
"unique_I1_1". Justification for mangling is that constraint namespace
should be independent from index namespace. However, ALTER TABLE ADD
CONSTRAINT UNIQUE which is alias to CREATE INDEX doesn't mangle name.
What is more, users may wonder why name of index is different from
name of created constraint. Hence, it has been decided to remove this
mangling and create index exactly with specified constraint's name.
---
 src/box/sql/build.c          | 23 +++++++++--------------
 test/sql-tap/index7.test.lua |  4 ++--
 test/sql-tap/table.test.lua  |  4 ++--
 3 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index e02b7f864..27bea1756 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -2496,22 +2496,17 @@ sql_create_index(struct Parse *parse) {
 		*/
 		assert(idx_type == SQL_INDEX_TYPE_CONSTRAINT_UNIQUE ||
 		       idx_type == SQL_INDEX_TYPE_CONSTRAINT_PK);
-		const char *prefix = NULL;
-		if (idx_type == SQL_INDEX_TYPE_CONSTRAINT_UNIQUE) {
-			prefix = constraint_name == NULL ?
-				"unique_unnamed_%s_%d" : "unique_%s_%d";
-		} else {
-			prefix = constraint_name == NULL ?
-				"pk_unnamed_%s_%d" : "pk_%s_%d";
-		}
 		uint32_t idx_count = space->index_count;
-		if (constraint_name == NULL ||
-		    strcmp(constraint_name, "") == 0) {
-			name = sqlMPrintf(db, prefix, def->name,
-					      idx_count + 1);
+		if (constraint_name == NULL) {
+			if (idx_type == SQL_INDEX_TYPE_CONSTRAINT_UNIQUE) {
+				name = sqlMPrintf(db, "unique_unnamed_%s_%d",
+						  def->name, idx_count + 1);
+			} else {
+				name = sqlMPrintf(db, "pk_unnamed_%s_%d",
+						  def->name, idx_count + 1);
+			}
 		} else {
-			name = sqlMPrintf(db, prefix,
-					      constraint_name, idx_count + 1);
+			name = sqlDbStrDup(db, constraint_name);
 		}
 		sqlDbFree(db, constraint_name);
 	}
diff --git a/test/sql-tap/index7.test.lua b/test/sql-tap/index7.test.lua
index ed2b17c74..ca5cb1910 100755
--- a/test/sql-tap/index7.test.lua
+++ b/test/sql-tap/index7.test.lua
@@ -367,7 +367,7 @@ test:do_catchsql_test(
                 "_index"."id" = "_space"."id" AND
                 "_space"."name"='TEST6';
         ]],
-        {0, {"pk_unnamed_TEST6_1",0,"unique_C1_2",1}})
+        {0, {"pk_unnamed_TEST6_1",0,"C1",1}})
 
 -- This test checks that CREATE TABLE statement with PK constraint
 -- and UNIQUE constraint is executed correctly
@@ -397,6 +397,6 @@ test:do_catchsql_test(
                 "_index"."id" = "_space"."id" AND
                 "_space"."name"='TEST8';
         ]],
-        {0, {"pk_unnamed_TEST8_2",0,"unique_C1_1",1}})
+        {0, {"pk_unnamed_TEST8_2",0,"C1",1}})
 
 test:finish_test()
diff --git a/test/sql-tap/table.test.lua b/test/sql-tap/table.test.lua
index 55187378b..2e986c59a 100755
--- a/test/sql-tap/table.test.lua
+++ b/test/sql-tap/table.test.lua
@@ -1283,7 +1283,7 @@ test:do_catchsql_test(
         INSERT INTO T22 VALUES(2, 1, 1);
     ]], {
         -- <table-22.3>
-        1,"Duplicate key exists in unique index 'unique_ONE_2' in space 'T22'"
+        1,"Duplicate key exists in unique index 'ONE' in space 'T22'"
         -- </table-22.3>
     })
 
@@ -1308,7 +1308,7 @@ test:do_catchsql_test(
         INSERT INTO T24 VALUES(2, 1, 1);
     ]], {
         -- <table-22.5>
-        1, "Duplicate key exists in unique index 'unique_TWO_2' in space 'T24'"
+        1, "Duplicate key exists in unique index 'TWO' in space 'T24'"
         -- </table-22.5>
     })
 
-- 
2.15.1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tarantool-patches] Re: [PATCH] sql: make default constraint names be consistent
  2019-07-31 10:55 [tarantool-patches] [PATCH] sql: make default constraint names be consistent Nikita Pettik
  2019-08-01 16:42 ` [tarantool-patches] " n.pettik
@ 2019-08-02 11:01 ` Kirill Yukhin
  1 sibling, 0 replies; 3+ messages in thread
From: Kirill Yukhin @ 2019-08-02 11:01 UTC (permalink / raw)
  To: tarantool-patches; +Cc: kostja, Nikita Pettik

Hello,

On 31 Jul 13:55, Nikita Pettik wrote:
> If during creation of constraint its name is not specified, then it is
> generated automatically. Occasionally, default names for each type of
> constraint turn out to be different. This patch makes them follow the
> same pattern: "shortcut"_unnamed_"table_name"_"ordinal_numb". For
> instance: fk_unnamed_T1_1 or ck_unnamed_T1_3 etc
> ---
> 
> Branch: https://github.com/tarantool/tarantool/commits/np/fix-constraint-names

I've checked the patchset into master.

--
Regards, Kirill Yukhin

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-02 11:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-31 10:55 [tarantool-patches] [PATCH] sql: make default constraint names be consistent Nikita Pettik
2019-08-01 16:42 ` [tarantool-patches] " n.pettik
2019-08-02 11:01 ` Kirill Yukhin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox