@@ -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