@@ -145,18 +145,22 @@ function ( $row ) use ( $now, $after ) {
145145 *
146146 * @param string $table The table name.
147147 * @param string $column The column name.
148+ * @param bool $legacy Whether to use the legacy driver's trigger name.
148149 */
149- private function createOnUpdateTrigger( string $table, string $column ): void {
150+ private function createOnUpdateTrigger( string $table, string $column, bool $legacy = false ): void {
151+ $connection = $this->engine->get_connection();
152+ $trigger_name = $legacy ? "__{$table}_{$column}_on_update__" : "_wp_sqlite_{$table}_{$column}_on_update";
150153 $this->sqlite->exec(
151154 sprintf(
152- 'CREATE TRIGGER `_wp_sqlite_ %1$s_%2$s_on_update`
153- AFTER UPDATE ON `%1$s`
155+ 'CREATE TRIGGER %1$s
156+ AFTER UPDATE ON %2$s
154157 FOR EACH ROW
155158 BEGIN
156- UPDATE `%1$s` SET `%2$s` = CURRENT_TIMESTAMP WHERE rowid = NEW.rowid;
159+ UPDATE %2$s SET %3$s = CURRENT_TIMESTAMP WHERE rowid = NEW.rowid;
157160 END',
158- $table,
159- $column
161+ $connection->quote_identifier( $trigger_name ),
162+ $connection->quote_identifier( $table ),
163+ $connection->quote_identifier( $column )
160164 )
161165 );
162166 }
@@ -7294,6 +7298,222 @@ public static function migrationDriverVersions(): array {
72947298 );
72957299 }
72967300
7301+ public function testConfigurationDropsOnUpdateCurrentTimestampTriggers(): void {
7302+ $this->assertQuery(
7303+ 'CREATE TABLE _tmp_table (
7304+ id INT PRIMARY KEY,
7305+ n INT,
7306+ updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP
7307+ )'
7308+ );
7309+ $this->assertQuery(
7310+ "INSERT INTO _tmp_table VALUES
7311+ (1, 1, '2000-01-01 00:00:00'),
7312+ (2, 1, '2000-01-01 00:00:00')"
7313+ );
7314+
7315+ // Create the trigger, as older driver versions did, and a user trigger.
7316+ $this->createOnUpdateTrigger( '_tmp_table', 'updated_at' );
7317+ $this->sqlite->exec( 'CREATE TRIGGER `_tmp_table_updated_at_on_update` AFTER UPDATE ON `_tmp_table` BEGIN SELECT 1; END' );
7318+
7319+ // Only the driver trigger is dropped.
7320+ $this->reconnectAfterDriverUpgrade();
7321+ $this->assertSame(
7322+ array( '_tmp_table_updated_at_on_update' ),
7323+ $this->sqlite->query( "SELECT name FROM sqlite_master WHERE type = 'trigger'" )->fetchAll( PDO::FETCH_COLUMN )
7324+ );
7325+
7326+ // UPDATE emulates ON UPDATE CURRENT_TIMESTAMP as MySQL does.
7327+ $now = gmdate( 'Y-m-d H:i:s' );
7328+ $this->assertQuery( 'UPDATE _tmp_table SET n = 2 WHERE id = 1' );
7329+ $this->assertQuery( 'UPDATE _tmp_table SET n = 1 WHERE id = 2' );
7330+ $this->assertSame(
7331+ array( $now, '2000-01-01 00:00:00' ),
7332+ $this->getOnUpdateTimestamps( 'SELECT updated_at FROM _tmp_table ORDER BY id', $now )
7333+ );
7334+ }
7335+
7336+ /**
7337+ * @dataProvider onUpdateTriggerMigrationDriverVersions
7338+ */
7339+ public function testConfigurationDropsOnUpdateCurrentTimestampTriggersOnlyForOlderDriverVersions(
7340+ string $saved_version,
7341+ array $expected_triggers,
7342+ bool $legacy = false
7343+ ): void {
7344+ $this->assertQuery( 'CREATE TABLE _tmp_table (id INT, updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP)' );
7345+ $this->createOnUpdateTrigger( '_tmp_table', 'updated_at', $legacy );
7346+
7347+ // Configure the database as if it was last configured by the given driver version.
7348+ $this->sqlite->exec(
7349+ sprintf(
7350+ "UPDATE %s SET value = '%s' WHERE name = '%s'",
7351+ WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME,
7352+ $saved_version,
7353+ WP_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME
7354+ )
7355+ );
7356+ $configurator = new WP_SQLite_Configurator(
7357+ $this->engine,
7358+ new WP_SQLite_Information_Schema_Builder( WP_MySQL_On_SQLite::RESERVED_PREFIX, $this->engine->get_connection() )
7359+ );
7360+ $configurator->configure_database();
7361+
7362+ $this->assertSame(
7363+ $expected_triggers,
7364+ $this->sqlite->query( "SELECT name FROM sqlite_master WHERE type = 'trigger'" )->fetchAll( PDO::FETCH_COLUMN )
7365+ );
7366+ }
7367+
7368+ public static function onUpdateTriggerMigrationDriverVersions(): array {
7369+ return array(
7370+ 'migrated from 3.1.1' => array( '3.1.1', array() ),
7371+ 'not migrated from 3.1.2' => array( '3.1.2', array( '_wp_sqlite__tmp_table_updated_at_on_update' ) ),
7372+ 'legacy from 3.1.1' => array( '3.1.1', array(), true ),
7373+ 'legacy from 3.1.2' => array( '3.1.2', array( '___tmp_table_updated_at_on_update__' ), true ),
7374+ );
7375+ }
7376+
7377+ /**
7378+ * @dataProvider legacyOnUpdateTriggerMigrations
7379+ */
7380+ public function testConfigurationMigratesLegacyOnUpdateTriggers( bool $already_reconstructed, string $rebuild_query ): void {
7381+ // Reproduce the table, type cache, and triggers created by the legacy driver.
7382+ $this->sqlite->exec(
7383+ 'CREATE TABLE _mysql_data_types_cache (
7384+ `table` TEXT NOT NULL,
7385+ `column_or_index` TEXT NOT NULL,
7386+ `mysql_type` TEXT NOT NULL,
7387+ PRIMARY KEY (`table`, `column_or_index`)
7388+ )'
7389+ );
7390+ $this->sqlite->exec(
7391+ "INSERT INTO _mysql_data_types_cache VALUES
7392+ ('legacy_timestamps', 'id', 'int'),
7393+ ('legacy_timestamps', 'n', 'int'),
7394+ ('legacy_timestamps', 'zero_default', 'timestamp'),
7395+ ('legacy_timestamps', 'generated_default', 'datetime'),
7396+ ('legacy_timestamps', 'nullable', 'timestamp')"
7397+ );
7398+ $this->sqlite->exec(
7399+ 'CREATE TABLE legacy_timestamps (
7400+ id INTEGER PRIMARY KEY,
7401+ n INTEGER,
7402+ zero_default TEXT NOT NULL DEFAULT 0,
7403+ generated_default TEXT DEFAULT CURRENT_TIMESTAMP,
7404+ nullable TEXT
7405+ )'
7406+ );
7407+ foreach ( range( 1, 5 ) as $id ) {
7408+ $this->sqlite->exec(
7409+ "INSERT INTO legacy_timestamps VALUES ($id, 1, '2000-01-01 00:00:00', '2000-01-01 00:00:00', '2000-01-01 00:00:00')"
7410+ );
7411+ }
7412+ $this->sqlite->exec( 'UPDATE legacy_timestamps SET zero_default = 0 WHERE id = 5' );
7413+ foreach ( array( 'zero_default', 'generated_default', 'nullable' ) as $column ) {
7414+ $this->createOnUpdateTrigger( 'legacy_timestamps', $column, true );
7415+ }
7416+
7417+ if ( $already_reconstructed ) {
7418+ // Earlier AST versions reconstructed the table but missed ON UPDATE metadata.
7419+ $reconstructor = new WP_SQLite_Information_Schema_Reconstructor(
7420+ $this->engine,
7421+ new WP_SQLite_Information_Schema_Builder( WP_MySQL_On_SQLite::RESERVED_PREFIX, $this->engine->get_connection() )
7422+ );
7423+ $reconstructor->ensure_correct_information_schema();
7424+ }
7425+
7426+ // Recover ON UPDATE before the zero-default migration rebuilds the table.
7427+ $this->reconnectAfterDriverUpgrade();
7428+ $this->assertQuery( 'SELECT zero_default, generated_default, nullable FROM legacy_timestamps WHERE id = 5' );
7429+ $this->assertEquals(
7430+ array(
7431+ (object) array(
7432+ 'zero_default' => '0000-00-00 00:00:00',
7433+ 'generated_default' => '2000-01-01 00:00:00',
7434+ 'nullable' => '2000-01-01 00:00:00',
7435+ ),
7436+ ),
7437+ $this->last_result
7438+ );
7439+ $this->assertSame(
7440+ array(),
7441+ $this->sqlite->query( "SELECT name FROM sqlite_master WHERE type = 'trigger'" )->fetchAll( PDO::FETCH_COLUMN )
7442+ );
7443+
7444+ // Repeated configuration must preserve other EXTRA flags without duplicating ON UPDATE.
7445+ $this->reconnectAfterDriverUpgrade();
7446+ $this->assertQuery( "SELECT extra FROM information_schema.columns WHERE table_name = 'legacy_timestamps' AND column_name != 'id' AND column_name != 'n' ORDER BY ordinal_position" );
7447+ $this->assertSame(
7448+ array( 'on update CURRENT_TIMESTAMP', 'DEFAULT_GENERATED on update CURRENT_TIMESTAMP', 'on update CURRENT_TIMESTAMP' ),
7449+ array_column( $this->last_result, 'EXTRA' )
7450+ );
7451+
7452+ if ( '' !== $rebuild_query ) {
7453+ $this->assertQuery( $rebuild_query );
7454+ }
7455+ $this->assertQuery( 'SHOW CREATE TABLE legacy_timestamps' );
7456+ $this->assertSame( 3, substr_count( $this->last_result[0]->{'Create Table'}, 'ON UPDATE CURRENT_TIMESTAMP' ) );
7457+
7458+ $now = gmdate( 'Y-m-d H:i:s' );
7459+ $this->assertQuery( 'UPDATE legacy_timestamps SET n = 2 WHERE id IN (1, 5)' );
7460+ $this->assertQuery( 'UPDATE legacy_timestamps SET n = n WHERE id = 2' );
7461+ $this->assertQuery( 'UPDATE legacy_timestamps SET n = 2, zero_default = zero_default, generated_default = generated_default, nullable = nullable WHERE id = 3' );
7462+ $this->assertQuery( "UPDATE legacy_timestamps SET zero_default = '2001-01-01 00:00:00', generated_default = '2001-01-01 00:00:00', nullable = '2001-01-01 00:00:00' WHERE id = 4" );
7463+ foreach ( array( 'zero_default', 'generated_default', 'nullable' ) as $column ) {
7464+ $this->assertSame(
7465+ array( $now, '2000-01-01 00:00:00', '2000-01-01 00:00:00', '2001-01-01 00:00:00', $now ),
7466+ $this->getOnUpdateTimestamps( "SELECT $column FROM legacy_timestamps ORDER BY id", $now )
7467+ );
7468+ }
7469+ }
7470+
7471+ public static function legacyOnUpdateTriggerMigrations(): array {
7472+ $cases = array();
7473+ foreach ( array( false, true ) as $already_reconstructed ) {
7474+ foreach ( array( '', 'ALTER TABLE legacy_timestamps ADD COLUMN note TEXT', 'OPTIMIZE TABLE legacy_timestamps', 'REPAIR TABLE legacy_timestamps' ) as $query ) {
7475+ $name = ( $already_reconstructed ? 'existing metadata: ' : 'reconstructed metadata: ' ) . $query;
7476+ $cases[ $name ] = array( $already_reconstructed, $query );
7477+ }
7478+ }
7479+ return $cases;
7480+ }
7481+
7482+ public function testConfigurationMigratesLegacyOnUpdateTriggersWithQuotedNames(): void {
7483+ $table = 'Legacy_`"table';
7484+ $column = 'Updated_`"at';
7485+ $connection = $this->engine->get_connection();
7486+ $quoted_table = $connection->quote_identifier( $table );
7487+ $quoted_column = $connection->quote_identifier( $column );
7488+ $this->assertQuery( "CREATE TABLE $quoted_table (id INT, $quoted_column TIMESTAMP NULL)" );
7489+ $this->assertQuery( "INSERT INTO $quoted_table VALUES (1, '2000-01-01 00:00:00')" );
7490+ $this->createOnUpdateTrigger( strtoupper( $table ), strtoupper( $column ), true );
7491+ $this->createOnUpdateTrigger( $table, $column );
7492+
7493+ // Similar names and a matching name on a different table are user triggers.
7494+ $this->sqlite->exec( "CREATE TRIGGER `__legacy_user_on_update__suffix` AFTER UPDATE ON $quoted_table BEGIN SELECT 1; END" );
7495+ $this->assertQuery( 'CREATE TABLE other_table (updated_at TIMESTAMP NULL)' );
7496+ $this->sqlite->exec( "CREATE TRIGGER `__other_table_updated_at_on_update__` AFTER UPDATE ON $quoted_table BEGIN SELECT 1; END" );
7497+
7498+ // A temporary trigger with the same name must not hide the main trigger.
7499+ $temp_trigger = $connection->quote_identifier( '__' . strtoupper( $table ) . '_' . strtoupper( $column ) . '_on_update__' );
7500+ $this->sqlite->exec( "CREATE TEMP TRIGGER $temp_trigger AFTER UPDATE ON $quoted_table BEGIN SELECT 1; END" );
7501+ $this->reconnectAfterDriverUpgrade();
7502+ $this->assertSame(
7503+ array( '__legacy_user_on_update__suffix', '__other_table_updated_at_on_update__' ),
7504+ $this->sqlite->query( "SELECT name FROM sqlite_master WHERE type = 'trigger' ORDER BY name" )->fetchAll( PDO::FETCH_COLUMN )
7505+ );
7506+ $this->assertSame( '1', (string) $this->sqlite->query( "SELECT COUNT(*) FROM sqlite_temp_master WHERE type = 'trigger'" )->fetchColumn() );
7507+ $this->assertQuery( "SELECT extra FROM information_schema.columns WHERE table_name = 'other_table'" );
7508+ $this->assertSame( '', $this->last_result[0]->EXTRA );
7509+
7510+ $now = gmdate( 'Y-m-d H:i:s' );
7511+ $this->assertQuery( "UPDATE $quoted_table SET id = 2" );
7512+ $this->assertSame( array( $now ), $this->getOnUpdateTimestamps( "SELECT $quoted_column FROM $quoted_table", $now ) );
7513+ $this->assertQuery( "UPDATE $quoted_table SET $quoted_column = '2001-01-01 00:00:00'" );
7514+ $this->assertSame( array( '2001-01-01 00:00:00' ), $this->getOnUpdateTimestamps( "SELECT $quoted_column FROM $quoted_table", $now ) );
7515+ }
7516+
72977517 public function testConfigurationNormalizesStoredTemporalZeroDefaultsOfReferencedTable(): void {
72987518 $this->assertQuery( 'CREATE TABLE _tmp_parent (id INT PRIMARY KEY, created_at TIMESTAMP NOT NULL DEFAULT 0)' );
72997519 $this->assertQuery( 'CREATE TABLE _tmp_cascade (id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES _tmp_parent (id) ON DELETE CASCADE)' );
@@ -7482,7 +7702,9 @@ public function testConfigurationRollsBackWhenRecreatingTableFails(): void {
74827702 // Make recreating the second table fail, as its data doesn't match the information schema.
74837703 $this->sqlite->exec( "UPDATE _wp_sqlite_mysql_information_schema_columns SET is_nullable = 'NO' WHERE table_name = '_tmp_failing' AND column_name = 'id'" );
74847704
7485- $sqlite_schema_query = "SELECT sql FROM sqlite_master WHERE name IN ('_tmp_recreated', '_tmp_failing') ORDER BY name";
7705+ // Trigger removal and recovered ON UPDATE metadata must also roll back.
7706+ $this->createOnUpdateTrigger( '_tmp_recreated', 'created_at', true );
7707+ $sqlite_schema_query = "SELECT sql FROM sqlite_master WHERE tbl_name IN ('_tmp_recreated', '_tmp_failing') ORDER BY name";
74867708 $sqlite_schema = $this->sqlite->query( $sqlite_schema_query )->fetchAll( PDO::FETCH_COLUMN );
74877709
74887710 $exception = null;
@@ -7498,16 +7720,18 @@ public function testConfigurationRollsBackWhenRecreatingTableFails(): void {
74987720 // All configuration changes are rolled back, including the recreated table.
74997721 $this->assertSame( '0.0.0', $this->engine->get_saved_driver_version() );
75007722 $this->assertSame( $sqlite_schema, $this->sqlite->query( $sqlite_schema_query )->fetchAll( PDO::FETCH_COLUMN ) );
7501- $this->assertQuery( "SELECT table_name, column_default FROM information_schema.columns WHERE column_name = 'created_at' ORDER BY table_name" );
7723+ $this->assertQuery( "SELECT table_name, column_default, extra FROM information_schema.columns WHERE column_name = 'created_at' ORDER BY table_name" );
75027724 $this->assertEquals(
75037725 array(
75047726 (object) array(
75057727 'TABLE_NAME' => '_tmp_failing',
75067728 'COLUMN_DEFAULT' => '0',
7729+ 'EXTRA' => '',
75077730 ),
75087731 (object) array(
75097732 'TABLE_NAME' => '_tmp_recreated',
75107733 'COLUMN_DEFAULT' => '0',
7734+ 'EXTRA' => '',
75117735 ),
75127736 ),
75137737 $this->last_result
0 commit comments