Changeset 53897
- Timestamp:
- 08/15/2022 01:16:22 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/upgrade.php
r53896 r53897 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. … … 2790 2792 $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' ); 2791 2793 $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' ); 2792 2793 $global_tables = $wpdb->tables( 'global' ); 2794 $int_fields = array( 'tinyint', 'smallint', 'mediumint', 'int', 'integer', 'bigint' ); 2795 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. … … 2949 2955 $tablefield_type_lowercased = strtolower( $tablefield->Type ); 2950 2956 2957 $tablefield_type_without_parentheses = preg_replace( 2958 '/' 2959 . '(.+)' // Field type, e.g. `int`. 2960 . '\(\d*\)' // Display width. 2961 . '(.*)' // Optional attributes, e.g. `unsigned`. 2962 . '/', 2963 '$1$2', 2964 $tablefield_type_lowercased 2965 ); 2966 2967 // Get the type without attributes, e.g. `int`. 2968 $tablefield_type_base = strtok( $tablefield_type_without_parentheses, ' ' ); 2969 2951 2970 // If the table field exists in the field array... 2952 2971 if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) { … … 2956 2975 $fieldtype = $matches[1]; 2957 2976 $fieldtype_lowercased = strtolower( $fieldtype ); 2977 2978 $fieldtype_without_parentheses = preg_replace( 2979 '/' 2980 . '(.+)' // Field type, e.g. `int`. 2981 . '\(\d*\)' // Display width. 2982 . '(.*)' // Optional attributes, e.g. `unsigned`. 2983 . '/', 2984 '$1$2', 2985 $fieldtype_lowercased 2986 ); 2987 2988 // Get the type without attributes, e.g. `int`. 2989 $fieldtype_base = strtok( $fieldtype_without_parentheses, ' ' ); 2958 2990 2959 2991 // Is actual field type different from the field type in query? … … 2968 3000 if ( in_array( $fieldtype_lowercased, $blob_fields, true ) && in_array( $tablefield_type_lowercased, $blob_fields, true ) ) { 2969 3001 if ( array_search( $fieldtype_lowercased, $blob_fields, true ) < array_search( $tablefield_type_lowercased, $blob_fields, true ) ) { 3002 $do_change = false; 3003 } 3004 } 3005 3006 if ( in_array( $fieldtype_base, $int_fields, true ) && in_array( $tablefield_type_base, $int_fields, true ) 3007 && $fieldtype_without_parentheses === $tablefield_type_without_parentheses 3008 ) { 3009 /* 3010 * MySQL 8.0.17 or later does not support display width for integer data types, 3011 * so if display width is the only difference, it can be safely ignored. 3012 * Note: This is specific to MySQL and does not affect MariaDB. 3013 */ 3014 if ( version_compare( $db_version, '8.0.17', '>=' ) 3015 && ! str_contains( $db_server_info, 'MariaDB' ) 3016 ) { 2970 3017 $do_change = false; 2971 3018 } -
trunk/tests/phpunit/tests/dbdelta.php
r52010 r53897 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. 29 */ 30 protected $bigint_display_width = ''; 27 * @var string 28 */ 29 private static $db_version; 30 31 /** 32 * Full database server information. 33 * 34 * @var string 35 */ 36 private static $db_server_info; 31 37 32 38 /** … … 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 … … 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 }55 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 63 } 60 64 … … 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, … … 71 75 KEY key_1 (column_1(%d)), 72 76 KEY compound_key (id,column_1(%d)), 73 FULLTEXT KEY fulltext_key (column_1) ".77 FULLTEXT KEY fulltext_key (column_1)' . 74 78 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 75 79 ") {$this->db_engine} … … 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) … … 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), … … 164 168 global $wpdb; 165 169 166 // id: bigint => int(11)170 // id: bigint(20) => int(11) 167 171 $updates = dbDelta( 168 172 " … … 176 180 " 177 181 ); 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 } 178 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 … … 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, … … 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)), … … 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, … … 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), … … 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, … … 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, … … 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, … … 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, … … 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), … … 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 $geomcollection_name = 'geomcollection'; 595 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'; 609 $geometrycollection_name = 'geometrycollection'; 610 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 … … 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) … … 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) … … 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`), … … 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, … … 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 … … 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, … … 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, … … 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, … … 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, … … 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, … … 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, … … 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, … … 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, … … 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, … … 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, … … 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, … … 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, … … 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,
Note: See TracChangeset
for help on using the changeset viewer.