[tarantool-patches] Re: [PATCH] sql: make default constraint names be consistent

n.pettik korablev at tarantool.org
Thu Aug 1 19:42:31 MSK 2019


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 at 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






More information about the Tarantool-patches mailing list