|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +class WP_MySQL_On_SQLite_Table_Rebuild_Tests extends TestCase { |
| 6 | + |
| 7 | + /** @var PDO */ |
| 8 | + private $sqlite; |
| 9 | + |
| 10 | + /** @var WP_MySQL_On_SQLite */ |
| 11 | + private $engine; |
| 12 | + |
| 13 | + public function setUp(): void { |
| 14 | + $pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class; |
| 15 | + $this->sqlite = new $pdo_class( 'sqlite::memory:' ); |
| 16 | + $this->engine = new WP_MySQL_On_SQLite( |
| 17 | + 'mysql-on-sqlite:dbname=wp', |
| 18 | + null, |
| 19 | + null, |
| 20 | + array( 'sqlite_pdo' => $this->sqlite ) |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @dataProvider stringTableRebuilds |
| 26 | + */ |
| 27 | + public function testRebuildPreservesLegacyStringValues( string $type, string $sqlite_type, string $query, string $column ): void { |
| 28 | + $this->createLegacyStringTable( $type, $sqlite_type ); |
| 29 | + $before = $this->getTableValues( 'Pay`load' ); |
| 30 | + |
| 31 | + $result = $this->engine->query( sprintf( $query, $type ) ); |
| 32 | + if ( $result->columnCount() > 0 ) { |
| 33 | + // Administration statements report errors in their result rows. |
| 34 | + $this->assertSame( array( 'status' ), array_column( $result->fetchAll( PDO::FETCH_ASSOC ), 'Msg_type' ) ); |
| 35 | + } |
| 36 | + |
| 37 | + $this->assertSame( $before, $this->getTableValues( $column ) ); |
| 38 | + $quoted_column = $this->engine->get_connection()->quote_identifier( $column ); |
| 39 | + $this->assertSame( |
| 40 | + array( strtolower( $sqlite_type ) ), |
| 41 | + $this->sqlite->query( |
| 42 | + "SELECT DISTINCT typeof($quoted_column) FROM t WHERE $quoted_column IS NOT NULL" |
| 43 | + )->fetchAll( PDO::FETCH_COLUMN ) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public static function stringTableRebuilds(): array { |
| 48 | + $queries = array( |
| 49 | + 'add' => array( 'ALTER TABLE t ADD COLUMN added TEXT', 'Pay`load' ), |
| 50 | + 'modify' => array( 'ALTER TABLE t MODIFY COLUMN `pay``LOAD` %s', 'pay`LOAD' ), |
| 51 | + 'change' => array( 'ALTER TABLE t CHANGE COLUMN `pay``LOAD` `New``Payload` %s', 'New`Payload' ), |
| 52 | + 'case rename' => array( 'ALTER TABLE t CHANGE COLUMN `pay``LOAD` `PAY``LOAD` %s', 'PAY`LOAD' ), |
| 53 | + 'optimize' => array( 'OPTIMIZE TABLE t', 'Pay`load' ), |
| 54 | + 'repair' => array( 'REPAIR TABLE t', 'Pay`load' ), |
| 55 | + 'drop primary' => array( 'DROP INDEX `PRIMARY` ON t', 'Pay`load' ), |
| 56 | + ); |
| 57 | + $types = array( |
| 58 | + 'BLOB' => array( 'BINARY(32)', 'VARBINARY(32)', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB' ), |
| 59 | + 'TEXT' => array( 'CHAR(32)', 'VARCHAR(32)', 'NCHAR(32)', 'NVARCHAR(32)', 'TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT' ), |
| 60 | + ); |
| 61 | + $cases = array(); |
| 62 | + foreach ( $types as $sqlite_type => $mysql_types ) { |
| 63 | + foreach ( $mysql_types as $type ) { |
| 64 | + foreach ( $queries as $name => $query ) { |
| 65 | + $cases[ "$type $name" ] = array( $type, $sqlite_type, $query[0], $query[1] ); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return $cases; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @dataProvider stringColumnTypeChanges |
| 74 | + */ |
| 75 | + public function testRebuildUsesChangedColumnType( string $old_type, string $new_type, string $storage_class ): void { |
| 76 | + $this->engine->exec( "CREATE TABLE t (payload $old_type)" ); |
| 77 | + $this->engine->exec( "INSERT INTO t VALUES ('000123'), ('abc'), (NULL)" ); |
| 78 | + $this->engine->exec( "ALTER TABLE t CHANGE COLUMN payload renamed $new_type" ); |
| 79 | + $this->assertSame( |
| 80 | + array( |
| 81 | + array( |
| 82 | + 'hex' => '303030313233', |
| 83 | + 'type' => $storage_class, |
| 84 | + ), |
| 85 | + array( |
| 86 | + 'hex' => '616263', |
| 87 | + 'type' => $storage_class, |
| 88 | + ), |
| 89 | + array( |
| 90 | + 'hex' => '', |
| 91 | + 'type' => 'null', |
| 92 | + ), |
| 93 | + ), |
| 94 | + $this->sqlite->query( 'SELECT hex(renamed) AS hex, typeof(renamed) AS type FROM t ORDER BY rowid' )->fetchAll( PDO::FETCH_ASSOC ) |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + public static function stringColumnTypeChanges(): array { |
| 99 | + return array( |
| 100 | + 'text to binary' => array( 'TEXT', 'VARBINARY(32)', 'blob' ), |
| 101 | + 'blob to text' => array( 'BLOB', 'TEXT', 'text' ), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @dataProvider textBackedColumnValues |
| 107 | + */ |
| 108 | + public function testRebuildPreservesTextBackedColumnValues( string $type, string $value ): void { |
| 109 | + $this->engine->exec( "CREATE TABLE t (payload $type)" ); |
| 110 | + $this->sqlite->exec( 'DROP TABLE t' ); |
| 111 | + $this->sqlite->exec( 'CREATE TABLE t (payload TEXT)' ); |
| 112 | + $this->sqlite->exec( "INSERT INTO t VALUES (x'" . bin2hex( $value ) . "')" ); |
| 113 | + |
| 114 | + $this->engine->exec( 'ALTER TABLE t ADD COLUMN note TEXT' ); |
| 115 | + |
| 116 | + $this->assertSame( $value, $this->sqlite->query( 'SELECT payload FROM t' )->fetchColumn() ); |
| 117 | + $this->assertSame( 'text', $this->sqlite->query( 'SELECT typeof(payload) FROM t' )->fetchColumn() ); |
| 118 | + } |
| 119 | + |
| 120 | + public static function textBackedColumnValues(): array { |
| 121 | + return array( |
| 122 | + 'json' => array( 'JSON', '{"name":"žluťoučký"}' ), |
| 123 | + 'enum' => array( "ENUM('abc','def')", 'abc' ), |
| 124 | + 'set' => array( "SET('a','b')", 'a,b' ), |
| 125 | + 'date' => array( 'DATE', '2024-02-29' ), |
| 126 | + 'time' => array( 'TIME(3)', '12:34:56.123' ), |
| 127 | + 'datetime' => array( 'DATETIME(3)', '2024-02-29 12:34:56.123' ), |
| 128 | + 'timestamp' => array( 'TIMESTAMP(3)', '2024-02-29 12:34:56.123' ), |
| 129 | + 'year' => array( 'YEAR', '2024' ), |
| 130 | + 'geometry' => array( 'GEOMETRY', 'POINT(1 2)' ), |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @dataProvider qualifiedTableRebuilds |
| 136 | + */ |
| 137 | + public function testRebuildUsesMainDatabaseMetadata( string $query ): void { |
| 138 | + $this->createLegacyStringTable( 'BLOB', 'BLOB' ); |
| 139 | + $before = $this->getTableValues( 'Pay`load' ); |
| 140 | + |
| 141 | + $this->engine->exec( 'USE information_schema' ); |
| 142 | + $result = $this->engine->query( $query ); |
| 143 | + if ( $result->columnCount() > 0 ) { |
| 144 | + $this->assertSame( array( 'status' ), array_column( $result->fetchAll( PDO::FETCH_ASSOC ), 'Msg_type' ) ); |
| 145 | + } |
| 146 | + |
| 147 | + $this->assertSame( $before, $this->getTableValues( 'Pay`load' ) ); |
| 148 | + $this->assertSame( |
| 149 | + array( 'blob' ), |
| 150 | + $this->sqlite->query( 'SELECT DISTINCT typeof(`Pay``load`) FROM t WHERE `Pay``load` IS NOT NULL' )->fetchAll( PDO::FETCH_COLUMN ) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + public static function qualifiedTableRebuilds(): array { |
| 155 | + return array( |
| 156 | + 'alter' => array( 'ALTER TABLE wp.t ADD COLUMN added TEXT' ), |
| 157 | + 'optimize' => array( 'OPTIMIZE TABLE wp.t' ), |
| 158 | + 'repair' => array( 'REPAIR TABLE wp.t' ), |
| 159 | + 'drop primary' => array( 'DROP INDEX `PRIMARY` ON wp.t' ), |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @dataProvider stringStorageTypes |
| 165 | + */ |
| 166 | + public function testRebuildUsesTemporaryColumnMetadata( string $type ): void { |
| 167 | + $main_type = 'BLOB' === $type ? 'TEXT' : 'BLOB'; |
| 168 | + $this->engine->exec( "CREATE TABLE t (`Pay``load` $main_type)" ); |
| 169 | + $this->engine->exec( "INSERT INTO t VALUES ('main table')" ); |
| 170 | + $this->createLegacyStringTable( $type, $type, true ); |
| 171 | + $before = $this->getTableValues( 'Pay`load' ); |
| 172 | + |
| 173 | + $this->engine->exec( 'ALTER TABLE t ADD COLUMN added TEXT' ); |
| 174 | + |
| 175 | + $this->assertSame( $before, $this->getTableValues( 'Pay`load' ) ); |
| 176 | + $this->assertSame( strtolower( $type ), $this->sqlite->query( 'SELECT typeof(`Pay``load`) FROM temp.t WHERE id = 3' )->fetchColumn() ); |
| 177 | + $this->assertSame( 'main table', $this->sqlite->query( 'SELECT `Pay``load` FROM main.t' )->fetchColumn() ); |
| 178 | + $this->assertSame( strtolower( $main_type ), $this->sqlite->query( 'SELECT typeof(`Pay``load`) FROM main.t' )->fetchColumn() ); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @dataProvider stringStorageTypes |
| 183 | + */ |
| 184 | + public function testFailedRebuildPreservesLegacyValuesAndSchema( string $type ): void { |
| 185 | + $this->createLegacyStringTable( $type, $type ); |
| 186 | + $this->sqlite->exec( 'UPDATE t SET note = NULL WHERE id = 3' ); |
| 187 | + $before = $this->getTableValues( 'Pay`load' ); |
| 188 | + $schema = $this->sqlite->query( "SELECT sql FROM sqlite_master WHERE name = 't'" )->fetchColumn(); |
| 189 | + |
| 190 | + $exception = null; |
| 191 | + try { |
| 192 | + $this->engine->exec( 'ALTER TABLE t MODIFY COLUMN note TEXT NOT NULL' ); |
| 193 | + } catch ( WP_MySQL_On_SQLite_Exception $e ) { |
| 194 | + $exception = $e; |
| 195 | + } |
| 196 | + $this->assertInstanceOf( WP_MySQL_On_SQLite_Exception::class, $exception ); |
| 197 | + $this->assertStringContainsString( 'NOT NULL constraint failed', $exception->getMessage() ); |
| 198 | + $this->assertSame( $before, $this->getTableValues( 'Pay`load' ) ); |
| 199 | + $this->assertSame( $schema, $this->sqlite->query( "SELECT sql FROM sqlite_master WHERE name = 't'" )->fetchColumn() ); |
| 200 | + $this->assertSame( 'text', $this->sqlite->query( 'SELECT typeof(`Pay``load`) FROM t WHERE id = 3' )->fetchColumn() ); |
| 201 | + $this->assertSame( |
| 202 | + 'YES', |
| 203 | + $this->engine->query( "SELECT is_nullable FROM information_schema.columns WHERE table_name = 't' AND column_name = 'note'" )->fetchColumn() |
| 204 | + ); |
| 205 | + $this->assertSame( 0, (int) $this->sqlite->query( "SELECT COUNT(*) FROM sqlite_master WHERE name GLOB '_wp_sqlite_tmp_*'" )->fetchColumn() ); |
| 206 | + |
| 207 | + $this->engine->exec( 'ALTER TABLE t ADD COLUMN added TEXT' ); |
| 208 | + $this->assertSame( $before, $this->getTableValues( 'Pay`load' ) ); |
| 209 | + } |
| 210 | + |
| 211 | + public static function stringStorageTypes(): array { |
| 212 | + return array( |
| 213 | + 'blob' => array( 'BLOB' ), |
| 214 | + 'text' => array( 'TEXT' ), |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * @dataProvider legacyMetadataStates |
| 220 | + */ |
| 221 | + public function testTemporalDefaultMigrationPreservesLegacyStringValues( bool $has_metadata, string $type ): void { |
| 222 | + if ( $has_metadata ) { |
| 223 | + $this->engine->exec( "CREATE TABLE t (id INT, payload $type, created_at TIMESTAMP DEFAULT 0)" ); |
| 224 | + $this->sqlite->exec( 'DROP TABLE t' ); |
| 225 | + } else { |
| 226 | + $this->sqlite->exec( |
| 227 | + 'CREATE TABLE _mysql_data_types_cache ( |
| 228 | + `table` TEXT NOT NULL, |
| 229 | + column_or_index TEXT NOT NULL, |
| 230 | + mysql_type TEXT NOT NULL, |
| 231 | + PRIMARY KEY (`table`, column_or_index) |
| 232 | + )' |
| 233 | + ); |
| 234 | + $this->sqlite->exec( |
| 235 | + "INSERT INTO _mysql_data_types_cache VALUES |
| 236 | + ('t', 'id', 'int'), ('t', 'payload', '" . strtolower( $type ) . "'), ('t', 'created_at', 'timestamp')" |
| 237 | + ); |
| 238 | + } |
| 239 | + $this->sqlite->exec( "CREATE TABLE t (id INTEGER, payload $type, created_at TEXT DEFAULT 0)" ); |
| 240 | + $this->sqlite->exec( "INSERT INTO t (id, payload) VALUES (1, CAST(x'6100FF' AS TEXT)), (2, x'00FF80'), (3, NULL)" ); |
| 241 | + $this->sqlite->exec( |
| 242 | + sprintf( |
| 243 | + "UPDATE %s SET value = '0.0.0' WHERE name = '%s'", |
| 244 | + WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME, |
| 245 | + WP_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME |
| 246 | + ) |
| 247 | + ); |
| 248 | + |
| 249 | + $this->engine = new WP_MySQL_On_SQLite( |
| 250 | + 'mysql-on-sqlite:dbname=wp', |
| 251 | + null, |
| 252 | + null, |
| 253 | + array( 'sqlite_pdo' => $this->sqlite ) |
| 254 | + ); |
| 255 | + |
| 256 | + $this->assertSame( |
| 257 | + array( |
| 258 | + array( |
| 259 | + 'hex' => '6100FF', |
| 260 | + 'type' => strtolower( $type ), |
| 261 | + 'created_at' => '0000-00-00 00:00:00', |
| 262 | + ), |
| 263 | + array( |
| 264 | + 'hex' => '00FF80', |
| 265 | + 'type' => strtolower( $type ), |
| 266 | + 'created_at' => '0000-00-00 00:00:00', |
| 267 | + ), |
| 268 | + array( |
| 269 | + 'hex' => '', |
| 270 | + 'type' => 'null', |
| 271 | + 'created_at' => '0000-00-00 00:00:00', |
| 272 | + ), |
| 273 | + ), |
| 274 | + $this->sqlite->query( 'SELECT hex(payload) AS hex, typeof(payload) AS type, created_at FROM t ORDER BY id' )->fetchAll( PDO::FETCH_ASSOC ) |
| 275 | + ); |
| 276 | + $this->assertSame( SQLITE_DRIVER_VERSION, $this->engine->get_saved_driver_version() ); |
| 277 | + $this->assertSame( |
| 278 | + '0000-00-00 00:00:00', |
| 279 | + $this->engine->query( "SELECT column_default FROM information_schema.columns WHERE table_name = 't' AND column_name = 'created_at'" )->fetchColumn() |
| 280 | + ); |
| 281 | + } |
| 282 | + |
| 283 | + public static function legacyMetadataStates(): array { |
| 284 | + return array( |
| 285 | + 'previously upgraded blob' => array( true, 'BLOB' ), |
| 286 | + 'previously upgraded text' => array( true, 'TEXT' ), |
| 287 | + 'legacy metadata blob' => array( false, 'BLOB' ), |
| 288 | + 'legacy metadata text' => array( false, 'TEXT' ), |
| 289 | + ); |
| 290 | + } |
| 291 | + |
| 292 | + private function createLegacyStringTable( string $type, string $sqlite_type, bool $temporary = false ): void { |
| 293 | + $temporary_keyword = $temporary ? 'TEMPORARY' : ''; |
| 294 | + $this->engine->exec( "CREATE $temporary_keyword TABLE t (id INT PRIMARY KEY, `Pay``load` $type, note TEXT)" ); |
| 295 | + |
| 296 | + // Earlier upgrades retained non-STRICT tables and their original storage classes. |
| 297 | + $this->sqlite->exec( 'DROP TABLE t' ); |
| 298 | + $this->sqlite->exec( "CREATE $temporary_keyword TABLE t (id INT PRIMARY KEY, `Pay``load` $sqlite_type, note TEXT)" ); |
| 299 | + $this->sqlite->exec( |
| 300 | + "INSERT INTO t (rowid, id, `Pay``load`, note) VALUES |
| 301 | + (11, 1, NULL, '000123'), |
| 302 | + (12, 2, '', '000123'), |
| 303 | + (13, 3, 'abc', '000123'), |
| 304 | + (14, 4, '000123', '000123'), |
| 305 | + (15, 5, 'žluťoučký', '000123'), |
| 306 | + (16, 6, CAST(x'610062' AS TEXT), '000123'), |
| 307 | + (17, 7, CAST(x'FF80FE61' AS TEXT), '000123'), |
| 308 | + (18, 8, x'00FF80FE61', '000123'), |
| 309 | + (19, 9, 123, '000123'), |
| 310 | + (20, 10, 1.25, '000123')" |
| 311 | + ); |
| 312 | + } |
| 313 | + |
| 314 | + private function getTableValues( string $column ): array { |
| 315 | + $quoted_column = $this->engine->get_connection()->quote_identifier( $column ); |
| 316 | + return $this->sqlite->query( |
| 317 | + "SELECT rowid, id, hex($quoted_column), $quoted_column IS NULL, note, typeof(note) FROM t ORDER BY rowid" |
| 318 | + )->fetchAll( PDO::FETCH_NUM ); |
| 319 | + } |
| 320 | +} |
0 commit comments