Ticket #49364: 49364.2.diff
File 49364.2.diff, 16.0 KB (added by , 2 years ago) |
---|
-
src/wp-admin/includes/upgrade.php
2713 2713 * Useful for creating new tables and updating existing tables to a new structure. 2714 2714 * 2715 2715 * @since 1.5.0 2716 * @since 6.1.0 Ignores display width for integer data types on MySQL 8.0.17 or later, 2717 * to match MySQL behavior. Note: This does not affect MariaDB. 2716 2718 * 2717 2719 * @global wpdb $wpdb WordPress database abstraction object. 2718 2720 * … … 2789 2791 2790 2792 $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' ); 2791 2793 $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' ); 2794 $int_fields = array( 'tinyint', 'smallint', 'mediumint', 'int', 'integer', 'bigint' ); 2792 2795 2793 $global_tables = $wpdb->tables( 'global' ); 2796 $global_tables = $wpdb->tables( 'global' ); 2797 $db_version = $wpdb->db_version(); 2798 $db_server_info = $wpdb->db_server_info(); 2799 2794 2800 foreach ( $cqueries as $table => $qry ) { 2795 2801 // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal. 2796 2802 if ( in_array( $table, $global_tables, true ) && ! wp_should_upgrade_global_tables() ) { … … 2946 2952 $tablefield_field_lowercased = strtolower( $tablefield->Field ); 2947 2953 $tablefield_type_lowercased = strtolower( $tablefield->Type ); 2948 2954 2955 $tablefield_type_without_parentheses = preg_replace( 2956 '/' 2957 . '(.+)' // Field type, e.g. `int`. 2958 . '\(\d*\)' // Display width. 2959 . '(.*)' // Optional attributes, e.g. `unsigned`. 2960 . '/', 2961 '$1$2', 2962 $tablefield_type_lowercased 2963 ); 2964 2965 // Get the type without attributes, e.g. `int`. 2966 $tablefield_type_base = strtok( $tablefield_type_without_parentheses, ' ' ); 2967 2949 2968 // If the table field exists in the field array... 2950 2969 if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) { 2951 2970 … … 2954 2973 $fieldtype = $matches[1]; 2955 2974 $fieldtype_lowercased = strtolower( $fieldtype ); 2956 2975 2976 $fieldtype_without_parentheses = preg_replace( 2977 '/' 2978 . '(.+)' // Field type, e.g. `int`. 2979 . '\(\d*\)' // Display width. 2980 . '(.*)' // Optional attributes, e.g. `unsigned`. 2981 . '/', 2982 '$1$2', 2983 $fieldtype_lowercased 2984 ); 2985 2986 // Get the type without attributes, e.g. `int`. 2987 $fieldtype_base = strtok( $fieldtype_without_parentheses, ' ' ); 2988 2957 2989 // Is actual field type different from the field type in query? 2958 2990 if ( $tablefield->Type != $fieldtype ) { 2959 2991 $do_change = true; … … 2969 3001 } 2970 3002 } 2971 3003 3004 if ( in_array( $fieldtype_base, $int_fields, true ) && in_array( $tablefield_type_base, $int_fields, true ) 3005 && $fieldtype_without_parentheses === $tablefield_type_without_parentheses 3006 ) { 3007 /* 3008 * MySQL 8.0.17 or later does not support display width for integer data types, 3009 * so if display width is the only difference, it can be safely ignored. 3010 * Note: This is specific to MySQL and does not affect MariaDB. 3011 */ 3012 if ( version_compare( $db_version, '8.0.17', '>=' ) 3013 && ! str_contains( $db_server_info, 'MariaDB' ) 3014 ) { 3015 $do_change = false; 3016 } 3017 } 3018 2972 3019 if ( $do_change ) { 2973 3020 // Add a query to change the column type. 2974 3021 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ]; -
tests/phpunit/tests/dbdelta.php
22 22 protected $db_engine = ''; 23 23 24 24 /** 25 * Display width for BIGINT data type.25 * The database server version. 26 26 * 27 * Prior to MySQL 8.0.17, default width of 20 digits was used: BIGINT(20). 28 * Since MySQL 8.0.17, display width for integer data types is no longer supported. 27 * @var string 29 28 */ 30 pr otected $bigint_display_width = '';29 private static $db_version; 31 30 32 31 /** 32 * Full database server information. 33 * 34 * @var string 35 */ 36 private static $db_server_info; 37 38 /** 33 39 * Make sure the upgrade code is loaded before the tests are run. 34 40 */ 35 41 public static function set_up_before_class() { 36 42 43 global $wpdb; 44 37 45 parent::set_up_before_class(); 38 46 39 47 require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 48 49 self::$db_version = $wpdb->db_version(); 50 self::$db_server_info = $wpdb->db_server_info(); 40 51 } 41 52 42 53 /** … … 46 57 47 58 global $wpdb; 48 59 49 $db_version = $wpdb->db_version(); 50 51 if ( version_compare( $db_version, '5.7', '<' ) ) { 60 if ( version_compare( self::$db_version, '5.7', '<' ) ) { 52 61 // Prior to MySQL 5.7, InnoDB did not support FULLTEXT indexes, so MyISAM is used instead. 53 62 $this->db_engine = 'ENGINE=MyISAM'; 54 63 } 55 64 56 if ( version_compare( $db_version, '8.0.17', '<' ) ) {57 // Prior to MySQL 8.0.17, default width of 20 digits was used: BIGINT(20).58 $this->bigint_display_width = '(20)';59 }60 61 65 $wpdb->query( 62 66 $wpdb->prepare( 63 67 " 64 68 CREATE TABLE {$wpdb->prefix}dbdelta_test (" . 65 69 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 66 "id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,70 "id bigint(20) NOT NULL AUTO_INCREMENT, 67 71 column_1 varchar(255) NOT NULL, 68 72 column_2 text, 69 73 column_3 blob, … … 109 113 110 114 $updates = dbDelta( 111 115 "CREATE TABLE {$wpdb->prefix}dbdelta_create_test ( 112 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,116 id bigint(20) NOT NULL AUTO_INCREMENT, 113 117 column_1 varchar(255) NOT NULL, 114 118 PRIMARY KEY (id) 115 119 );" … … 144 148 $updates = dbDelta( 145 149 " 146 150 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 147 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,151 id bigint(20) NOT NULL AUTO_INCREMENT, 148 152 column_1 varchar(255) NOT NULL, 149 153 PRIMARY KEY (id), 150 154 KEY key_1 (column_1($this->max_index_length)), … … 163 167 164 168 global $wpdb; 165 169 166 // id: bigint => int(11)170 // id: bigint(20) => int(11) 167 171 $updates = dbDelta( 168 172 " 169 173 CREATE TABLE {$wpdb->prefix}dbdelta_test ( … … 176 180 " 177 181 ); 178 182 183 $bigint_display_width = '(20)'; 184 185 /* 186 * MySQL 8.0.17 or later does not support display width for integer data types, 187 * so if display width is the only difference, it can be safely ignored. 188 * Note: This is specific to MySQL and does not affect MariaDB. 189 */ 190 if ( version_compare( self::$db_version, '8.0.17', '>=' ) 191 && ! str_contains( self::$db_server_info, 'MariaDB' ) 192 ) { 193 $bigint_display_width = ''; 194 } 195 179 196 $this->assertSame( 180 197 array( 181 198 "{$wpdb->prefix}dbdelta_test.id" 182 => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint{$ this->bigint_display_width} to int(11)",199 => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint{$bigint_display_width} to int(11)", 183 200 ), 184 201 $updates 185 202 ); … … 195 212 $updates = dbDelta( 196 213 " 197 214 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 198 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,215 id bigint(20) NOT NULL AUTO_INCREMENT, 199 216 column_1 varchar(255) NOT NULL, 200 217 extra_col longtext, 201 218 PRIMARY KEY (id), … … 230 247 $updates = dbDelta( 231 248 " 232 249 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 233 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,250 id bigint(20) NOT NULL AUTO_INCREMENT, 234 251 PRIMARY KEY (id), 235 252 KEY key_1 (column_1($this->max_index_length)), 236 253 KEY compound_key (id,column_1($this->max_index_length)) … … 254 271 $updates = dbDelta( 255 272 " 256 273 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 257 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,274 id bigint(20) NOT NULL AUTO_INCREMENT, 258 275 column_1 varchar(255) NOT NULL, 259 276 extra_col longtext, 260 277 PRIMARY KEY (id), … … 306 323 $updates = dbDelta( 307 324 " 308 325 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 309 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,326 id bigint(20) NOT NULL AUTO_INCREMENT, 310 327 column_1 varchar(255) NOT NULL, 311 328 PRIMARY KEY (id), 312 329 KEY key_1 (column_1($this->max_index_length)), … … 451 468 $result = dbDelta( 452 469 " 453 470 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 454 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,471 id bigint(20) NOT NULL AUTO_INCREMENT, 455 472 column_1 varchar(255) NOT NULL, 456 473 column_2 tinytext, 457 474 column_3 blob, … … 476 493 $result = dbDelta( 477 494 " 478 495 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 479 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,496 id bigint(20) NOT NULL AUTO_INCREMENT, 480 497 column_1 varchar(255) NOT NULL, 481 498 column_2 text, 482 499 column_3 tinyblob, … … 501 518 $result = dbDelta( 502 519 " 503 520 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 504 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,521 id bigint(20) NOT NULL AUTO_INCREMENT, 505 522 column_1 varchar(255) NOT NULL, 506 523 column_2 bigtext, 507 524 column_3 blob, … … 532 549 $result = dbDelta( 533 550 " 534 551 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 535 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,552 id bigint(20) NOT NULL AUTO_INCREMENT, 536 553 column_1 varchar(255) NOT NULL, 537 554 column_2 text, 538 555 column_3 mediumblob, … … 562 579 563 580 $schema = " 564 581 CREATE TABLE {$wpdb->prefix}dbdelta_test2 ( 565 `id` bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,582 `id` bigint(20) NOT NULL AUTO_INCREMENT, 566 583 `column_1` varchar(255) NOT NULL, 567 584 PRIMARY KEY (id), 568 585 KEY compound_key (id,column_1($this->max_index_length)) … … 585 602 public function test_spatial_indices() { 586 603 global $wpdb; 587 604 588 $db_version = $wpdb->db_version(); 589 590 if ( version_compare( $db_version, '5.4', '<' ) ) { 605 if ( version_compare( self::$db_version, '5.4', '<' ) ) { 591 606 $this->markTestSkipped( 'Spatial indices require MySQL 5.4 and above.' ); 592 607 } 593 608 594 $geom collection_name = 'geomcollection';609 $geometrycollection_name = 'geometrycollection'; 595 610 596 if ( version_compare( $db_version, '8.0.11', '<' ) ) { 597 // Prior to MySQL 8.0.11, GeometryCollection data type name was used. 598 $geomcollection_name = 'geometrycollection'; 611 if ( version_compare( self::$db_version, '8.0.11', '>=' ) 612 && ! str_contains( self::$db_server_info, 'MariaDB' ) 613 ) { 614 /* 615 * MySQL 8.0.11 or later uses GeomCollection data type name 616 * as the preferred synonym for GeometryCollection. 617 * Note: This is specific to MySQL and does not affect MariaDB. 618 */ 619 $geometrycollection_name = 'geomcollection'; 599 620 } 600 621 601 622 $schema = 602 623 " 603 624 CREATE TABLE {$wpdb->prefix}spatial_index_test ( 604 non_spatial bigint {$this->bigint_display_width}unsigned NOT NULL,605 spatial_value {$geom collection_name} NOT NULL,625 non_spatial bigint(20) unsigned NOT NULL, 626 spatial_value {$geometrycollection_name} NOT NULL, 606 627 KEY non_spatial (non_spatial), 607 628 SPATIAL KEY spatial_key (spatial_value) 608 629 ) {$this->db_engine}; … … 618 639 $schema = 619 640 " 620 641 CREATE TABLE {$wpdb->prefix}spatial_index_test ( 621 non_spatial bigint {$this->bigint_display_width}unsigned NOT NULL,622 spatial_value {$geom collection_name} NOT NULL,623 spatial_value2 {$geom collection_name} NOT NULL,642 non_spatial bigint(20) unsigned NOT NULL, 643 spatial_value {$geometrycollection_name} NOT NULL, 644 spatial_value2 {$geometrycollection_name} NOT NULL, 624 645 KEY non_spatial (non_spatial), 625 646 SPATIAL KEY spatial_key (spatial_value) 626 647 SPATIAL KEY spatial_key2 (spatial_value2) … … 648 669 649 670 $schema = " 650 671 CREATE TABLE {$wpdb->prefix}dbdelta_test2 ( 651 `id` bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,672 `id` bigint(20) NOT NULL AUTO_INCREMENT, 652 673 `references` varchar(255) NOT NULL, 653 674 PRIMARY KEY (`id`), 654 675 KEY `compound_key` (`id`,`references`($this->max_index_length)) … … 678 699 $updates = dbDelta( 679 700 " 680 701 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 681 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,702 id bigint(20) NOT NULL AUTO_INCREMENT, 682 703 column_1 varchar(255) NOT NULL, 683 704 column_2 text, 684 705 column_3 blob, … … 708 729 /** 709 730 * @ticket 20263 710 731 */ 711 public function test_wp_get_db_schema_does_no _alter_queries_on_existing_install() {732 public function test_wp_get_db_schema_does_not_alter_queries_on_existing_install() { 712 733 $updates = dbDelta( wp_get_db_schema() ); 713 734 714 735 $this->assertEmpty( $updates ); … … 722 743 723 744 $schema = " 724 745 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 725 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,746 id bigint(20) NOT NULL AUTO_INCREMENT, 726 747 column_1 varchar(255) NOT NULL, 727 748 column_2 text, 728 749 column_3 blob, … … 761 782 $updates = dbDelta( 762 783 " 763 784 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 764 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,785 id bigint(20) NOT NULL AUTO_INCREMENT, 765 786 column_1 varchar(255) NOT NULL, 766 787 column_2 text, 767 788 column_3 blob, … … 784 805 785 806 $schema = " 786 807 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 787 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,808 id bigint(20) NOT NULL AUTO_INCREMENT, 788 809 column_1 varchar(255) NOT NULL, 789 810 column_2 text, 790 811 column_3 blob, … … 819 840 $updates = dbDelta( 820 841 " 821 842 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 822 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,843 id bigint(20) NOT NULL AUTO_INCREMENT, 823 844 column_1 varchar(255) NOT NULL, 824 845 column_2 text, 825 846 column_3 blob, … … 843 864 $updates = dbDelta( 844 865 " 845 866 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 846 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,867 id bigint(20) NOT NULL AUTO_INCREMENT, 847 868 column_1 varchar(255) NOT NULL, 848 869 column_2 text, 849 870 column_3 blob, … … 867 888 $updates = dbDelta( 868 889 " 869 890 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 870 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,891 id bigint(20) NOT NULL AUTO_INCREMENT, 871 892 column_1 varchar(255) NOT NULL, 872 893 column_2 text, 873 894 column_3 blob, … … 891 912 $updates = dbDelta( 892 913 " 893 914 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 894 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,915 id bigint(20) NOT NULL AUTO_INCREMENT, 895 916 column_1 varchar(255) NOT NULL, 896 917 column_2 text, 897 918 column_3 blob, … … 915 936 $updates = dbDelta( 916 937 " 917 938 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 918 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,939 id bigint(20) NOT NULL AUTO_INCREMENT, 919 940 column_1 varchar(255) NOT NULL, 920 941 column_2 text, 921 942 column_3 blob, … … 940 961 $updates = dbDelta( 941 962 " 942 963 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 943 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,964 id bigint(20) NOT NULL AUTO_INCREMENT, 944 965 column_1 varchar(255) NOT NULL, 945 966 column_2 text, 946 967 column_3 blob, … … 965 986 $updates = dbDelta( 966 987 " 967 988 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 968 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,989 id bigint(20) NOT NULL AUTO_INCREMENT, 969 990 column_1 varchar(255) NOT NULL, 970 991 column_2 text, 971 992 column_3 blob, … … 988 1009 $updates = dbDelta( 989 1010 " 990 1011 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 991 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,1012 id bigint(20) NOT NULL AUTO_INCREMENT, 992 1013 column_1 varchar(255) NOT NULL, 993 1014 column_2 text, 994 1015 column_3 blob, … … 1006 1027 $updates = dbDelta( 1007 1028 " 1008 1029 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 1009 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,1030 id bigint(20) NOT NULL AUTO_INCREMENT, 1010 1031 column_1 varchar(255) NOT NULL, 1011 1032 column_2 text, 1012 1033 column_3 blob, … … 1024 1045 $updates = dbDelta( 1025 1046 " 1026 1047 CREATE TABLE {$wpdb->prefix}dbdelta_test ( 1027 id bigint {$this->bigint_display_width}NOT NULL AUTO_INCREMENT,1048 id bigint(20) NOT NULL AUTO_INCREMENT, 1028 1049 column_1 varchar(255) NOT NULL, 1029 1050 column_2 text, 1030 1051 column_3 blob,