From 52100ba1272b37b0336bb71c09a95278a1fec1bb Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Thu, 23 Aug 2018 21:13:10 +0900 Subject: [PATCH] Issue #2994561 by andypost: Properly deprecate db_create_table --- core/includes/database.inc | 1 + .../Core/Database/DatabaseLegacyTest.php | 21 +++++++++++++++++++ .../Core/Database/TransactionTest.php | 4 ++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/core/includes/database.inc b/core/includes/database.inc index 1b60ca1d82b..0aa1dabcda5 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -589,6 +589,7 @@ function db_condition($conjunction) { * @see \Drupal\Core\Database\Schema::createTable() */ function db_create_table($name, $table) { + @trigger_error('db_create_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call createTable() on it. For example, $injected_database->schema()->createTable($name, $table). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED); return Database::getConnection()->schema()->createTable($name, $table); } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php index fa463a115ac..e1c0bf5d2cc 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php @@ -151,4 +151,25 @@ class DatabaseLegacyTest extends DatabaseTestBase { $this->assertSame(['test_field'], db_field_names(['test_field'])); } + /** + * Tests deprecation of the db_create_table() function. + * + * @expectedDeprecation db_create_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call createTable() on it. For example, $injected_database->schema()->createTable($name, $table). See https://www.drupal.org/node/2993033 + */ + public function testDbCreateTable() { + $name = 'test_create_table'; + $table = [ + 'fields' => [ + 'id' => [ + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ], + ], + 'primary key' => ['id'], + ]; + db_create_table($name, $table); + $this->assertTrue($this->connection->schema()->tableExists($name)); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php b/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php index 550ba6a876a..0f75d286a98 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php @@ -123,7 +123,7 @@ class TransactionTest extends DatabaseTestBase { ], 'primary key' => ['id'], ]; - db_create_table('database_test_1', $table); + $this->connection->schema()->createTable('database_test_1', $table); $this->assertTrue($this->connection->inTransaction(), 'In transaction inside nested transaction.'); } @@ -330,7 +330,7 @@ class TransactionTest extends DatabaseTestBase { ], 'primary key' => ['id'], ]; - db_create_table('database_test_' . ++$count, $table); + $this->connection->schema()->createTable('database_test_' . ++$count, $table); } /**