Skip to content

Commit 5272887

Browse files
SQL constraint changes (#3707)
1 parent 30c9b3c commit 5272887

File tree

4 files changed

+392
-81
lines changed

4 files changed

+392
-81
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
g.before_each(function(cg)
6+
cg.server = server:new {
7+
box_cfg = {},
8+
workdir = fio.cwd() .. '/tmp'
9+
}
10+
cg.server:start()
11+
end)
12+
13+
g.after_each(function(cg)
14+
cg.server:stop()
15+
cg.server:drop()
16+
end)
17+
18+
g.test_space_is_updated = function(cg)
19+
cg.server:exec(function()
20+
box.execute([[
21+
-- create_author_table_start
22+
CREATE TABLE author (
23+
id INTEGER PRIMARY KEY,
24+
name STRING,
25+
CONSTRAINT check_name_length CHECK (CHAR_LENGTH(name) > 4)
26+
);
27+
-- create_author_table_end
28+
]])
29+
box.execute([[
30+
-- insert_authors_start
31+
INSERT INTO author VALUES (1, 'Leo Tolstoy'),
32+
(2, 'Fyodor Dostoevsky');
33+
-- insert_authors_end
34+
]])
35+
local _, insert_author_err = box.execute([[
36+
-- insert_short_name_start
37+
INSERT INTO author VALUES (3, 'Alex');
38+
/*
39+
- Check constraint 'CHECK_NAME_LENGTH' failed for tuple
40+
*/
41+
-- insert_short_name_end
42+
]])
43+
44+
-- Tests
45+
t.assert_equals(insert_author_err:unpack().message, "Check constraint 'CHECK_NAME_LENGTH' failed for tuple")
46+
end)
47+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
g.before_each(function(cg)
6+
cg.server = server:new {
7+
box_cfg = {},
8+
workdir = fio.cwd() .. '/tmp'
9+
}
10+
cg.server:start()
11+
end)
12+
13+
g.after_each(function(cg)
14+
cg.server:stop()
15+
cg.server:drop()
16+
end)
17+
18+
g.test_space_is_updated = function(cg)
19+
cg.server:exec(function()
20+
box.execute([[
21+
-- create_author_table_start
22+
CREATE TABLE author (
23+
id INTEGER PRIMARY KEY,
24+
name STRING NOT NULL
25+
);
26+
-- create_author_table_end
27+
]])
28+
box.execute([[
29+
-- insert_authors_start
30+
INSERT INTO author VALUES (1, 'Leo Tolstoy'),
31+
(2, 'Fyodor Dostoevsky');
32+
-- insert_authors_end
33+
]])
34+
local _, insert_author_err = box.execute([[
35+
-- insert_duplicate_author_start
36+
INSERT INTO author VALUES (2, 'Alexander Pushkin');
37+
/*
38+
- Duplicate key exists in unique index "pk_unnamed_AUTHOR_1" in space "AUTHOR" with
39+
old tuple - [2, "Fyodor Dostoevsky"] and new tuple - [2, "Alexander Pushkin"]
40+
*/
41+
-- insert_duplicate_author_end
42+
]])
43+
box.execute([[
44+
-- create_book_table_start
45+
CREATE TABLE book (
46+
id INTEGER,
47+
title STRING NOT NULL,
48+
PRIMARY KEY (id, title)
49+
);
50+
-- create_book_table_end
51+
]])
52+
box.execute([[
53+
-- insert_books_start
54+
INSERT INTO book VALUES (1, 'War and Peace'),
55+
(2, 'Crime and Punishment');
56+
-- insert_books_end
57+
]])
58+
local _, insert_book_err = box.execute([[
59+
-- insert_duplicate_book_start
60+
INSERT INTO book VALUES (2, 'Crime and Punishment');
61+
/*
62+
- Duplicate key exists in unique index "pk_unnamed_BOOK_1" in space "BOOK" with old
63+
tuple - [2, "Crime and Punishment"] and new tuple - [2, "Crime and Punishment"]
64+
*/
65+
-- insert_duplicate__book_end
66+
]])
67+
68+
-- Tests
69+
t.assert_equals(insert_author_err:unpack().message, "Duplicate key exists in unique index \"pk_unnamed_AUTHOR_1\" in space \"AUTHOR\" with old tuple - [2, \"Fyodor Dostoevsky\"] and new tuple - [2, \"Alexander Pushkin\"]")
70+
t.assert_equals(insert_book_err:unpack().message, "Duplicate key exists in unique index \"pk_unnamed_BOOK_1\" in space \"BOOK\" with old tuple - [2, \"Crime and Punishment\"] and new tuple - [2, \"Crime and Punishment\"]")
71+
end)
72+
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
g.before_each(function(cg)
6+
cg.server = server:new {
7+
box_cfg = {},
8+
workdir = fio.cwd() .. '/tmp'
9+
}
10+
cg.server:start()
11+
end)
12+
13+
g.after_each(function(cg)
14+
cg.server:stop()
15+
cg.server:drop()
16+
end)
17+
18+
g.test_space_is_updated = function(cg)
19+
cg.server:exec(function()
20+
box.execute([[
21+
-- create_author_table_start
22+
CREATE TABLE author (
23+
id INTEGER PRIMARY KEY,
24+
name STRING UNIQUE
25+
);
26+
-- create_author_table_end
27+
]])
28+
box.execute([[
29+
-- insert_authors_start
30+
INSERT INTO author VALUES (1, 'Leo Tolstoy'),
31+
(2, 'Fyodor Dostoevsky');
32+
-- insert_authors_end
33+
]])
34+
local _, insert_author_err = box.execute([[
35+
-- insert_duplicate_author_start
36+
INSERT INTO author VALUES (3, 'Leo Tolstoy');
37+
/*
38+
- Duplicate key exists in unique index "unique_unnamed_AUTHOR_2" in space "AUTHOR"
39+
with old tuple - [1, "Leo Tolstoy"] and new tuple - [3, "Leo Tolstoy"]
40+
*/
41+
-- insert_duplicate_author_end
42+
]])
43+
box.execute([[
44+
-- create_book_table_start
45+
CREATE TABLE book (
46+
id INTEGER PRIMARY KEY,
47+
title STRING NOT NULL,
48+
author_id INTEGER UNIQUE,
49+
UNIQUE (title, author_id)
50+
);
51+
-- create_book_table_end
52+
]])
53+
box.execute([[
54+
-- insert_books_start
55+
INSERT INTO book VALUES (1, 'War and Peace', 1),
56+
(2, 'Crime and Punishment', 2);
57+
-- insert_books_end
58+
]])
59+
local _, insert_book_err = box.execute([[
60+
-- insert_duplicate_book_start
61+
INSERT INTO book VALUES (3, 'War and Peace', 1);
62+
/*
63+
- Duplicate key exists in unique index "unique_unnamed_BOOK_2" in space "BOOK" with
64+
old tuple - [1, "War and Peace", 1] and new tuple - [3, "War and Peace", 1]
65+
*/
66+
-- insert_duplicate__book_end
67+
]])
68+
69+
-- Tests
70+
t.assert_equals(insert_author_err:unpack().message, "Duplicate key exists in unique index \"unique_unnamed_AUTHOR_2\" in space \"AUTHOR\" with old tuple - [1, \"Leo Tolstoy\"] and new tuple - [3, \"Leo Tolstoy\"]")
71+
t.assert_equals(insert_book_err:unpack().message, "Duplicate key exists in unique index \"unique_unnamed_BOOK_2\" in space \"BOOK\" with old tuple - [1, \"War and Peace\", 1] and new tuple - [3, \"War and Peace\", 1]")
72+
end)
73+
end

0 commit comments

Comments
 (0)