Skip to content

Commit f17b3bf

Browse files
committed
Create ON UPDATE triggers for columns with generated defaults
A column with ON UPDATE CURRENT_TIMESTAMP and an expression default, such as DEFAULT CURRENT_TIMESTAMP or DEFAULT (NOW()), has the EXTRA value "DEFAULT_GENERATED on update CURRENT_TIMESTAMP". The trigger was created only when EXTRA was exactly "on update CURRENT_TIMESTAMP", so these columns never updated, and ALTER COLUMN ... SET DEFAULT (expr) removed a working trigger from a column. The trigger still updates unconditionally, as tracked in the issue below. See #554
1 parent 4bbad21 commit f17b3bf

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7227,7 +7227,7 @@ private function get_sqlite_create_table_statement(
72277227
}
72287228
$rows[] = $query;
72297229

7230-
if ( 'on update CURRENT_TIMESTAMP' === $column['EXTRA'] ) {
7230+
if ( str_contains( $column['EXTRA'], 'on update CURRENT_TIMESTAMP' ) ) {
72317231
$on_update_queries[] = $this->get_column_on_update_trigger_query(
72327232
$table_name,
72337233
$column['COLUMN_NAME']

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,27 @@ public function testColumnWithOnUpdateAndNoIdField() {
20102010
$this->assertRegExp( '/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $result[0]->created_at );
20112011
}
20122012

2013+
public function testColumnWithGeneratedDefaultAndOnUpdate(): void {
2014+
$this->assertQuery(
2015+
'CREATE TABLE _tmp_table (
2016+
id INT,
2017+
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
2018+
updated_at DATETIME NULL DEFAULT (NOW()) ON UPDATE CURRENT_TIMESTAMP
2019+
)'
2020+
);
2021+
$result = $this->assertQuery( 'SHOW COLUMNS FROM _tmp_table' );
2022+
$this->assertSame( 'DEFAULT_GENERATED on update CURRENT_TIMESTAMP', $result[1]->Extra );
2023+
$this->assertSame( 'DEFAULT_GENERATED on update CURRENT_TIMESTAMP', $result[2]->Extra );
2024+
2025+
$this->assertQuery( "INSERT INTO _tmp_table VALUES (1, '2000-01-01 00:00:00', '2000-01-01 00:00:00')" );
2026+
$this->assertQuery( 'UPDATE _tmp_table SET id = 2 WHERE id = 1' );
2027+
$result = $this->assertQuery( 'SELECT * FROM _tmp_table WHERE id = 2' );
2028+
$this->assertRegExp( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $result[0]->created_at );
2029+
$this->assertRegExp( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $result[0]->updated_at );
2030+
$this->assertNotSame( '2000-01-01 00:00:00', $result[0]->created_at );
2031+
$this->assertNotSame( '2000-01-01 00:00:00', $result[0]->updated_at );
2032+
}
2033+
20132034
public function testChangeColumnWithOnUpdate() {
20142035
// CREATE TABLE with ON UPDATE
20152036
$this->assertQuery(
@@ -2716,6 +2737,11 @@ public function testAlterColumnDefaultReplacesExpressionAndPreservesExtra(): voi
27162737
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
27172738
$result = $this->assertQuery( 'SELECT value FROM t' );
27182739
$this->assertRegExp( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $result[0]->value );
2740+
$this->assertQuery( "INSERT INTO t (id, value) VALUES (10, '2000-01-01 00:00:00')" );
2741+
$this->assertQuery( 'UPDATE t SET id = 11 WHERE id = 10' );
2742+
$result = $this->assertQuery( 'SELECT value FROM t WHERE id = 11' );
2743+
$this->assertRegExp( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $result[0]->value );
2744+
$this->assertNotSame( '2000-01-01 00:00:00', $result[0]->value );
27192745
$this->assertQuery( 'ALTER TABLE t ALTER value DROP DEFAULT' );
27202746
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
27212747
$this->assertNull( $result[1]->Default );

0 commit comments

Comments
 (0)