From 20e4baee6958a209a041a2bbfcb38593b9a4a149 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Wed, 13 Jul 2022 15:54:02 +0900 Subject: [PATCH] Issue #3257201 by murilohp, ravi.shankar, beatrizrodrigues, daffie, mondrake: Create the new method Drupal\Core\Database\Connection::getPrefix() and deprecate Drupal\Core\Database\Connection::prefixTable($table) --- core/lib/Drupal/Core/Database/Connection.php | 17 ++++++++++++++++- core/lib/Drupal/Core/Database/Schema.php | 4 ++-- .../migrate/src/Plugin/migrate/id_map/Sql.php | 2 +- .../mysql/src/Driver/Database/mysql/Schema.php | 2 +- .../src/Driver/Database/pgsql/Connection.php | 2 +- .../pgsql/src/Driver/Database/pgsql/Schema.php | 2 +- .../src/Driver/Database/sqlite/Connection.php | 2 +- .../src/Kernel/Scripts/DbCommandBaseTest.php | 4 ++-- .../src/Kernel/Scripts/DbDumpCommandTest.php | 2 +- .../Core/Database/ConnectionTest.php | 10 ++++++++++ .../Tests/Core/Database/ConnectionTest.php | 2 +- 11 files changed, 37 insertions(+), 12 deletions(-) diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 283e161815b1..66dca1427316 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -363,6 +363,15 @@ public function getConnectionOptions() { public function attachDatabase(string $database): void { } + /** + * Returns the prefix of the tables. + * + * @return string $prefix + */ + public function getPrefix(): string { + return $this->prefix; + } + /** * Set the prefix used by this database connection. * @@ -428,8 +437,14 @@ public function quoteIdentifiers($sql) { * * @param string $table * (optional) The table to find the prefix for. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. + * Instead, you should just use Connection::getPrefix(). + * + * @see https://www.drupal.org/node/3260849 */ public function tablePrefix($table = 'default') { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Instead, you should just use Connection::getPrefix(). See https://www.drupal.org/node/3260849', E_USER_DEPRECATED); return $this->prefix; } @@ -460,7 +475,7 @@ public function getUnprefixedTablesMap() { */ public function getFullQualifiedTableName($table) { $options = $this->getConnectionOptions(); - $prefix = $this->tablePrefix($table); + $prefix = $this->getPrefix(); return $options['database'] . '.' . $prefix . $table; } diff --git a/core/lib/Drupal/Core/Database/Schema.php b/core/lib/Drupal/Core/Database/Schema.php index 9f14f359214a..09597b35cf01 100644 --- a/core/lib/Drupal/Core/Database/Schema.php +++ b/core/lib/Drupal/Core/Database/Schema.php @@ -84,7 +84,7 @@ public function nextPlaceholder() { protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) { $info = [ 'schema' => $this->defaultSchema, - 'prefix' => $this->connection->tablePrefix($table), + 'prefix' => $this->connection->getPrefix(), ]; if ($add_prefix) { $table = $info['prefix'] . $table; @@ -196,7 +196,7 @@ public function findTables($table_expression) { $condition = $this->buildTableNameCondition('%', 'LIKE'); $condition->compile($this->connection, $this); - $prefix = $this->connection->tablePrefix(); + $prefix = $this->connection->getPrefix(); $prefix_length = strlen($prefix); $tables = []; // Normally, we would heartily discourage the use of string diff --git a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php index d0f31ffa0b4e..55971fde0edd 100644 --- a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php +++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php @@ -171,7 +171,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition // Default generated table names, limited to 63 characters. $machine_name = str_replace(':', '__', $this->migration->id()); - $prefix_length = strlen($this->database->tablePrefix()); + $prefix_length = strlen($this->database->getPrefix()); $this->mapTableName = 'migrate_map_' . mb_strtolower($machine_name); $this->mapTableName = mb_substr($this->mapTableName, 0, 63 - $prefix_length); $this->messageTableName = 'migrate_message_' . mb_strtolower($machine_name); diff --git a/core/modules/mysql/src/Driver/Database/mysql/Schema.php b/core/modules/mysql/src/Driver/Database/mysql/Schema.php index 374cce56f308..ab937ef2aec1 100644 --- a/core/modules/mysql/src/Driver/Database/mysql/Schema.php +++ b/core/modules/mysql/src/Driver/Database/mysql/Schema.php @@ -48,7 +48,7 @@ class Schema extends DatabaseSchema { * A keyed array with information about the database, table name and prefix. */ protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) { - $info = ['prefix' => $this->connection->tablePrefix($table)]; + $info = ['prefix' => $this->connection->getPrefix()]; if ($add_prefix) { $table = $info['prefix'] . $table; } diff --git a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php index 017f586f9d41..67ed8016316e 100644 --- a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php @@ -299,7 +299,7 @@ public function nextId($existing = 0) { */ public function getFullQualifiedTableName($table) { $options = $this->getConnectionOptions(); - $prefix = $this->tablePrefix($table); + $prefix = $this->getPrefix(); // The fully qualified table name in PostgreSQL is in the form of // <database>.<schema>.<table>, so we have to include the 'public' schema in diff --git a/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php index 9649efdbb79f..986005d6828f 100644 --- a/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php @@ -511,7 +511,7 @@ public function tableExists($table) { * {@inheritdoc} */ public function findTables($table_expression) { - $prefix = $this->connection->tablePrefix(); + $prefix = $this->connection->getPrefix(); $prefix_length = strlen($prefix); $tables = []; diff --git a/core/modules/sqlite/src/Driver/Database/sqlite/Connection.php b/core/modules/sqlite/src/Driver/Database/sqlite/Connection.php index bc4356403592..583eeea0a82f 100644 --- a/core/modules/sqlite/src/Driver/Database/sqlite/Connection.php +++ b/core/modules/sqlite/src/Driver/Database/sqlite/Connection.php @@ -426,7 +426,7 @@ public function nextId($existing_id = 0) { * {@inheritdoc} */ public function getFullQualifiedTableName($table) { - $prefix = $this->tablePrefix($table); + $prefix = $this->getPrefix(); // Don't include the SQLite database file name as part of the table name. return $prefix . $table; diff --git a/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php b/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php index 5f29d4ed3f63..38be5d01bdd3 100644 --- a/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php +++ b/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php @@ -87,13 +87,13 @@ public function testPrefix() { '--database' => 'magic_db', '--prefix' => 'extra', ]); - $this->assertEquals('extra', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix()); + $this->assertEquals('extra', $command->getDatabaseConnection($command_tester->getInput())->getPrefix()); $command_tester->execute([ '-db-url' => Database::getConnectionInfoAsUrl(), '--prefix' => 'extra2', ]); - $this->assertEquals('extra2', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix()); + $this->assertEquals('extra2', $command->getDatabaseConnection($command_tester->getInput())->getPrefix()); // This breaks simpletest cleanup. // @code diff --git a/core/modules/system/tests/src/Kernel/Scripts/DbDumpCommandTest.php b/core/modules/system/tests/src/Kernel/Scripts/DbDumpCommandTest.php index 4f24cebf8c26..fb01a73f1fb6 100644 --- a/core/modules/system/tests/src/Kernel/Scripts/DbDumpCommandTest.php +++ b/core/modules/system/tests/src/Kernel/Scripts/DbDumpCommandTest.php @@ -39,7 +39,7 @@ protected function setUp(): void { // Create a table with a field type not defined in // \Drupal\Core\Database\Schema::getFieldTypeMap. - $table_name = $connection->tablePrefix() . 'foo'; + $table_name = $connection->getPrefix() . 'foo'; $sql = "create table if not exists `$table_name` (`test` datetime NOT NULL);"; $connection->query($sql)->execute(); } diff --git a/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php b/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php index d8e02224251e..81dfe1dac2d1 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php @@ -207,4 +207,14 @@ public function testHasJson() { $this->assertTrue($this->connection->hasJson()); } + /** + * Tests deprecation of ::tablePrefix(). + * + * @group legacy + */ + public function testDeprecatedTablePrefix(): void { + $this->expectDeprecation('Drupal\Core\Database\Connection::tablePrefix() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Instead, you should just use Connection::getPrefix(). See https://www.drupal.org/node/3260849'); + $this->assertIsString($this->connection->tablePrefix()); + } + } diff --git a/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php b/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php index 19bd5eb91627..e46a01d8e7a2 100644 --- a/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php +++ b/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php @@ -59,7 +59,7 @@ public function testPrefixRoundTrip($expected, $prefix_info) { $set_prefix->invokeArgs($connection, [$prefix_info]); // Check the round-trip. foreach ($expected as $table => $prefix) { - $this->assertEquals($prefix, $connection->tablePrefix($table)); + $this->assertEquals($prefix, $connection->getPrefix()); } } -- GitLab