From 97aa0f7f077bd67221e127d7cb8394904418b135 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Tue, 30 Jul 2024 13:07:00 +0700 Subject: [PATCH] Fix show create returning invalid create statement for NULL columns without default value --- tests/WP_SQLite_Translator_Tests.php | 24 +++++++++++++++++++ .../sqlite/class-wp-sqlite-translator.php | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/WP_SQLite_Translator_Tests.php b/tests/WP_SQLite_Translator_Tests.php index 4118f5e5..cc08c68c 100644 --- a/tests/WP_SQLite_Translator_Tests.php +++ b/tests/WP_SQLite_Translator_Tests.php @@ -457,6 +457,30 @@ public function testShowCreateTableWithColumnKeys() { ); } + public function testShowCreateTableWithCorrectDefaultValues() { + $this->assertQuery( + "CREATE TABLE _tmp__table ( + ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL, + default_empty_string VARCHAR(255) default '', + null_no_default VARCHAR(255), + );" + ); + + $this->assertQuery( + 'SHOW CREATE TABLE _tmp__table;' + ); + $results = $this->engine->get_query_results(); + $this->assertEquals( + 'CREATE TABLE `_tmp__table` ( + `ID` bigint NOT NULL AUTO_INCREMENT, + `default_empty_string` varchar(255) DEFAULT \'\', + `null_no_default` varchar(255), + PRIMARY KEY (`ID`) +);', + $results[0]->{'Create Table'} + ); + } + public function testSelectIndexHintForce() { $this->assertQuery( "INSERT INTO _options (option_name) VALUES ('first');" ); $result = $this->assertQuery( diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index 2730d857..133e984e 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -3522,7 +3522,7 @@ protected function get_column_definitions( $table_name, $columns ) { $definition[] = 'NOT NULL'; } - if ( '' !== $column->dflt_value && ! $is_auto_incr ) { + if ( null !== $column->dflt_value && '' !== $column->dflt_value && ! $is_auto_incr ) { $definition[] = 'DEFAULT ' . $column->dflt_value; }