Skip to content

Commit 433fd76

Browse files
committed
Repair temporal zero values stored in existing rows
Rows that got a zero default from older driver versions, such as "0" in a TIMESTAMP column, kept that value after the defaults were repaired. MySQL stores the zero value in these rows, and a dump of them doesn't import into MySQL in strict SQL mode. When repairing the defaults, replace the old default in existing rows with the zero value of the column. The table triggers are dropped first, so that the update doesn't run them, e.g., to emulate ON UPDATE CURRENT_TIMESTAMP. Recreating the table drops them anyway, and it creates the driver's triggers again.
1 parent a4c57c7 commit 433fd76

2 files changed

Lines changed: 170 additions & 40 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-configurator.php‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,16 @@ private function migrate_datetime_precision(): void {
366366
* Migrate zero defaults of temporal columns stored by older driver versions.
367367
*
368368
* Older driver versions stored defaults such as "DEFAULT 0" of a TIMESTAMP
369-
* column verbatim, while MySQL stores them as a zero date/time value.
369+
* column verbatim, while MySQL stores them as a zero date/time value. Rows
370+
* that got such a default store it as well, so they get the zero value too.
370371
*
371372
* The columns are found by their SQLite defaults, as the information schema
372373
* of a table reconstructed from the legacy SQLite driver already has a zero
373374
* value, while the SQLite table still stores "0".
374375
*/
375376
private function migrate_temporal_zero_defaults(): void {
376-
$columns_table = $this->driver->get_connection()->quote_identifier(
377+
$connection = $this->driver->get_connection();
378+
$columns_table = $connection->quote_identifier(
377379
$this->schema_builder->get_table_name( false, 'columns' )
378380
);
379381
$columns = $this->driver->execute_sqlite_query(
@@ -415,14 +417,52 @@ private function migrate_temporal_zero_defaults(): void {
415417
}
416418

417419
$table_columns[ $column['TABLE_NAME'] ][] = array(
418-
'name' => $column['COLUMN_NAME'],
419-
'zero_value' => $zero_value,
420+
'name' => $column['COLUMN_NAME'],
421+
'zero_value' => $zero_value,
422+
'old_default' => $default,
423+
'is_string' => $is_string,
420424
);
421425
}
422426

423427
foreach ( $table_columns as $table_name => $repaired_columns ) {
428+
/*
429+
* Drop the table triggers, so that updating its rows doesn't run them,
430+
* e.g., to emulate ON UPDATE CURRENT_TIMESTAMP. Recreating the table
431+
* drops them anyway, and it creates the driver's triggers again.
432+
*/
433+
$triggers = $this->driver->execute_sqlite_query(
434+
"SELECT name FROM sqlite_master WHERE type = 'trigger' AND tbl_name = ? COLLATE NOCASE",
435+
array( $table_name )
436+
)->fetchAll( PDO::FETCH_COLUMN );
437+
foreach ( $triggers as $trigger ) {
438+
$this->driver->execute_sqlite_query(
439+
sprintf( 'DROP TRIGGER %s', $connection->quote_identifier( $trigger ) )
440+
);
441+
}
442+
424443
$alter_list = array();
425444
foreach ( $repaired_columns as $column ) {
445+
/*
446+
* Replace the old default stored in existing rows with the zero value.
447+
* Rows store the old default after the column affinity converted it,
448+
* e.g., -0 as '0' in a TEXT column. An SQL comparison converts it the
449+
* same way, so a number is compared as is, as it was checked to be
450+
* numeric, and a string as a parameter.
451+
*/
452+
$quoted_column = $connection->quote_identifier( $column['name'] );
453+
$this->driver->execute_sqlite_query(
454+
sprintf(
455+
'UPDATE %s SET %s = ? WHERE %s = %s',
456+
$connection->quote_identifier( $table_name ),
457+
$quoted_column,
458+
$quoted_column,
459+
$column['is_string'] ? '?' : $column['old_default']
460+
),
461+
$column['is_string']
462+
? array( $column['zero_value'], $column['old_default'] )
463+
: array( $column['zero_value'] )
464+
);
465+
426466
$alter_list[] = sprintf(
427467
'ALTER COLUMN `%s` SET DEFAULT %s',
428468
str_replace( '`', '``', $column['name'] ),

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 126 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6654,7 +6654,8 @@ public function testConfigurationNormalizesStoredTemporalZeroDefaults(): void {
66546654
);
66556655

66566656
// Store the defaults verbatim and a zero precision, as older driver versions
6657-
// did, and apply them to the SQLite table. Then, insert a row with them.
6657+
// did, and apply them to the SQLite table. Then, insert a row with them, and
6658+
// a row with other values.
66586659
$this->sqlite->exec(
66596660
"UPDATE _wp_sqlite_mysql_information_schema_columns
66606661
SET
@@ -6664,6 +6665,10 @@ public function testConfigurationNormalizesStoredTemporalZeroDefaults(): void {
66646665
);
66656666
$this->assertQuery( 'OPTIMIZE TABLE _tmp_table' );
66666667
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (1)' );
6668+
$this->assertQuery(
6669+
"INSERT INTO _tmp_table (id, created_at, created_at_fsp, updated_on, duration)
6670+
VALUES (2, '2020-01-02 03:04:05', '2020-01-02 03:04:05', '2020-01-02', '01:02:03')"
6671+
);
66676672

66686673
$this->reconnectAfterDriverUpgrade();
66696674

@@ -6687,34 +6692,54 @@ public function testConfigurationNormalizesStoredTemporalZeroDefaults(): void {
66876692
$this->assertQuery( 'SHOW CREATE TABLE _tmp_table' );
66886693
$this->assertSame( $expected_create_table, $this->last_result[0]->{'Create Table'} );
66896694

6690-
// New rows get the normalized defaults in both strict and non-strict SQL
6691-
// mode. The existing row keeps its values.
6692-
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (2)' );
6693-
$this->assertQuery( "SET sql_mode = ''" );
6695+
// The existing row with the old defaults gets the zero values, while the
6696+
// other existing row is unchanged. New rows get the normalized defaults
6697+
// in both strict and non-strict SQL mode.
66946698
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (3)' );
6695-
$this->assertQuery( 'SELECT id, created_at, created_at_fsp, updated_on, duration FROM _tmp_table ORDER BY id' );
6699+
$this->assertQuery( "SET sql_mode = ''" );
6700+
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (4)' );
6701+
$expected_rows_query = 'SELECT id, created_at, created_at_fsp, updated_on, duration FROM _tmp_table ORDER BY id';
6702+
$expected_rows = array(
6703+
(object) array(
6704+
'id' => '1',
6705+
'created_at' => '0000-00-00 00:00:00',
6706+
'created_at_fsp' => '0000-00-00 00:00:00.000',
6707+
'updated_on' => '0000-00-00',
6708+
'duration' => '00:00:00.00',
6709+
),
6710+
(object) array(
6711+
'id' => '2',
6712+
'created_at' => '2020-01-02 03:04:05',
6713+
'created_at_fsp' => '2020-01-02 03:04:05',
6714+
'updated_on' => '2020-01-02',
6715+
'duration' => '01:02:03',
6716+
),
6717+
(object) array(
6718+
'id' => '3',
6719+
'created_at' => '0000-00-00 00:00:00',
6720+
'created_at_fsp' => '0000-00-00 00:00:00.000',
6721+
'updated_on' => '0000-00-00',
6722+
'duration' => '00:00:00.00',
6723+
),
6724+
(object) array(
6725+
'id' => '4',
6726+
'created_at' => '0000-00-00 00:00:00',
6727+
'created_at_fsp' => '0000-00-00 00:00:00.000',
6728+
'updated_on' => '0000-00-00',
6729+
'duration' => '00:00:00.00',
6730+
),
6731+
);
6732+
$this->assertQuery( $expected_rows_query );
6733+
$this->assertEquals( $expected_rows, $this->last_result );
6734+
6735+
// The existing row keeps the YEAR and expression values, as their defaults are not normalized.
6736+
$this->assertQuery( 'SELECT year_number, year_string, expression FROM _tmp_table WHERE id = 1' );
66966737
$this->assertEquals(
66976738
array(
66986739
(object) array(
6699-
'id' => '1',
6700-
'created_at' => '0',
6701-
'created_at_fsp' => '0',
6702-
'updated_on' => '0',
6703-
'duration' => '0',
6704-
),
6705-
(object) array(
6706-
'id' => '2',
6707-
'created_at' => '0000-00-00 00:00:00',
6708-
'created_at_fsp' => '0000-00-00 00:00:00.000',
6709-
'updated_on' => '0000-00-00',
6710-
'duration' => '00:00:00.00',
6711-
),
6712-
(object) array(
6713-
'id' => '3',
6714-
'created_at' => '0000-00-00 00:00:00',
6715-
'created_at_fsp' => '0000-00-00 00:00:00.000',
6716-
'updated_on' => '0000-00-00',
6717-
'duration' => '00:00:00.00',
6740+
'year_number' => '0',
6741+
'year_string' => '0',
6742+
'expression' => '0',
67186743
),
67196744
),
67206745
$this->last_result
@@ -6724,8 +6749,41 @@ public function testConfigurationNormalizesStoredTemporalZeroDefaults(): void {
67246749
$this->reconnectAfterDriverUpgrade();
67256750
$this->assertQuery( 'SHOW CREATE TABLE _tmp_table' );
67266751
$this->assertSame( $expected_create_table, $this->last_result[0]->{'Create Table'} );
6727-
$this->assertQuery( 'SELECT COUNT(*) AS count FROM _tmp_table' );
6728-
$this->assertSame( '3', $this->last_result[0]->count );
6752+
$this->assertQuery( $expected_rows_query );
6753+
$this->assertEquals( $expected_rows, $this->last_result );
6754+
}
6755+
6756+
public function testConfigurationNormalizesStoredTemporalZeroValuesWithoutRunningTriggers(): void {
6757+
$this->assertQuery(
6758+
'CREATE TABLE _tmp_table (
6759+
id INT NOT NULL,
6760+
created_at TIMESTAMP NOT NULL DEFAULT 0,
6761+
updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP
6762+
)'
6763+
);
6764+
6765+
// Store the default verbatim, as older driver versions did, and apply it to the SQLite table.
6766+
$this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET column_default = '0' WHERE table_name = '_tmp_table' AND column_name = 'created_at'" );
6767+
$this->assertQuery( 'OPTIMIZE TABLE _tmp_table' );
6768+
$this->assertQuery( "INSERT INTO _tmp_table (id, updated_at) VALUES (1, '2020-01-02 03:04:05')" );
6769+
6770+
// Updating the stored zero doesn't run the ON UPDATE CURRENT_TIMESTAMP trigger.
6771+
$this->reconnectAfterDriverUpgrade();
6772+
$this->assertQuery( 'SELECT created_at, updated_at FROM _tmp_table' );
6773+
$this->assertEquals(
6774+
array(
6775+
(object) array(
6776+
'created_at' => '0000-00-00 00:00:00',
6777+
'updated_at' => '2020-01-02 03:04:05',
6778+
),
6779+
),
6780+
$this->last_result
6781+
);
6782+
6783+
// The recreated table has the trigger again.
6784+
$this->assertQuery( 'UPDATE _tmp_table SET id = 2' );
6785+
$this->assertQuery( 'SELECT updated_at FROM _tmp_table' );
6786+
$this->assertNotSame( '2020-01-02 03:04:05', $this->last_result[0]->updated_at );
67296787
}
67306788

67316789
/**
@@ -6856,7 +6914,10 @@ public function testConfigurationNormalizesTemporalZeroDefaultsOfReconstructedTa
68566914
('_tmp_table', 'year_number', 'year'),
68576915
('_tmp_table', 'year_string', 'year'),
68586916
('_tmp_double_quoted', 'id', 'int'),
6859-
('_tmp_double_quoted', 'created_at', 'timestamp')"
6917+
('_tmp_double_quoted', 'created_at', 'timestamp'),
6918+
('_tmp_number_forms', 'id', 'int'),
6919+
('_tmp_number_forms', 'negative_zero', 'datetime'),
6920+
('_tmp_number_forms', 'decimal_zero', 'datetime')"
68606921
);
68616922
$this->sqlite->exec(
68626923
"CREATE TABLE _tmp_table (
@@ -6872,6 +6933,11 @@ public function testConfigurationNormalizesTemporalZeroDefaultsOfReconstructedTa
68726933

68736934
// A double-quoted SQLite default is a string as well.
68746935
$this->sqlite->exec( 'CREATE TABLE _tmp_double_quoted (id INTEGER, created_at TEXT NOT NULL DEFAULT "0")' );
6936+
$this->sqlite->exec( 'INSERT INTO _tmp_double_quoted (id) VALUES (1)' );
6937+
6938+
// Rows store numeric defaults as converted by the column affinity, e.g., -0 as '0'.
6939+
$this->sqlite->exec( 'CREATE TABLE _tmp_number_forms (id INTEGER, negative_zero TEXT NOT NULL DEFAULT -0, decimal_zero TEXT NOT NULL DEFAULT 0.0)' );
6940+
$this->sqlite->exec( 'INSERT INTO _tmp_number_forms (id) VALUES (1)' );
68756941

68766942
// The information schema is reconstructed, and then the defaults are normalized.
68776943
// A string zero of a YEAR column, such as '0', is not normalized.
@@ -6894,17 +6960,18 @@ public function testConfigurationNormalizesTemporalZeroDefaultsOfReconstructedTa
68946960
$this->last_result[0]->{'Create Table'}
68956961
);
68966962

6897-
// New rows get the normalized defaults. The existing row keeps its values.
6963+
// The existing row gets the zero values, except for the YEAR string zero.
6964+
// New rows get the normalized defaults.
68986965
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (2)' );
68996966
$this->assertQuery( 'SELECT * FROM _tmp_table ORDER BY id' );
69006967
$this->assertEquals(
69016968
array(
69026969
(object) array(
69036970
'id' => '1',
6904-
'created_at' => '0',
6905-
'created_at_fsp' => '0',
6906-
'updated_on' => '0',
6907-
'year_number' => '0',
6971+
'created_at' => '0000-00-00 00:00:00',
6972+
'created_at_fsp' => '0000-00-00 00:00:00.000',
6973+
'updated_on' => '0000-00-00',
6974+
'year_number' => '0000',
69086975
'year_string' => '0',
69096976
),
69106977
(object) array(
@@ -6919,9 +6986,32 @@ public function testConfigurationNormalizesTemporalZeroDefaultsOfReconstructedTa
69196986
$this->last_result
69206987
);
69216988

6922-
$this->assertQuery( 'INSERT INTO _tmp_double_quoted (id) VALUES (1)' );
6923-
$this->assertQuery( 'SELECT created_at FROM _tmp_double_quoted' );
6924-
$this->assertSame( '0000-00-00 00:00:00', $this->last_result[0]->created_at );
6989+
$this->assertQuery( 'INSERT INTO _tmp_double_quoted (id) VALUES (2)' );
6990+
$this->assertQuery( 'SELECT id, created_at FROM _tmp_double_quoted ORDER BY id' );
6991+
$this->assertEquals(
6992+
array(
6993+
(object) array(
6994+
'id' => '1',
6995+
'created_at' => '0000-00-00 00:00:00',
6996+
),
6997+
(object) array(
6998+
'id' => '2',
6999+
'created_at' => '0000-00-00 00:00:00',
7000+
),
7001+
),
7002+
$this->last_result
7003+
);
7004+
7005+
$this->assertQuery( 'SELECT negative_zero, decimal_zero FROM _tmp_number_forms' );
7006+
$this->assertEquals(
7007+
array(
7008+
(object) array(
7009+
'negative_zero' => '0000-00-00 00:00:00',
7010+
'decimal_zero' => '0000-00-00 00:00:00',
7011+
),
7012+
),
7013+
$this->last_result
7014+
);
69257015
}
69267016

69277017
public function testConfigurationRollsBackWhenRecreatingTableFails(): void {

0 commit comments

Comments
 (0)