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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
-- test-run result file version 2
test_run = require('test_run').new()
| ---
| ...
engine = test_run:get_cfg('engine')
| ---
| ...
_ = box.space._session_settings:update('sql_default_engine', {{'=', 2, engine}})
| ---
| ...
--
-- Check a constraint name for duplicate within a single
-- <CREATE TABLE> statement.
--
box.execute('CREATE TABLE t1 (i INT PRIMARY KEY);')
| ---
| - row_count: 1
| ...
test_run:cmd("setopt delimiter ';'");
| ---
| - true
| ...
box.execute([[CREATE TABLE t2 (i INT, CONSTRAINT c CHECK (i > 0),
CONSTRAINT c PRIMARY KEY (i));]]);
| ---
| - null
| - PRIMARY KEY constraint 'C' already exists in space 'T2'
| ...
box.execute([[CREATE TABLE t2 (i INT,
CONSTRAINT c FOREIGN KEY(i) REFERENCES t1(i),
CONSTRAINT c PRIMARY KEY (i));]]);
| ---
| - null
| - PRIMARY KEY constraint 'C' already exists in space 'T2'
| ...
box.execute([[CREATE TABLE t2 (i INT PRIMARY KEY,
CONSTRAINT c CHECK (i > 0),
CONSTRAINT c UNIQUE (i));]]);
| ---
| - null
| - UNIQUE constraint 'C' already exists in space 'T2'
| ...
box.execute([[CREATE TABLE t2 (i INT PRIMARY KEY,
CONSTRAINT c FOREIGN KEY(i) REFERENCES t1(i),
CONSTRAINT c UNIQUE (i));]]);
| ---
| - null
| - UNIQUE constraint 'C' already exists in space 'T2'
| ...
box.execute([[CREATE TABLE t2 (i INT PRIMARY KEY,
CONSTRAINT c CHECK (i > 0),
CONSTRAINT c CHECK (i < 0));]]);
| ---
| - null
| - CHECK constraint 'C' already exists in space 'T2'
| ...
box.execute([[CREATE TABLE t2 (i INT PRIMARY KEY,
CONSTRAINT c FOREIGN KEY(i) REFERENCES t1(i),
CONSTRAINT c CHECK (i > 0));]]);
| ---
| - null
| - FOREIGN KEY constraint 'C' already exists in space 'T2'
| ...
box.execute([[CREATE TABLE t2 (i INT PRIMARY KEY CONSTRAINT c REFERENCES t1(i),
CONSTRAINT c FOREIGN KEY(i) REFERENCES t1(i))]]);
| ---
| - null
| - FOREIGN KEY constraint 'C' already exists in space 'T2'
| ...
test_run:cmd("setopt delimiter ''");
| ---
| - true
| ...
--
-- Check a constraint name for duplicate using <ALTER TABLE>
-- statement.
--
box.execute('CREATE TABLE t2 (i INT CONSTRAINT c PRIMARY KEY);')
| ---
| - row_count: 1
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT c CHECK(i > 0);')
| ---
| - null
| - PRIMARY KEY constraint 'C' already exists in space 'T2'
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT c UNIQUE(i);')
| ---
| - null
| - Index 'C' already exists in space 'T2'
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT c FOREIGN KEY(i) REFERENCES t1(i);')
| ---
| - null
| - PRIMARY KEY constraint 'C' already exists in space 'T2'
| ...
--
-- Make sure that a constraint's name isn't kept after the
-- constraint drop.
--
box.execute('CREATE UNIQUE INDEX d ON t2(i);')
| ---
| - row_count: 1
| ...
box.execute('DROP INDEX d ON t2;')
| ---
| - row_count: 1
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT d CHECK(i > 0);')
| ---
| - row_count: 1
| ...
box.space.T2.ck_constraint.D:drop()
| ---
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT d FOREIGN KEY(i) REFERENCES t1(i);')
| ---
| - row_count: 1
| ...
box.execute('ALTER TABLE t2 DROP CONSTRAINT d;')
| ---
| - row_count: 1
| ...
--
-- The same inside a transaction.
--
box.begin() \
box.execute('CREATE UNIQUE INDEX d ON t2(i);') \
box.execute('DROP INDEX d ON t2;') \
box.execute('ALTER TABLE t2 ADD CONSTRAINT d CHECK(i > 0);') \
box.space.T2.ck_constraint.D:drop() \
box.execute('ALTER TABLE t2 ADD CONSTRAINT d FOREIGN KEY(i) REFERENCES t1(i);') \
box.execute('ALTER TABLE t2 DROP CONSTRAINT d;') \
box.commit()
| ---
| ...
--
-- Circle renaming in a transaction.
--
box.execute('CREATE UNIQUE INDEX d ON t2(i);')
| ---
| - row_count: 1
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT e CHECK (i > 0);')
| ---
| - row_count: 1
| ...
box.begin() \
-- Rename CHECK E to F. Note, CHECK can't be properly renamed, \
-- because CHECK name is a primary key of _ck_constraint. \
-- It can be only dropped and created again with a new name. \
box.space.T2.ck_constraint.E:drop() \
box.execute('ALTER TABLE t2 ADD CONSTRAINT f CHECK (i > 0);') \
-- Rename UNIQUE D to E. \
box.space.T2.index.D:rename('E') \
-- Now E and F are occupied, and D is free. Check this. \
_, err1 = box.execute('ALTER TABLE t2 ADD CONSTRAINT e CHECK (i > 0);') \
_, err2 = box.execute('CREATE UNIQUE INDEX f ON t2(i);') \
_, err3 = box.execute('CREATE UNIQUE INDEX d ON t2(i);') \
box.rollback()
| ---
| ...
-- Fail. E and F were occupied in the end of the transaction. The
-- error messages say that E was an index, not a CHECK, like it
-- was before the transaction.
err1, err2
| ---
| - UNIQUE constraint 'E' already exists in space 'T2'
| - CHECK constraint 'F' already exists in space 'T2'
| ...
-- Success, D was free in the end.
err3
| ---
| - null
| ...
-- After rollback ensure, that the names E and D are occupied both
-- again.
box.execute('ALTER TABLE t2 ADD CONSTRAINT d CHECK (i > 0);')
| ---
| - null
| - UNIQUE constraint 'D' already exists in space 'T2'
| ...
box.execute('CREATE UNIQUE INDEX e ON t2(i);')
| ---
| - null
| - CHECK constraint 'E' already exists in space 'T2'
| ...
-- F is free after rollback.
box.execute('CREATE UNIQUE INDEX f ON t2(i);')
| ---
| - row_count: 1
| ...
-- Cleanup after the test case.
box.execute('DROP INDEX f ON t2;')
| ---
| - row_count: 1
| ...
box.execute('DROP INDEX d ON t2;')
| ---
| - row_count: 1
| ...
box.space.T2.ck_constraint.E:drop()
| ---
| ...
--
-- Make sure, that altering of an index name affects its record
-- in the space's constraint hash table.
--
box.execute('CREATE UNIQUE INDEX d ON t2(i);')
| ---
| - row_count: 1
| ...
box.space.T2.index.D:rename('E')
| ---
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT e CHECK(i > 0);')
| ---
| - null
| - UNIQUE constraint 'E' already exists in space 'T2'
| ...
--
-- Try rename to an already occupied name.
--
box.execute('ALTER TABLE t2 ADD CONSTRAINT f CHECK(i > 0);')
| ---
| - row_count: 1
| ...
box.execute('CREATE UNIQUE INDEX g ON t2(i);')
| ---
| - row_count: 1
| ...
box.space.T2.index.G:rename('F')
| ---
| - error: CHECK constraint 'F' already exists in space 'T2'
| ...
box.execute('DROP INDEX g ON t2;')
| ---
| - row_count: 1
| ...
box.space.T2.ck_constraint.F:drop()
| ---
| ...
--
-- Make sure, that altering of an index uniqueness puts/drops
-- its name to/from the space's constraint hash table.
--
box.space.T2.index.E:alter({unique = false})
| ---
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT e CHECK(i > 0);')
| ---
| - row_count: 1
| ...
box.space.T2.index.E:alter({unique = true})
| ---
| - error: CHECK constraint 'E' already exists in space 'T2'
| ...
box.space.T2.ck_constraint.E:drop()
| ---
| ...
box.space.T2.index.E:alter({unique = true})
| ---
| ...
box.execute('ALTER TABLE t2 ADD CONSTRAINT e CHECK(i > 0);')
| ---
| - null
| - UNIQUE constraint 'E' already exists in space 'T2'
| ...
-- Alter name and uniqueness of an unique index simultaneously.
box.space.T2.index.E:alter({name = 'D', unique = false})
| ---
| ...
box.execute('CREATE UNIQUE INDEX e ON t2(i);')
| ---
| - row_count: 1
| ...
-- The existing D index is not unique, but regardless of
-- uniqueness, index names should be unique in a space.
box.execute('CREATE UNIQUE INDEX d ON t2(i);')
| ---
| - null
| - Index 'D' already exists in space 'T2'
| ...
--
-- gh-4120: Ensure that <ALTER TABLE DROP CONSTRAINT> works
-- correctly for each type of constraint.
--
-- Drop non-constraint object (non-unique index).
box.execute('CREATE INDEX non_constraint ON t2(i);')
| ---
| - row_count: 1
| ...
box.execute('ALTER TABLE t2 DROP CONSTRAINT non_constraint;')
| ---
| - null
| - Constraint 'NON_CONSTRAINT' does not exist in space 'T2'
| ...
-- Drop UNIQUE constraint.
box.space.T2.index.E ~= nil
| ---
| - true
| ...
box.execute('ALTER TABLE t2 DROP CONSTRAINT e;')
| ---
| - row_count: 1
| ...
box.space.T2.index.E == nil
| ---
| - true
| ...
-- Drop PRIMARY KEY constraint named "C".
box.execute('DROP INDEX non_constraint ON t2;')
| ---
| - row_count: 1
| ...
box.execute('DROP INDEX d ON t2;')
| ---
| - row_count: 1
| ...
box.space.T2.index.C ~= nil
| ---
| - true
| ...
box.execute('ALTER TABLE t2 DROP CONSTRAINT c;')
| ---
| - row_count: 1
| ...
box.space.T2.index.C == nil
| ---
| - true
| ...
-- Drop CHECK constraint.
box.execute('ALTER TABLE t2 ADD CONSTRAINT ck_constraint CHECK(i > 0);')
| ---
| - row_count: 1
| ...
box.space.T2.ck_constraint.CK_CONSTRAINT ~= nil
| ---
| - true
| ...
box.execute('ALTER TABLE t2 DROP CONSTRAINT ck_constraint;')
| ---
| - row_count: 1
| ...
box.space.T2.ck_constraint.CK_CONSTRAINT == nil
| ---
| - true
| ...
-- Drop FOREIGN KEY constraint.
box.execute('ALTER TABLE t2 ADD CONSTRAINT fk FOREIGN KEY(i) REFERENCES t1(i);')
| ---
| - row_count: 1
| ...
box.execute('ALTER TABLE t2 DROP CONSTRAINT fk;')
| ---
| - row_count: 1
| ...
-- Drop non-existing constraint.
box.execute('ALTER TABLE t2 DROP CONSTRAINT non_existing_constraint;')
| ---
| - null
| - Constraint 'NON_EXISTING_CONSTRAINT' does not exist in space 'T2'
| ...
--
-- Cleanup.
--
box.execute('DROP TABLE t2;')
| ---
| - row_count: 1
| ...
box.execute('DROP TABLE t1;')
| ---
| - row_count: 1
| ...
|