File: ddl.test.lua

package info (click to toggle)
tarantool 2.6.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 85,396 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,176; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (329 lines) | stat: -rw-r--r-- 10,090 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
test_run = require('test_run').new()
json = require('json')
engine = test_run:get_cfg('engine')
_ = box.space._session_settings:update('sql_default_engine', {{'=', 2, engine}})

--
-- gh-4086: SQL transactional DDL.
--
test_run:cmd("setopt delimiter ';'")
box.begin()
box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')
box.execute('CREATE TABLE t2(id INTEGER PRIMARY KEY);')
box.commit();
test_run:cmd("setopt delimiter ''");

box.space.T1 ~= nil
box.space.T1.index[0] ~= nil
box.space.T2 ~= nil
box.space.T2.index[0] ~= nil

test_run:cmd("setopt delimiter ';'")
box.begin()
box.execute('DROP TABLE t1;')
assert(box.space.T1 == nil)
assert(box.space.T2 ~= nil)
box.execute('DROP TABLE t2;')
assert(box.space.T2 == nil)
box.commit();
test_run:cmd("setopt delimiter ''");

--
-- Try to build an index transactionally.
--
box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER)')
box.space.T1:replace{1, 1, 1}
box.space.T1:replace{2, 2, 2}
box.space.T1:replace{3, 3, 3}
box.space.T1:replace{4, 4, 4}
box.space.T1:replace{5, 5, 5}
-- Snapshot to dump Vinyl memory level, and force reading from a
-- disk, if someday create index will support transactions.
box.snapshot()

test_run:cmd("setopt delimiter ';'")
box.begin(),
box.execute('CREATE TABLE t2(id INTEGER PRIMARY KEY)') or box.error(),
box.execute('CREATE INDEX t1a ON t1(a)') or box.error(),
box.execute('CREATE INDEX t1b ON t1(b)') or box.error(),
box.commit();
test_run:cmd("setopt delimiter ''");
box.rollback()

--
-- Index drop does not yield, and is being done in background
-- later. So it is transactional.
--
box.execute('CREATE INDEX t1a ON t1(a)')
box.execute('CREATE INDEX t1b ON t1(b)')

test_run:cmd("setopt delimiter ';'")
box.begin()
box.execute('CREATE TABLE t2(id INTEGER PRIMARY KEY)')
box.execute('DROP INDEX t1a ON t1')
box.execute('DROP INDEX t1b ON t1')
box.commit()
test_run:cmd("setopt delimiter ''");

--
-- Truncate should not be different from index drop in terms of
-- yields and atomicity.
--
box.space.T2:replace{1}
test_run:cmd("setopt delimiter ';'")
function truncate_both()
    box.execute('TRUNCATE TABLE t1;')
    box.execute('TRUNCATE TABLE t2;')
end;
test_run:cmd("setopt delimiter ''");

box.begin() truncate_both() box.rollback()

box.space.T1:count() > 0 and box.space.T2:count() > 0

box.begin() truncate_both() box.commit()

box.space.T1:count() == 0 and box.space.T2:count() == 0

--
-- Rename transactionally changes name of the table and its
-- mentioning in trigger bodies.
--
_trigger_index = box.space._trigger.index.space_id
test_run:cmd("setopt delimiter '$'")
box.execute([[CREATE TRIGGER t1t AFTER INSERT ON t1 FOR EACH ROW
              BEGIN
                  INSERT INTO t2 VALUES(1);
              END; ]])$

box.begin()
box.execute('ALTER TABLE t1 RENAME TO t1_new;')
sql = _trigger_index:select(box.space.T1_NEW.id)[1].opts.sql
assert(sql:find('T1_NEW'))
box.rollback()$
test_run:cmd("setopt delimiter ''")$

sql = _trigger_index:select(box.space.T1.id)[1].opts.sql
not sql:find('T1_NEW') and sql:find('t1') ~= nil

box.execute('ALTER TABLE t1 RENAME TO t1_new;')
sql = _trigger_index:select(box.space.T1_NEW.id)[1].opts.sql
sql:find('T1_NEW') ~= nil

box.execute('DROP TABLE t1_new')
box.execute('DROP TABLE t2')

--
-- Use all the possible SQL DDL statements.
--
test_run:cmd("setopt delimiter '$'")
function monster_ddl()
-- Try random errors inside this big batch of DDL to ensure, that
-- they do not affect normal operation.
    local _, err1, err2, err3, err4, err5, err6
    box.execute([[CREATE TABLE t1(id INTEGER PRIMARY KEY,
                                  a INTEGER,
                                  b INTEGER);]])
    box.execute([[CREATE TABLE t2(id INTEGER PRIMARY KEY,
                                  a INTEGER,
                                  b INTEGER UNIQUE,
                                  CONSTRAINT ck1 CHECK(b < 100));]])

    box.execute('CREATE INDEX t1a ON t1(a);')
    box.execute('CREATE INDEX t2a ON t2(a);')

    box.execute('CREATE TABLE t_to_rename(id INTEGER PRIMARY KEY, a INTEGER);')

    box.execute('DROP INDEX t2a ON t2;')

    box.execute('CREATE INDEX t_to_rename_a ON t_to_rename(a);')

    box.execute('ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK(b > 0);')

    _, err1 = box.execute('ALTER TABLE t_to_rename RENAME TO t1;')

    box.execute('ALTER TABLE t1 ADD CONSTRAINT ck2 CHECK(a > 0);')
    box.space.T1.ck_constraint.CK1:drop()

    box.execute([[ALTER TABLE t1 ADD CONSTRAINT fk1 FOREIGN KEY
                  (a) REFERENCES t2(b);]])
    box.execute('ALTER TABLE t1 DROP CONSTRAINT fk1;')

    _, err2 = box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')

    box.execute([[ALTER TABLE t1 ADD CONSTRAINT fk1 FOREIGN KEY
                  (a) REFERENCES t2(b);]])

    box.execute([[CREATE TABLE trigger_catcher(id INTEGER PRIMARY
                                               KEY AUTOINCREMENT);]])

    box.execute('ALTER TABLE t_to_rename RENAME TO t_renamed;')

    box.execute('DROP INDEX t_to_rename_a ON t_renamed;')

    box.execute([[CREATE TRIGGER t1t AFTER INSERT ON t1 FOR EACH ROW
                  BEGIN
                      INSERT INTO trigger_catcher VALUES(1);
                  END; ]])

    _, err3 = box.execute('DROP TABLE t3;')

    box.execute([[CREATE TRIGGER t2t AFTER INSERT ON t2 FOR EACH ROW
                  BEGIN
                      INSERT INTO trigger_catcher VALUES(1);
                  END; ]])

    _, err4 = box.execute('CREATE INDEX t1a ON t1(a, b);')

    box.execute('TRUNCATE TABLE t1;')
    _, err5 = box.execute('TRUNCATE TABLE t2;')
    _, err6 = box.execute('TRUNCATE TABLE t_does_not_exist;')

    box.execute('DROP TRIGGER t2t;')

    return {'Finished ok, errors in the middle: ', err1, err2, err3, err4,
            err5, err6}
end$
function monster_ddl_cmp_res(res1, res2)
    if json.encode(res1) == json.encode(res2) then
        return true
    end
    return res1, res2
end$
function monster_ddl_is_clean()
    assert(box.space.T1 == nil)
    assert(box.space.T2 == nil)
    assert(box.space._trigger:count() == 0)
    assert(box.space._fk_constraint:count() == 0)
    assert(box.space._ck_constraint:count() == 0)
    assert(box.space.T_RENAMED == nil)
    assert(box.space.T_TO_RENAME == nil)
end$
function monster_ddl_check()
    local _, err1, err2, err3, err4, res
    _, err1 = box.execute('INSERT INTO t2 VALUES (1, 1, 101)')
    box.execute('INSERT INTO t2 VALUES (1, 1, 1)')
    _, err2 = box.execute('INSERT INTO t2 VALUES(2, 2, 1)')
    _, err3 = box.execute('INSERT INTO t1 VALUES(1, 20, 1)')
    _, err4 = box.execute('INSERT INTO t1 VALUES(1, -1, 1)')
    box.execute('INSERT INTO t1 VALUES (1, 1, 1)')
    res = box.execute('SELECT * FROM trigger_catcher')
    assert(box.space.T_RENAMED ~= nil)
    assert(box.space.T_RENAMED.index.T_TO_RENAME_A == nil)
    assert(box.space.T_TO_RENAME == nil)
    return {'Finished ok, errors and trigger catcher content: ', err1, err2,
            err3, err4, res}
end$
function monster_ddl_clear()
    box.execute('DROP TRIGGER IF EXISTS t1t;')
    box.execute('DROP TABLE IF EXISTS trigger_catcher;')
    box.execute('ALTER TABLE t1 DROP CONSTRAINT fk1;')
    box.execute('DROP TABLE IF EXISTS t2')
    box.execute('DROP TABLE IF EXISTS t1')
    box.execute('DROP TABLE IF EXISTS t_renamed')
    monster_ddl_is_clean()
end$
test_run:cmd("setopt delimiter ''")$

-- No txn.
true_ddl_res = monster_ddl()
true_ddl_res

true_check_res = monster_ddl_check()
true_check_res

monster_ddl_clear()

-- Both DDL and cleanup in one txn.
ddl_res = nil
box.begin() ddl_res = monster_ddl() monster_ddl_clear() box.commit()
monster_ddl_cmp_res(ddl_res, true_ddl_res)

-- DDL in txn, cleanup is not.
box.begin() ddl_res = monster_ddl() box.commit()
monster_ddl_cmp_res(ddl_res, true_ddl_res)

check_res = monster_ddl_check()
monster_ddl_cmp_res(check_res, true_check_res)

monster_ddl_clear()

-- DDL is not in txn, cleanup is.
ddl_res = monster_ddl()
monster_ddl_cmp_res(ddl_res, true_ddl_res)

check_res = monster_ddl_check()
monster_ddl_cmp_res(check_res, true_check_res)

box.begin() monster_ddl_clear() box.commit()

-- DDL and cleanup in separate txns.
box.begin() ddl_res = monster_ddl() box.commit()
monster_ddl_cmp_res(ddl_res, true_ddl_res)

check_res = monster_ddl_check()
monster_ddl_cmp_res(check_res, true_check_res)

box.begin() monster_ddl_clear() box.commit()

-- Try SQL transactions.
box.execute('START TRANSACTION') ddl_res = monster_ddl() box.execute('COMMIT')
monster_ddl_cmp_res(ddl_res, true_ddl_res)

check_res = monster_ddl_check()
monster_ddl_cmp_res(check_res, true_check_res)

box.execute('START TRANSACTION') monster_ddl_clear() box.execute('COMMIT')

box.execute('START TRANSACTION') ddl_res = monster_ddl() box.execute('ROLLBACK')
monster_ddl_cmp_res(ddl_res, true_ddl_res)

--
-- Voluntary rollback.
--
test_run:cmd("setopt delimiter ';'")
box.begin()
box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')
assert(box.space.T1 ~= nil)
box.execute('CREATE TABLE t2(id INTEGER PRIMARY KEY);')
assert(box.space.T2 ~= nil)
box.rollback();

box.space.T1 == nil and box.space.T2 == nil;

box.begin()
save1 = box.savepoint()
box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY)')
save2 = box.savepoint()
box.execute('CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER)')
box.execute('CREATE INDEX t2a ON t2(a)')
save3 = box.savepoint()
assert(box.space.T1 ~= nil)
assert(box.space.T2 ~= nil)
assert(box.space.T2.index.T2A ~= nil)
box.execute('DROP TABLE t2')
assert(box.space.T2 == nil)
box.rollback_to_savepoint(save3)
assert(box.space.T2 ~= nil)
assert(box.space.T2.index.T2A ~= nil)
save3 = box.savepoint()
box.execute('DROP TABLE t2')
assert(box.space.T2 == nil)
box.rollback_to_savepoint(save2)
assert(box.space.T2 == nil)
assert(box.space.T1 ~= nil)
box.rollback_to_savepoint(save1)
box.commit();
test_run:cmd("setopt delimiter ''");

box.space.T1 == nil and box.space.T2 == nil

--
-- Unexpected rollback.
--

box.begin() ddl_res = monster_ddl() require('fiber').yield()
monster_ddl_cmp_res(ddl_res, true_ddl_res)
box.commit()
box.rollback()
monster_ddl_clear()