From a23df4a680e4d66e33c7bae9ab83d1b8a881f2e6 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Wed, 10 Oct 2012 18:31:33 +0400
Subject: [PATCH] Provide a more verbose error when violating a unique key.

---
 include/errcode.h       |  2 +-
 mod/box/space.m         |  3 ++-
 test/box_big/lua.result | 14 +++++++++++++-
 test/box_big/lua.test   |  6 +++++-
 4 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/include/errcode.h b/include/errcode.h
index 6e368f5077..72ae7d9420 100644
--- a/include/errcode.h
+++ b/include/errcode.h
@@ -108,7 +108,7 @@ enum { TNT_ERRMSG_MAX = 512 };
 	/* 53 */_(ER_NO_SUCH_INDEX,		2, "No index #%u is defined in space %u") \
 	/* 54 */_(ER_NO_SUCH_FIELD,		2, "Field %u was not found in the tuple") \
 	/* 55 */_(ER_TUPLE_FOUND,		2, "Tuple already exists") \
-	/* 56 */_(ER_INDEX_VIOLATION,		2, "Duplicate key exists in a unique index") \
+	/* 56 */_(ER_INDEX_VIOLATION,		2, "Duplicate key exists in unique index %d") \
 	/* 57 */_(ER_NO_SUCH_SPACE,		2, "Space %u does not exist")
 
 
diff --git a/mod/box/space.m b/mod/box/space.m
index d5fbcbf51b..048bb2dbf1 100644
--- a/mod/box/space.m
+++ b/mod/box/space.m
@@ -175,7 +175,8 @@ space_validate(struct space *sp, struct tuple *old_tuple,
 		if (index->key_def->is_unique) {
 			struct tuple *tuple = [index findByTuple: new_tuple];
 			if (tuple != NULL && tuple != old_tuple)
-				tnt_raise(ClientError, :ER_INDEX_VIOLATION);
+				tnt_raise(ClientError, :ER_INDEX_VIOLATION,
+					  index_n(index));
 		}
 	}
 }
diff --git a/test/box_big/lua.result b/test/box_big/lua.result
index cf8393e7b0..6698c0b0fd 100644
--- a/test/box_big/lua.result
+++ b/test/box_big/lua.result
@@ -533,7 +533,7 @@ lua push_collection(5, 1038784, 'hey')
 ---
  - 1038784: {26984, 'hey', 'hey', 'hey', 'hey'}
 ...
-A test case for Bug#1060967: truncation of 64-bit numbers
+# A test case for Bug#1060967: truncation of 64-bit numbers
 lua box.space[5]:insert(2^51, 'hello', 'world')
 ---
  - 2251799813685248: {'hello', 'world'}
@@ -545,3 +545,15 @@ lua box.space[5]:select(0, 2^51)
 lua box.space[5]:truncate()
 ---
 ...
+# Test that we print index number in error ER_INDEX_VIOLATION
+lua box.space[1]:insert(1, 'hello', 'world')
+---
+ - 1: {'hello', 'world'}
+...
+lua box.space[1]:insert(2, 'hello', 'world')
+---
+error: 'Duplicate key exists in unique index 1'
+...
+lua box.space[1]:truncate()
+---
+...
diff --git a/test/box_big/lua.test b/test/box_big/lua.test
index e57494235f..4ccc898075 100644
--- a/test/box_big/lua.test
+++ b/test/box_big/lua.test
@@ -197,7 +197,11 @@ exec admin "lua push_collection(5, 1038784, 'hey')"
 exec admin "lua push_collection(5, 1038784, 'hey')"
 exec admin "lua push_collection(5, 1038784, 'hey')"
 
-print """A test case for Bug#1060967: truncation of 64-bit numbers"""
+print """# A test case for Bug#1060967: truncation of 64-bit numbers"""
 exec admin "lua box.space[5]:insert(2^51, 'hello', 'world')"
 exec admin "lua box.space[5]:select(0, 2^51)"
 exec admin "lua box.space[5]:truncate()"
+print """# Test that we print index number in error ER_INDEX_VIOLATION"""
+exec admin "lua box.space[1]:insert(1, 'hello', 'world')"
+exec admin "lua box.space[1]:insert(2, 'hello', 'world')"
+exec admin "lua box.space[1]:truncate()"
-- 
GitLab