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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
|
#
# Tests of foreign keys that need a debug build or debug_sync feature.
#
--source include/have_debug.inc
--source include/have_debug_sync.inc
# Some parts of the test require enabled binary log.
--source include/have_log_bin.inc
SET @saved_binlog_format= @@SESSION.binlog_format;
SET SESSION debug= '+d,skip_dd_table_access_check';
SET @@foreign_key_checks= DEFAULT;
--echo #
--echo # WL#6929: Move FOREIGN KEY constraints to the global data dictionary
--echo #
--echo # Test coverage for foreign key name generation. Should be removed
--echo # or rewritten when WL#7141 is pushed.
CREATE TABLE t1(a INT PRIMARY KEY);
CREATE TABLE t2(a INT PRIMARY KEY);
CREATE TABLE t3(a INT PRIMARY KEY, b INT, c INT);
ALTER TABLE t3 ADD FOREIGN KEY (b) REFERENCES t1(a);
# InnoDB generated name (until after WL#6599)
SHOW CREATE TABLE t3;
# Server generated name (until after WL#6599)
--sorted_result
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
ALTER TABLE t3 ADD FOREIGN KEY (c) REFERENCES t1(a);
# InnoDB generated name (until after WL#6599)
SHOW CREATE TABLE t3;
# Server generated name (until after WL#6599)
--sorted_result
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
ALTER TABLE t3 ADD FOREIGN KEY (b) REFERENCES t1(a);
# InnoDB generated name (until after WL#6599)
SHOW CREATE TABLE t3;
# Server generated name (until after WL#6599)
--sorted_result
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
ALTER TABLE t3 DROP FOREIGN KEY t3_ibfk_1;
# InnoDB generated name (until after WL#6599)
SHOW CREATE TABLE t3;
# Server generated name (until after WL#6599)
--sorted_result
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
ALTER TABLE t3 ADD FOREIGN KEY (b) REFERENCES t1(a);
# InnoDB generated name (until after WL#6599)
SHOW CREATE TABLE t3;
# Server generated name (until after WL#6599)
--sorted_result
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
DROP TABLE t3, t2, t1;
CREATE TABLE t1(a INT PRIMARY KEY);
CREATE TABLE name567890123456789012345678901234567890123456789012345678901234(a INT PRIMARY KEY, b INT);
--error ER_TOO_LONG_IDENT
ALTER TABLE name567890123456789012345678901234567890123456789012345678901234
ADD FOREIGN KEY(b) REFERENCES t1(a);
DROP TABLE name567890123456789012345678901234567890123456789012345678901234, t1;
--echo #
--echo # WL#6049: Meta data locking for foreign keys.
--echo #
--echo #
--echo # Normal CT will set the FK unique constraint name.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
DROP TABLES child, parent;
--echo #
--echo # CT introducing a missing parent will update the FK unique constraint name in the child.
--echo #
SET @@foreign_key_checks= 0;
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
--echo # An index is created for the FK, but the unique constraint name is NULL.
--sorted_result
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
SET @@foreign_key_checks= 1;
--echo # After creating the parent, the unique constraint name is updated.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
DROP TABLES child, parent;
--echo #
--echo # CTL does not copy FKs from the source table.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
CREATE TABLE child_copy LIKE child;
--echo ## The index is re-created for the new table.
--sorted_result
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
--sorted_result
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child_copy');
--echo ## ... but not the constraint.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
DROP TABLES child, child_copy, parent;
--echo #
--echo # CTL introducing a missing parent will update the FK in the child.
--echo #
SET @@foreign_key_checks= 0;
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
--echo ## An index is created for the FK, but the unique constraint name is NULL.
--sorted_result
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'parent');
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
CREATE TABLE parent_base(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
CREATE TABLE parent LIKE parent_base;
SET @@foreign_key_checks= 1;
--echo ## After creating the parent, the unique constraint name is updated.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
DROP TABLE child, parent_base, parent;
--echo #
--echo # CTS will update the unique constraint name in its FK info.
--echo #
CREATE TABLE source(pk INTEGER PRIMARY KEY, j INTEGER);
INSERT INTO source VALUES (1, 1);
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key(j));
INSERT INTO parent VALUES (2, 1);
SET @@SESSION.binlog_format=STATEMENT;
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j)) AS SELECT pk, j AS fk FROM source;
SET SESSION binlog_format= @saved_binlog_format;
SELECT * FROM child;
--echo ## An index is created for the FK, and the unique constraint name is updated.
--sorted_result
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
DROP TABLES source, child, parent;
--echo #
--echo # CTS introducing a missing parent will update the FK in the child.
--echo #
SET @@foreign_key_checks= 0;
CREATE TABLE source(pk INTEGER PRIMARY KEY, j INTEGER);
INSERT INTO source VALUES (1, 1);
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
--echo ## An index is created for the FK, but the unique constraint name is NULL.
--sorted_result
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
SET @@SESSION.binlog_format=STATEMENT;
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key(j)) AS SELECT * FROM source;
SET SESSION binlog_format= @saved_binlog_format;
SELECT * FROM child;
SET @@foreign_key_checks= 1;
--echo ## After creating the parent, the unique constraint name is updated.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
DROP TABLES source, child, parent;
--echo #
--echo # RENAME will update FK information in both children and parents.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY parent_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, k INTEGER, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j), UNIQUE KEY child_key(k));
--echo # Constraint is enforced.
--error ER_NO_REFERENCED_ROW_2
INSERT INTO child VALUES (1, 2, 3);
CREATE TABLE grandchild(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES child(k));
--echo # Constraint is enforced.
--error ER_NO_REFERENCED_ROW_2
INSERT INTO grandchild VALUES (1, 2);
SET @@foreign_key_checks= 0;
CREATE TABLE orphan_grandchild(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES siebling(k));
SET @@foreign_key_checks= 1;
--echo # FK definitions before rename:
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
RENAME TABLE child TO siebling;
--echo # After the rename, we see that:
--echo # 1. The name of the constraint is changed to 'siebling_ibfk...'.
--echo # 2. The referenced table name of the grandchild is changed to 'siebling'.
--echo # 3. The unique constraint name of the orphan_grandchild is corrected.
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
--echo # Constraint is still enforced.
--error ER_NO_REFERENCED_ROW_2
INSERT INTO siebling VALUES (1, 2, 3);
--echo # Constraint is still enforced.
--error ER_NO_REFERENCED_ROW_2
INSERT INTO grandchild VALUES (1, 2);
--echo # Constraint is enforced here too.
--error ER_NO_REFERENCED_ROW_2
INSERT INTO orphan_grandchild VALUES (1, 2);
DROP TABLE grandchild;
DROP TABLE orphan_grandchild;
DROP TABLE siebling;
DROP TABLE parent;
--echo # When processing LOCK TABLES, we will prelock even
--echo # when F_K_C = 0.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY parent_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j) ON DELETE CASCADE);
SET @@foreign_key_checks= 0;
LOCK TABLES parent WRITE;
--echo # There are two metadata locks because the child has
--echo # two different FK roles wrt. the parent. Note that
--echo # the locks are stronger for LOCK TABLES.
SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COLUMN_NAME,
LOCK_TYPE FROM performance_schema.metadata_locks
WHERE OBJECT_NAME LIKE 'child'
ORDER BY OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COLUMN_NAME, LOCK_TYPE;
--echo # From another connection, verify that child is locked.
--connect (con_1, localhost, root)
SET @@session.lock_wait_timeout= 1;
--error ER_LOCK_WAIT_TIMEOUT
INSERT INTO child VALUES (1, 1);
--disconnect con_1
--connection default
UNLOCK TABLES;
SET @@foreign_key_checks= 1;
--echo # Now, the locks are gone.
SELECT LOCK_TYPE FROM performance_schema.metadata_locks
WHERE OBJECT_NAME LIKE 'child';
DROP TABLE child;
DROP TABLE parent;
--echo #
--echo # A prepared statement will become invalid if a child table
--echo # is modified between executions of the prepared statement.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY parent_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j) ON DELETE CASCADE);
PREPARE stmt FROM 'DELETE FROM parent WHERE pk = ?';
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
SET @a= 1;
EXECUTE stmt USING @a;
--echo # No reprepare for first execution.
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
--echo # Altering child will trigger reprepare on next execution.
ALTER TABLE child ADD COLUMN (j INTEGER);
EXECUTE stmt USING @a;
--echo # Statement has been reprepared for second execution.
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
EXECUTE stmt USING @a;
--echo # Cache version for the prelock entry is updated, so no
--echo # reprepare for third execution.
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
DROP TABLE child;
DROP TABLE parent;
--echo #
--echo # Check situations where there are multiple foreign keys
--echo # referring the same table.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER, j INTEGER,
UNIQUE KEY parent_i_key(i), UNIQUE KEY parent_j_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER, fk_j INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i),
FOREIGN KEY (fk_j) REFERENCES parent(j));
ALTER TABLE child RENAME TO siebling;
DROP TABLES siebling, parent;
--echo #
--echo # Rename a table multiple times in the same statement.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_key(i));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i));
RENAME TABLE parent TO mother, mother TO father;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
RENAME TABLE child TO sister, sister TO brother;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
RENAME TABLE father TO mother, brother TO sister, mother TO parent, sister TO child;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLES child, parent;
--echo #
--echo # Let a RENAME statement introduce a missing parent and rename
--echo # it further as well.
--echo #
SET @@foreign_key_checks= 0;
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES mother(i));
SET @@foreign_key_checks= 1;
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_key(i));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
RENAME TABLE parent TO mother, mother TO father;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLES child, father;
--echo #
--echo # Rename a parent with a child having several FKs to it.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER, j INTEGER,
UNIQUE KEY parent_i_key(i),
UNIQUE KEY parent_j_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER, fk_j INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i),
FOREIGN KEY (fk_j) REFERENCES parent(j));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
RENAME TABLE parent TO mother;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLES child, mother;
--echo #
--echo # ALTER TABLE RENAME and complex ALTER TABLE RENAME involving
--echo # self-referncing foreign key.
--echo #
CREATE TABLE self (pk INT PRIMARY KEY, fk INT, FOREIGN KEY(fk) REFERENCES self(pk));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE self RENAME TO self2;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE self2 RENAME TO self3, ADD COLUMN i INT;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE self3;
--echo #
--echo # Drop a schema with tables referencing/being referenced by tables
--echo # in a different schema.
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_i_key(i));
SET @@foreign_key_checks= 0;
CREATE TABLE grandchild(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES s1.child(i));
SET @@foreign_key_checks= 1;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
CREATE SCHEMA s1;
CREATE TABLE s1.child(pk INTEGER PRIMARY KEY, i INTEGER, fk_i INTEGER,
UNIQUE KEY child_i_key(i),
FOREIGN KEY (fk_i) REFERENCES test.parent(i));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
--error ER_FK_CANNOT_DROP_PARENT
DROP SCHEMA s1;
SET @@foreign_key_checks= 0;
DROP SCHEMA s1;
SET @@foreign_key_checks= 1;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
--echo # Skip FK to parent.
CREATE SCHEMA s1;
CREATE TABLE s1.child(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY child_i_key(i));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
--error ER_FK_CANNOT_DROP_PARENT
DROP SCHEMA s1;
SET @@foreign_key_checks= 0;
DROP SCHEMA s1;
SET @@foreign_key_checks= 1;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
DROP TABLE grandchild;
--echo # No FK from grandchild.
CREATE SCHEMA s1;
CREATE TABLE s1.child(pk INTEGER PRIMARY KEY, fk_i INTEGER);
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
--echo # Introduce FK to parent.
ALTER TABLE s1.child ADD FOREIGN KEY (fk_i) REFERENCES test.parent(i);
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP SCHEMA s1;
DROP TABLE parent;
--echo #
--echo # Trigger an error in Foreign_key_parents_invalidator::invalidate().
--echo #
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_key(i));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
SET @@session.debug= '+d,fail_while_invalidating_fk_parents';
ALTER TABLE child RENAME TO siebling;
SET @@session.debug= '-d,fail_while_invalidating_fk_parents';
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE siebling, parent;
--echo #
--echo # Coverage for various corner cases when figuring out unique_constraint_name.
--echo #
CREATE TABLE parent (i INT, j INT, PRIMARY KEY (i), UNIQUE u(i,j));
CREATE TABLE child (i INT, j INT, FOREIGN KEY (i, j) REFERENCES parent (i, j));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE parent RENAME KEY u TO u1;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE child, parent;
CREATE TABLE parent (i INT, j INT, k INT, PRIMARY KEY (i), UNIQUE u(j), UNIQUE u1(i,j), UNIQUE u2(i,j,k));
CREATE TABLE child (i INT, j INT, k INT, FOREIGN KEY (i, j, k) REFERENCES parent (i, j, k));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE parent RENAME KEY u2 TO u3;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE child, parent;
CREATE TABLE parent (i INT, j INT,
d INT GENERATED ALWAYS AS (i) VIRTUAL,
e INT GENERATED ALWAYS AS (j) VIRTUAL,
PRIMARY KEY (i), UNIQUE u(i,d), UNIQUE u1(i,j,e));
CREATE TABLE child (i INT, j INT, FOREIGN KEY (i, j) REFERENCES parent (i, j));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE parent RENAME KEY u1 TO u2;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE child, parent;
CREATE TABLE parent (i INT, a VARCHAR(10), b VARCHAR(10),
PRIMARY KEY (i), UNIQUE u(i,a(5)), UNIQUE u1(i,a,b(5)));
CREATE TABLE child (i INT, a VARCHAR(10), FOREIGN KEY (i, a) REFERENCES parent (i, a));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE parent RENAME KEY u1 TO u2;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE child, parent;
CREATE TABLE self (i INT, j INT, i2 INT, j2 INT, PRIMARY KEY (i), UNIQUE u(i,j),
FOREIGN KEY (i2, j2) REFERENCES self (i, j));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE self RENAME KEY u TO u1;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE self;
CREATE TABLE self (i INT, j INT, k INT, i2 INT, j2 INT, k2 INT,
PRIMARY KEY (i), UNIQUE u(j), UNIQUE u1(i,j), UNIQUE u2(i,j,k),
FOREIGN KEY (i2, j2, k2) REFERENCES self (i, j, k));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE self RENAME KEY u2 TO u3;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE self;
CREATE TABLE self (i INT, j INT, i2 INT, j2 INT,
d INT GENERATED ALWAYS AS (i) VIRTUAL,
e INT GENERATED ALWAYS AS (j) VIRTUAL,
PRIMARY KEY (i), UNIQUE u(i,d), UNIQUE u1(i,j,e),
FOREIGN KEY (i2, j2) REFERENCES self (i, j));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE self RENAME KEY u1 TO u2;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE self;
CREATE TABLE self (i INT, a VARCHAR(10), b VARCHAR(10), i2 INT, a2 VARCHAR(10),
PRIMARY KEY (i), UNIQUE u(i,a(5)), UNIQUE u1(i,a,b(5)),
FOREIGN KEY (i2, a2) REFERENCES self (i, a));
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
ALTER TABLE self RENAME KEY u1 TO u2;
--sorted_result
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
DROP TABLE self;
# Restore defaults.
SET @@foreign_key_checks= DEFAULT;
SET SESSION debug= '-d,skip_dd_table_access_check';
--echo #
--echo # Part of systemic test coverage for metadata locks related to foreign
--echo # keys acquired by various DDL statements which requires debug_sync.
--echo #
--echo # The main part of this coverage resides in foreign_key.test.
--echo #
--enable_connect_log
connect (con1, localhost, root,,);
connection default;
--echo #
--echo # 7) ALTER TABLE ... INPLACE
--echo #
--echo # 7.1) ALTER TABLE ... ADD FOREIGN KEY ... INPLACE must start by
--echo # acquiring SU lock on parent table.
CREATE TABLE parent (pk INT PRIMARY KEY);
CREATE TABLE child (fk INT);
SET DEBUG_SYNC="alter_table_inplace_after_lock_downgrade SIGNAL reached WAIT_FOR go";
SET FOREIGN_KEY_CHECKS=0;
--send ALTER TABLE child ADD CONSTRAINT fk FOREIGN KEY (fk) REFERENCES parent (pk), ALGORITHM=INPLACE;
connection con1;
SET DEBUG_SYNC="now WAIT_FOR reached";
--echo # DML on parent is still possible at this point.
INSERT INTO parent VALUES (1);
--echo # But not DDL.
SET @old_lock_wait_timeout= @@lock_wait_timeout;
SET @@lock_wait_timeout= 1;
--error ER_LOCK_WAIT_TIMEOUT
ALTER TABLE parent ADD COLUMN a INT;
SET @@lock_wait_timeout= @old_lock_wait_timeout;
SET DEBUG_SYNC="now SIGNAL go";
connection default;
--echo # Reap ALTER TABLE
--reap
SET FOREIGN_KEY_CHECKS=1;
ALTER TABLE child DROP FOREIGN KEY fk;
--echo #
--echo # 8) ALTER TABLE ... COPY
--echo #
--echo # 8.1) ALTER TABLE ... ADD FOREIGN KEY ... COPY must start by
--echo # acquiring SU lock on parent table.
SET DEBUG_SYNC="alter_table_copy_after_lock_upgrade SIGNAL reached WAIT_FOR go";
--send ALTER TABLE child ADD CONSTRAINT fk FOREIGN KEY (fk) REFERENCES parent (pk), ALGORITHM=COPY;
connection con1;
SET DEBUG_SYNC="now WAIT_FOR reached";
--echo # DML on parent is still possible at this point.
INSERT INTO parent VALUES (2);
--echo # But not DDL.
SET @old_lock_wait_timeout= @@lock_wait_timeout;
SET @@lock_wait_timeout= 1;
--error ER_LOCK_WAIT_TIMEOUT
ALTER TABLE parent ADD COLUMN a INT;
SET @@lock_wait_timeout= @old_lock_wait_timeout;
SET DEBUG_SYNC="now SIGNAL go";
connection default;
--echo # Reap ALTER TABLE
--reap
SET DEBUG_SYNC="RESET";
ALTER TABLE child DROP FOREIGN KEY fk;
--echo # 8.1') ALTER TABLE ... ADD FOREIGN KEY ... COPY due to workaround
--echo # must upgrade SU lock on parent table SRO lock.
SET DEBUG_SYNC="alter_after_copy_table SIGNAL reached WAIT_FOR go";
--send ALTER TABLE child ADD CONSTRAINT fk FOREIGN KEY (fk) REFERENCES parent (pk), ALGORITHM=COPY;
connection con1;
SET DEBUG_SYNC="now WAIT_FOR reached";
--echo # SELECT on parent is still possible at this point.
SELECT * FROM parent;
--echo # But not changes.
SET @old_lock_wait_timeout= @@lock_wait_timeout;
SET @@lock_wait_timeout= 1;
--error ER_LOCK_WAIT_TIMEOUT
DELETE FROM parent;
SET @@lock_wait_timeout= @old_lock_wait_timeout;
SET DEBUG_SYNC="now SIGNAL go";
connection default;
--echo # Reap ALTER TABLE
--reap
SET DEBUG_SYNC="RESET";
DROP TABLES child, parent;
connection con1;
disconnect con1;
--source include/wait_until_disconnected.inc
connection default;
--disable_connect_log
|