| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | These changes fix the following issues with dbDelta:
|
|---|
| 5 |
|
|---|
| 6 | - Can't add an AUTO_INCREMENT column to a table that already exists
|
|---|
| 7 | - Can't change which column is the primary key
|
|---|
| 8 | - Can't change key definitions
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | require_once( __DIR__ . '/wp-load.php' );
|
|---|
| 12 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
|---|
| 13 | global $wpdb;
|
|---|
| 14 |
|
|---|
| 15 | $tables = array();
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | $tables[ "col1(PK), col2" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 19 | col1 varchar(100),
|
|---|
| 20 | col2 varchar(100),
|
|---|
| 21 | PRIMARY KEY (col1)
|
|---|
| 22 | )";
|
|---|
| 23 |
|
|---|
| 24 | $tables[ "col1, col2(PK)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 25 | col1 varchar(100),
|
|---|
| 26 | col2 varchar(100),
|
|---|
| 27 | PRIMARY KEY (col2)
|
|---|
| 28 | )";
|
|---|
| 29 |
|
|---|
| 30 | $tables[ "col1, col2, col3(AI/PK)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 31 | col1 varchar(100),
|
|---|
| 32 | col2 varchar(100),
|
|---|
| 33 | col3 int(255) NOT NULL AUTO_INCREMENT,
|
|---|
| 34 | PRIMARY KEY (col3)
|
|---|
| 35 | )";
|
|---|
| 36 |
|
|---|
| 37 | $tables[ "col1(PK), col2, col3(AI)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 38 | col1 varchar(100),
|
|---|
| 39 | col2 varchar(100),
|
|---|
| 40 | col3 int(100) NOT NULL AUTO_INCREMENT,
|
|---|
| 41 | UNIQUE KEY thename (col3),
|
|---|
| 42 | PRIMARY KEY (col1)
|
|---|
| 43 | )";
|
|---|
| 44 |
|
|---|
| 45 | $tables[ "col1(PK(50)), col2" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 46 | col1 varchar(100),
|
|---|
| 47 | col2 varchar(100),
|
|---|
| 48 | PRIMARY KEY (col1(50))
|
|---|
| 49 | )";
|
|---|
| 50 |
|
|---|
| 51 | $tables[ "col1(PK(75), col2" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 52 | col1 varchar(100),
|
|---|
| 53 | col2 varchar(100),
|
|---|
| 54 | PRIMARY KEY (col1(75))
|
|---|
| 55 | )";
|
|---|
| 56 |
|
|---|
| 57 | $tables[ "col1, col2, col3(PK)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 58 | col1 varchar(100),
|
|---|
| 59 | col2 varchar(100),
|
|---|
| 60 | col3 int(255) NOT NULL,
|
|---|
| 61 | PRIMARY KEY (col3)
|
|---|
| 62 | )";
|
|---|
| 63 |
|
|---|
| 64 | $tables[ "col1(PK), col2(FULLTEXT)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 65 | col1 varchar(100),
|
|---|
| 66 | col2 varchar(100),
|
|---|
| 67 | PRIMARY KEY (col1),
|
|---|
| 68 | FULLTEXT KEY myidx (col2)
|
|---|
| 69 | )";
|
|---|
| 70 |
|
|---|
| 71 | $tables[ "col1(PK), col2(BTREE)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 72 | col1 varchar(100),
|
|---|
| 73 | col2 varchar(100),
|
|---|
| 74 | PRIMARY KEY (col1),
|
|---|
| 75 | KEY myidx (col2)
|
|---|
| 76 | )";
|
|---|
| 77 |
|
|---|
| 78 | $tables[ "col1(PK), col2(UNIQUE)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 79 | col1 varchar(100),
|
|---|
| 80 | col2 varchar(100),
|
|---|
| 81 | PRIMARY KEY (col1),
|
|---|
| 82 | UNIQUE KEY myidx (col2)
|
|---|
| 83 | )";
|
|---|
| 84 |
|
|---|
| 85 | $tables[ "col1(PK), col2, Index(col2,col1)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 86 | col1 varchar(100),
|
|---|
| 87 | col2 varchar(100),
|
|---|
| 88 | PRIMARY KEY (col1),
|
|---|
| 89 | KEY myidx (col2,col1)
|
|---|
| 90 | )";
|
|---|
| 91 |
|
|---|
| 92 | $tables[ "col1(PK), col2, Index(col1,col2)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 93 | col1 varchar(100),
|
|---|
| 94 | col2 varchar(100),
|
|---|
| 95 | PRIMARY KEY (col1),
|
|---|
| 96 | INDEX myidx (col1,col2)
|
|---|
| 97 | )";
|
|---|
| 98 |
|
|---|
| 99 | $tables[ "col1(PK), col2, PK(col1,col2)" ] = "CREATE TABLE dbdelta_tests (
|
|---|
| 100 | col1 varchar(100),
|
|---|
| 101 | col2 varchar(100),
|
|---|
| 102 | PRIMARY KEY (col1,col2)
|
|---|
| 103 | )";
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Test dbDelta.
|
|---|
| 110 | *
|
|---|
| 111 | * 1) Delete the table
|
|---|
| 112 | * 2) Create the table (and make sure it worked)
|
|---|
| 113 | * 3) dbDelta the table (and make sure nothing happens)
|
|---|
| 114 | * 4) dbDelta the new table def (and make sure something happens)
|
|---|
| 115 | * 4) dbDelta the new table def (and make sure nothing happens)
|
|---|
| 116 | */
|
|---|
| 117 | function dbDelta_test( $test_name, $table1, $table2 ) {
|
|---|
| 118 | global $wpdb;
|
|---|
| 119 | $wpdb->query("DROP TABLE IF EXISTS dbdelta_tests");
|
|---|
| 120 |
|
|---|
| 121 | $res1 = dbDelta( $table1 );
|
|---|
| 122 |
|
|---|
| 123 | if ( empty( $res1 ) ) {
|
|---|
| 124 | return -1; // "Couldn't create table 1\n";
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | $res2 = dbDelta( $table1 );
|
|---|
| 128 |
|
|---|
| 129 | if ( !empty( $res2 ) ) {
|
|---|
| 130 | error_log(print_r($res2,true));
|
|---|
| 131 | return -2; // "Table 1 changes were incomplete\n";
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | $res3 = dbDelta( $table2 );
|
|---|
| 135 |
|
|---|
| 136 | if ( empty( $res3 ) && $table1 !== $table2 ) {
|
|---|
| 137 | return -3; // "Couldn't apply table 2 changes\n";
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | $res4 = dbDelta( $table2 );
|
|---|
| 141 |
|
|---|
| 142 | if ( !empty( $res4 ) ) {
|
|---|
| 143 | error_log(print_r($res4,true));
|
|---|
| 144 | return -4; // "Table 2 changes were incomplete\n";
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | return 0; // "OK!\n";
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Command line handling / run tests.
|
|---|
| 153 | */
|
|---|
| 154 |
|
|---|
| 155 | $testId = 0;
|
|---|
| 156 | $keys = array_keys( $tables );
|
|---|
| 157 | $colwidth = 25;
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * These are test numbers where the second table is a subset of the first, so there are no changes to be made.
|
|---|
| 161 | *
|
|---|
| 162 | * Unfortunately, adding additional tests breaks these numbers.
|
|---|
| 163 | */
|
|---|
| 164 | $non_zero_expected_results = array(
|
|---|
| 165 | 4 => -3,
|
|---|
| 166 | 5 => -3,
|
|---|
| 167 | 39 => -3,
|
|---|
| 168 | 43 => -3,
|
|---|
| 169 | 44 => -3,
|
|---|
| 170 | 52 => -3,
|
|---|
| 171 | 57 => -3,
|
|---|
| 172 | 65 => -3,
|
|---|
| 173 | 69 => -3,
|
|---|
| 174 | 91 => -3,
|
|---|
| 175 | 95 => -3,
|
|---|
| 176 | 96 => -3,
|
|---|
| 177 | 104 => -3,
|
|---|
| 178 | 108 => -3,
|
|---|
| 179 | 109 => -3,
|
|---|
| 180 | 117 => -3,
|
|---|
| 181 | 121 => -3,
|
|---|
| 182 | 122 => -3,
|
|---|
| 183 | 130 => -3,
|
|---|
| 184 | 134 => -3,
|
|---|
| 185 | 135 => -3,
|
|---|
| 186 | 143 => -3,
|
|---|
| 187 | 147 => -3,
|
|---|
| 188 | 148 => -3,
|
|---|
| 189 | );
|
|---|
| 190 |
|
|---|
| 191 | $results_map = array(
|
|---|
| 192 | -1 => "Couldn't create table 1",
|
|---|
| 193 | -2 => "Table 1 changes were incomplete",
|
|---|
| 194 | -3 => "Couldn't apply table 2 changes",
|
|---|
| 195 | -4 => "Table 2 changes were incomplete",
|
|---|
| 196 | 'OK' => 'OK',
|
|---|
| 197 | );
|
|---|
| 198 |
|
|---|
| 199 | print "Usage: php ./test.php [test number]\n";
|
|---|
| 200 | print "Results meanings:\n";
|
|---|
| 201 |
|
|---|
| 202 | foreach( $results_map as $k => $res ) {
|
|---|
| 203 | print "$k : $res\n";
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | print "\n";
|
|---|
| 207 |
|
|---|
| 208 | foreach( $keys as $from) {
|
|---|
| 209 | foreach( $keys as $to) {
|
|---|
| 210 | if ( !isset( $argv[1] ) || $argv[1] == $testId ) {
|
|---|
| 211 |
|
|---|
| 212 | print str_pad( $testId,3,' ', STR_PAD_RIGHT);
|
|---|
| 213 | print ': ';
|
|---|
| 214 | print str_pad( "$from => $to", 80, ' ', STR_PAD_RIGHT);
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 | print ': ';
|
|---|
| 218 | $res = dbDelta_test( '', $tables[$from], $tables[$to] );
|
|---|
| 219 | $res = ( $res === 0 || (isset($non_zero_expected_results[ $testId ]) && $res === $non_zero_expected_results[ $testId ] ) ) ? 'OK' : $res;
|
|---|
| 220 | print $res;
|
|---|
| 221 | print "\n";
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | $testId++;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | print "\n";
|
|---|