| 491 | |
| 492 | /** |
| 493 | * @ticket 20263 |
| 494 | */ |
| 495 | function test_query_with_backticks_does_not_cause_a_query_to_alter_all_columns_and_indices_to_run_even_if_none_have_changed() { |
| 496 | global $wpdb; |
| 497 | |
| 498 | $schema = " |
| 499 | CREATE TABLE {$wpdb->prefix}dbdelta_test2 ( |
| 500 | `id` bigint(20) NOT NULL AUTO_INCREMENT, |
| 501 | `references` varchar(255) NOT NULL, |
| 502 | PRIMARY KEY (`id`), |
| 503 | KEY `compound_key` (`id`,`references`) |
| 504 | ) |
| 505 | "; |
| 506 | |
| 507 | $wpdb->query( $schema ); |
| 508 | |
| 509 | $updates = dbDelta( $schema ); |
| 510 | |
| 511 | $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test2" ); |
| 512 | $compound_key_index = wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key' ) ); |
| 513 | |
| 514 | $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" ); |
| 515 | |
| 516 | $this->assertCount( 2, $compound_key_index ); |
| 517 | $this->assertEmpty( $updates ); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * @ticket 20263 |
| 522 | */ |
| 523 | function test_index_with_reserved_keywords_can_be_created() { |
| 524 | global $wpdb; |
| 525 | |
| 526 | $updates = dbDelta( |
| 527 | " |
| 528 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 529 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 530 | column_1 varchar(255) NOT NULL, |
| 531 | column_2 text, |
| 532 | column_3 blob, |
| 533 | `references` varchar(255) NOT NULL, |
| 534 | PRIMARY KEY (id), |
| 535 | KEY key_1 (column_1), |
| 536 | KEY compound_key (id , column_1), |
| 537 | KEY compound_key2 (id,`references`), |
| 538 | FULLTEXT KEY fulltext_key (column_1) |
| 539 | ) ENGINE=MyISAM |
| 540 | " |
| 541 | ); |
| 542 | |
| 543 | $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test" ); |
| 544 | |
| 545 | $this->assertCount( 2, wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key2' ) , 'AND' ) ); |
| 546 | |
| 547 | $this->assertSame( |
| 548 | array( |
| 549 | "{$wpdb->prefix}dbdelta_test.references" => "Added column {$wpdb->prefix}dbdelta_test.references", |
| 550 | 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `compound_key2` (`id`,`references`)", |
| 551 | ), |
| 552 | $updates |
| 553 | ); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @ticket 20263 |
| 558 | */ |
| 559 | function test_wp_get_db_schema_does_no_alter_queries_on_existing_install() { |
| 560 | $updates = dbDelta( wp_get_db_schema() ); |
| 561 | |
| 562 | $this->assertEmpty( $updates ); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * @ticket 20263 |
| 567 | */ |
| 568 | function test_all_possible_index_types_are_supported() { |
| 569 | global $wpdb; |
| 570 | |
| 571 | $schema = " |
| 572 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 573 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 574 | column_1 varchar(255) NOT NULL, |
| 575 | column_2 text, |
| 576 | column_3 blob, |
| 577 | PRIMARY KEY (id), |
| 578 | KEY key_1 (column_1), |
| 579 | KEY compound_key (id,column_1), |
| 580 | FULLTEXT KEY fulltext_key (column_1), |
| 581 | INDEX key_2 (column_1), |
| 582 | UNIQUE KEY key_3 (column_1), |
| 583 | UNIQUE INDEX key_4 (column_1), |
| 584 | FULLTEXT INDEX key_5 (column_1), |
| 585 | ) ENGINE=MyISAM |
| 586 | "; |
| 587 | |
| 588 | $creates = dbDelta( $schema ); |
| 589 | $this->assertSame( |
| 590 | array( |
| 591 | 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`)", |
| 592 | 1 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_3` (`column_1`)", |
| 593 | 2 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_4` (`column_1`)", |
| 594 | 3 => "Added index {$wpdb->prefix}dbdelta_test FULLTEXT KEY `key_5` (`column_1`)", |
| 595 | ), |
| 596 | $creates |
| 597 | ); |
| 598 | |
| 599 | $updates = dbDelta( $schema ); |
| 600 | $this->assertEmpty( $updates ); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * @ticket 20263 |
| 605 | */ |
| 606 | function test_index_and_key_is_the_same_and_does_not_recreate_index() { |
| 607 | global $wpdb; |
| 608 | |
| 609 | $updates = dbDelta( |
| 610 | " |
| 611 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 612 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 613 | column_1 varchar(255) NOT NULL, |
| 614 | column_2 text, |
| 615 | column_3 blob, |
| 616 | PRIMARY KEY (id), |
| 617 | INDEX key_1 (column_1), |
| 618 | INDEX compound_key (id,column_1), |
| 619 | FULLTEXT INDEX fulltext_key (column_1) |
| 620 | ) ENGINE=MyISAM |
| 621 | " |
| 622 | ); |
| 623 | |
| 624 | $this->assertEmpty( $updates ); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * @ticket 20263 |
| 629 | */ |
| 630 | function test_indices_with_prefix_limits_are_created_and_do_not_cause_an_index_recreate() { |
| 631 | global $wpdb; |
| 632 | |
| 633 | $schema = " |
| 634 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 635 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 636 | column_1 varchar(255) NOT NULL, |
| 637 | column_2 text, |
| 638 | column_3 blob, |
| 639 | PRIMARY KEY (id), |
| 640 | KEY key_1 (column_1), |
| 641 | KEY compound_key (id,column_1), |
| 642 | FULLTEXT KEY fulltext_key (column_1), |
| 643 | KEY key_2 (column_1(10)), |
| 644 | KEY key_3 (column_2(100),column_1(10)), |
| 645 | ) ENGINE=MyISAM |
| 646 | "; |
| 647 | |
| 648 | $creates = dbDelta( $schema ); |
| 649 | $this->assertSame( |
| 650 | array( |
| 651 | 0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`(10))", |
| 652 | 1 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_3` (`column_2`(100),`column_1`(10))", |
| 653 | ), |
| 654 | $creates |
| 655 | ); |
| 656 | |
| 657 | $updates = dbDelta( $schema ); |
| 658 | $this->assertEmpty( $updates ); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * @ticket 34959 |
| 663 | */ |
| 664 | function test_index_col_name_with_order_does_not_recreate_index() { |
| 665 | global $wpdb; |
| 666 | |
| 667 | $updates = dbDelta( |
| 668 | " |
| 669 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 670 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 671 | column_1 varchar(255) NOT NULL, |
| 672 | column_2 text, |
| 673 | column_3 blob, |
| 674 | PRIMARY KEY (id), |
| 675 | KEY key_1 (column_1 DESC), |
| 676 | KEY compound_key (id,column_1 ASC), |
| 677 | FULLTEXT KEY fulltext_key (column_1) |
| 678 | ) ENGINE=MyISAM |
| 679 | " |
| 680 | ); |
| 681 | |
| 682 | $this->assertEmpty( $updates ); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * @ticket 34873 |
| 687 | */ |
| 688 | function test_primary_key_with_single_space_does_not_recreate_index() { |
| 689 | global $wpdb; |
| 690 | |
| 691 | $updates = dbDelta( |
| 692 | " |
| 693 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 694 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 695 | column_1 varchar(255) NOT NULL, |
| 696 | column_2 text, |
| 697 | column_3 blob, |
| 698 | PRIMARY KEY (id), |
| 699 | KEY key_1 (column_1), |
| 700 | KEY compound_key (id,column_1), |
| 701 | FULLTEXT KEY fulltext_key (column_1) |
| 702 | ) ENGINE=MyISAM |
| 703 | " |
| 704 | ); |
| 705 | |
| 706 | $this->assertEmpty( $updates ); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * @ticket 34869 |
| 711 | */ |
| 712 | function test_index_definitions_with_spaces_do_not_recreate_indices() { |
| 713 | global $wpdb; |
| 714 | |
| 715 | $updates = dbDelta( |
| 716 | " |
| 717 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 718 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 719 | column_1 varchar(255) NOT NULL, |
| 720 | column_2 text, |
| 721 | column_3 blob, |
| 722 | PRIMARY KEY (id), |
| 723 | KEY key_1 (column_1), |
| 724 | KEY compound_key (id, column_1), |
| 725 | FULLTEXT KEY fulltext_key (column_1) |
| 726 | ) ENGINE=MyISAM |
| 727 | " |
| 728 | ); |
| 729 | |
| 730 | $this->assertEmpty( $updates ); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * @ticket 34871 |
| 735 | */ |
| 736 | function test_index_types_are_not_case_sensitive_and_do_not_recreate_indices() { |
| 737 | global $wpdb; |
| 738 | |
| 739 | $updates = dbDelta( |
| 740 | " |
| 741 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 742 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 743 | column_1 varchar(255) NOT NULL, |
| 744 | column_2 text, |
| 745 | column_3 blob, |
| 746 | PRIMARY KEY (id), |
| 747 | key key_1 (column_1), |
| 748 | key compound_key (id,column_1), |
| 749 | FULLTEXT KEY fulltext_key (column_1) |
| 750 | ) ENGINE=MyISAM |
| 751 | " |
| 752 | ); |
| 753 | |
| 754 | $this->assertEmpty( $updates ); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * @ticket 34874 |
| 759 | */ |
| 760 | function test_index_names_are_not_case_sensitive_and_do_not_recreate_indices() { |
| 761 | $this->markTestSkipped(); |
| 762 | global $wpdb; |
| 763 | |
| 764 | $updates = dbDelta( |
| 765 | " |
| 766 | CREATE TABLE {$wpdb->prefix}dbdelta_test ( |
| 767 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 768 | column_1 varchar(255) NOT NULL, |
| 769 | column_2 text, |
| 770 | column_3 blob, |
| 771 | PRIMARY KEY (id), |
| 772 | KEY key_1 (column_1), |
| 773 | KEY COMPOUND_KEY (id,column_1), |
| 774 | FULLTEXT KEY fulltext_key (column_1) |
| 775 | ) ENGINE=MyISAM |
| 776 | " |
| 777 | ); |
| 778 | |
| 779 | $this->assertEmpty( $updates ); |
| 780 | } |