diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php index 6d6e6edc1f3..08a53c09af6 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php @@ -505,11 +505,9 @@ protected function introspectSchema($table) { 'type' => $type, 'size' => $size, 'not null' => !empty($row->notnull) || $row->pk !== "0", - // @todo decide whether this more correct change is better than ?? ''. - // The problem with the current code is that an empty string as a - // default value is not the same as a NULL but we current make it - // the same thing. - 'default' => $row->dflt_value !== NULL ? trim($row->dflt_value, "'") : NULL, + // Only trim string values. Other values such as NULLs should be + // preserved. + 'default' => is_string($row->dflt_value) ? trim($row->dflt_value, "'") : $row->dflt_value, ]; if ($length) { $schema['fields'][$row->name]['length'] = $length; diff --git a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php index eb8ea41d376..cfe6386d0b4 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php @@ -1276,4 +1276,39 @@ public function testUpperCaseTableName() { $this->assertTrue($this->schema->dropTable($table_name), 'Table with uppercase table name dropped'); } + /** + * Tests NULL default value after altering table. + */ + public function testNullDefaultAfterAlter() { + $table_name = 'test_table'; + + // Create the tables. + $table_specification = [ + 'description' => 'Test table.', + 'fields' => [ + 'column1' => [ + 'type' => 'int', + 'default' => NULL, + ], + 'column2' => [ + 'type' => 'varchar', + 'length' => 20, + 'default' => NULL, + ], + ], + ]; + $this->schema->createTable($table_name, $table_specification); + + // Insert a column and check that column 2 has the expected default value. + $this->connection->insert($table_name)->fields(['column1' => 1])->execute(); + $this->assertNull($this->connection->select($table_name, 't')->fields('t', ['column2'])->condition('column1', 1)->execute()->fetchField()); + + // Force SQLite schema to create a new table and copy data. + $this->schema->addField('test_table', 'column3', ['type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'description' => 'Added column 3.', 'initial' => 'test']); + + // Test that the column 2 default NULL value still works. + $this->connection->insert($table_name)->fields(['column1' => 2, 'column3' => 'value'])->execute(); + $this->assertNull($this->connection->select($table_name, 't')->fields('t', ['column2'])->condition('column1', 2)->execute()->fetchField()); + } + }