Tarantool development patches archive
 help / color / mirror / Atom feed
From: "n.pettik" <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: Konstantin Osipov <kostja@tarantool.org>
Subject: [tarantool-patches] Re: [PATCH] sql: make default constraint names be consistent
Date: Thu, 1 Aug 2019 19:42:31 +0300	[thread overview]
Message-ID: <929F658A-E4E9-4D8A-BD7C-65F42A62AE89@tarantool.org> (raw)
In-Reply-To: <20190731105556.42786-1-korablev@tarantool.org>

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

  reply	other threads:[~2019-08-01 16:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-31 10:55 [tarantool-patches] " Nikita Pettik
2019-08-01 16:42 ` n.pettik [this message]
2019-08-02 11:01 ` [tarantool-patches] " Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=929F658A-E4E9-4D8A-BD7C-65F42A62AE89@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='[tarantool-patches] Re: [PATCH] sql: make default constraint names be consistent' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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