From 5e582a62c4f76e31e385ffa76c1b7c8c035ccc60 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Mon, 17 Sep 2018 11:05:56 +0100 Subject: [PATCH] Issue #2999588 by mondrake, longwave, voleger: Properly deprecate db_index_exists --- core/includes/database.inc | 1 + .../SqlContentEntityStorageSchemaIndexTest.php | 15 +++++++++------ .../src/Functional/Update/UpdateSchemaTest.php | 7 +++++-- .../Update/UpdatePathTestBaseTest.php | 7 +++++-- .../Core/Database/DatabaseLegacyTest.php | 9 +++++++++ .../KernelTests/Core/Database/RegressionTest.php | 6 +++--- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/core/includes/database.inc b/core/includes/database.inc index 23276a43db5..0ed50818b43 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -650,6 +650,7 @@ function db_field_names($fields) { * @see \Drupal\Core\Database\Schema::indexExists() */ function db_index_exists($table, $name) { + @trigger_error('db_index_exists() 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 indexExists() on it. For example, $injected_database->schema()->indexExists($table, $name). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED); return Database::getConnection()->schema()->indexExists($table, $name); } diff --git a/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaIndexTest.php b/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaIndexTest.php index 408493f9048..e3de0e0c153 100644 --- a/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaIndexTest.php +++ b/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaIndexTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\system\Functional\Entity\Update; +use Drupal\Core\Database\Database; use Drupal\FunctionalTests\Update\UpdatePathTestBase; /** @@ -25,19 +26,21 @@ class SqlContentEntityStorageSchemaIndexTest extends UpdatePathTestBase { * Tests entity and field schema database updates and execution order. */ public function testIndex() { + $schema = Database::getConnection()->schema(); + // The initial Drupal 8 database dump before any updates does not include // the entity ID in the entity field data table indices that were added in // https://www.drupal.org/node/2261669. - $this->assertTrue(db_index_exists('node_field_data', 'node__default_langcode'), 'Index node__default_langcode exists prior to running updates.'); - $this->assertFalse(db_index_exists('node_field_data', 'node__id__default_langcode__langcode'), 'Index node__id__default_langcode__langcode does not exist prior to running updates.'); - $this->assertFalse(db_index_exists('users_field_data', 'user__id__default_langcode__langcode'), 'Index users__id__default_langcode__langcode does not exist prior to running updates.'); + $this->assertTrue($schema->indexExists('node_field_data', 'node__default_langcode'), 'Index node__default_langcode exists prior to running updates.'); + $this->assertFalse($schema->indexExists('node_field_data', 'node__id__default_langcode__langcode'), 'Index node__id__default_langcode__langcode does not exist prior to running updates.'); + $this->assertFalse($schema->indexExists('users_field_data', 'user__id__default_langcode__langcode'), 'Index users__id__default_langcode__langcode does not exist prior to running updates.'); // Running database updates should update the entity schemata to add the // indices from https://www.drupal.org/node/2261669. $this->runUpdates(); - $this->assertFalse(db_index_exists('node_field_data', 'node__default_langcode'), 'Index node__default_langcode properly removed.'); - $this->assertTrue(db_index_exists('node_field_data', 'node__id__default_langcode__langcode'), 'Index node__id__default_langcode__langcode properly created on the node_field_data table.'); - $this->assertTrue(db_index_exists('users_field_data', 'user__id__default_langcode__langcode'), 'Index users__id__default_langcode__langcode properly created on the user_field_data table.'); + $this->assertFalse($schema->indexExists('node_field_data', 'node__default_langcode'), 'Index node__default_langcode properly removed.'); + $this->assertTrue($schema->indexExists('node_field_data', 'node__id__default_langcode__langcode'), 'Index node__id__default_langcode__langcode properly created on the node_field_data table.'); + $this->assertTrue($schema->indexExists('users_field_data', 'user__id__default_langcode__langcode'), 'Index users__id__default_langcode__langcode properly created on the user_field_data table.'); } } diff --git a/core/modules/system/tests/src/Functional/Update/UpdateSchemaTest.php b/core/modules/system/tests/src/Functional/Update/UpdateSchemaTest.php index 1c2edee065d..3c9588fd1e2 100644 --- a/core/modules/system/tests/src/Functional/Update/UpdateSchemaTest.php +++ b/core/modules/system/tests/src/Functional/Update/UpdateSchemaTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\system\Functional\Update; +use Drupal\Core\Database\Database; use Drupal\Core\Url; use Drupal\Tests\BrowserTestBase; @@ -44,9 +45,11 @@ class UpdateSchemaTest extends BrowserTestBase { * Tests that update hooks are properly run. */ public function testUpdateHooks() { + $connection = Database::getConnection(); + // Verify that the 8000 schema is in place. $this->assertEqual(drupal_get_installed_schema_version('update_test_schema'), 8000); - $this->assertFalse(db_index_exists('update_test_schema_table', 'test'), 'Version 8000 of the update_test_schema module is installed.'); + $this->assertFalse($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8000 of the update_test_schema module is installed.'); // Increment the schema version. \Drupal::state()->set('update_test_schema_version', 8001); @@ -62,7 +65,7 @@ class UpdateSchemaTest extends BrowserTestBase { // Ensure schema has changed. $this->assertEqual(drupal_get_installed_schema_version('update_test_schema', TRUE), 8001); // Ensure the index was added for column a. - $this->assertTrue(db_index_exists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.'); + $this->assertTrue($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.'); } } diff --git a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php index 45885dd086c..0c047d90de2 100644 --- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php @@ -4,6 +4,7 @@ namespace Drupal\FunctionalTests\Update; use Drupal\Component\Utility\Html; use Drupal\Component\Render\FormattableMarkup; +use Drupal\Core\Database\Database; /** * Tests the update path base class. @@ -75,11 +76,13 @@ class UpdatePathTestBaseTest extends UpdatePathTestBase { * Test that updates are properly run. */ public function testUpdateHookN() { + $connection = Database::getConnection(); + // Increment the schema version. \Drupal::state()->set('update_test_schema_version', 8001); $this->runUpdates(); - $select = \Drupal::database()->select('watchdog'); + $select = $connection->select('watchdog'); $select->orderBy('wid', 'DESC'); $select->range(0, 5); $select->fields('watchdog', ['message']); @@ -92,7 +95,7 @@ class UpdatePathTestBaseTest extends UpdatePathTestBase { // Ensure schema has changed. $this->assertEqual(drupal_get_installed_schema_version('update_test_schema', TRUE), 8001); // Ensure the index was added for column a. - $this->assertTrue(db_index_exists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.'); + $this->assertTrue($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.'); } } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php index 5637b291bce..bbc8cb9ca86 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php @@ -322,6 +322,15 @@ class DatabaseLegacyTest extends DatabaseTestBase { $this->assertFalse(db_drop_index('test', 'no_such_index')); } + /** + * Tests deprecation of the db_index_exists() function. + * + * @expectedDeprecation db_index_exists() 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 indexExists() on it. For example, $injected_database->schema()->indexExists($table, $name). See https://www.drupal.org/node/2993033 + */ + public function testDbIndexExists() { + $this->assertFalse(db_index_exists('test', 'no_such_index')); + } + /** * Tests deprecation of the db_drop_unique_key() function. * diff --git a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php index 4dee52d5c7f..ddb2aa41b6c 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php @@ -50,11 +50,11 @@ class RegressionTest extends DatabaseTestBase { } /** - * Tests the db_index_exists() function. + * Tests the Schema::indexExists() method. */ public function testDBIndexExists() { - $this->assertSame(TRUE, db_index_exists('test', 'ages'), 'Returns true for existent index.'); - $this->assertSame(FALSE, db_index_exists('test', 'nosuchindex'), 'Returns false for nonexistent index.'); + $this->assertSame(TRUE, $this->connection->schema()->indexExists('test', 'ages'), 'Returns true for existent index.'); + $this->assertSame(FALSE, $this->connection->schema()->indexExists('test', 'nosuchindex'), 'Returns false for nonexistent index.'); } }