From 4365ab3f9112192dd7b04f40eef78d93a1845a4d Mon Sep 17 00:00:00 2001 From: Dave Long <dave@longwaveconsulting.com> Date: Wed, 16 Aug 2023 21:42:28 +0100 Subject: [PATCH] Issue #3221101 by mondrake, daffie, longwave: Deprecate Drupal\Core\Database\Connection::makeSequenceName(), make it internal to PostgreSQL --- core/lib/Drupal/Core/Database/Connection.php | 6 +++++ .../tests/src/Kernel/mysql/ConnectionTest.php | 10 +++++++ .../src/Driver/Database/pgsql/Connection.php | 22 ++++++++++++++++ core/modules/pgsql/src/Update10101.php | 2 +- .../src/Kernel/sqlite/ConnectionTest.php | 26 +++++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 core/modules/sqlite/tests/src/Kernel/sqlite/ConnectionTest.php diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 39a856e966eb..aecaed728ffc 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -668,8 +668,14 @@ public function getLogger() { * * @return string * A table prefix-parsed string for the sequence name. + * + * @deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is + * no replacement. + * + * @see https://www.drupal.org/node/3377046 */ public function makeSequenceName($table, $field) { + @trigger_error(__METHOD__ . "() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3377046", E_USER_DEPRECATED); $sequence_name = $this->prefixTables('{' . $table . '}_' . $field . '_seq'); // Remove identifier quotes as we are constructing a new name from a // prefixed and quoted table name. diff --git a/core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php b/core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php index 71be9ea5139e..6521a339f9fb 100644 --- a/core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php +++ b/core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php @@ -21,4 +21,14 @@ public function testMultipleStatementsForNewPhp(): void { Database::getConnection('default', 'default')->query('SELECT * FROM {test}; SELECT * FROM {test_people}', [], ['allow_delimiter_in_query' => TRUE]); } + /** + * Tests deprecation of ::makeSequenceName(). + * + * @group legacy + */ + public function testMakeSequenceNameDeprecation(): void { + $this->expectDeprecation("Drupal\\Core\\Database\\Connection::makeSequenceName() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3377046"); + $this->assertIsString($this->connection->makeSequenceName('foo', 'bar')); + } + } diff --git a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php index 3ba9748bcdde..6bda0f314778 100644 --- a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php @@ -294,6 +294,28 @@ public function mapConditionOperator($operator) { return static::$postgresqlConditionOperatorMap[$operator] ?? NULL; } + /** + * Creates the appropriate sequence name for a given table and serial field. + * + * This method should only be called by the driver's code. + * + * @param string $table + * The table name to use for the sequence. + * @param string $field + * The field name to use for the sequence. + * + * @return string + * A table prefix-parsed string for the sequence name. + * + * @internal + */ + public function makeSequenceName($table, $field) { + $sequence_name = $this->prefixTables('{' . $table . '}_' . $field . '_seq'); + // Remove identifier quotes as we are constructing a new name from a + // prefixed and quoted table name. + return str_replace($this->identifierQuotes, '', $sequence_name); + } + /** * Retrieve a the next id in a sequence. * diff --git a/core/modules/pgsql/src/Update10101.php b/core/modules/pgsql/src/Update10101.php index 75ad28f806f2..4e9d0382b706 100644 --- a/core/modules/pgsql/src/Update10101.php +++ b/core/modules/pgsql/src/Update10101.php @@ -222,7 +222,7 @@ public function getSequenceName(string $table, string $column): ?string { * @return bool * TRUE if the sequence exists by the name. * - * @see \Drupal\Core\Database\Connection::makeSequenceName() + * @see \Drupal\pgsql\Driver\Database\pgsql\Connection::makeSequenceName() */ private function sequenceExists(string $name): bool { return (bool) \Drupal::database() diff --git a/core/modules/sqlite/tests/src/Kernel/sqlite/ConnectionTest.php b/core/modules/sqlite/tests/src/Kernel/sqlite/ConnectionTest.php new file mode 100644 index 000000000000..3f415541ba5e --- /dev/null +++ b/core/modules/sqlite/tests/src/Kernel/sqlite/ConnectionTest.php @@ -0,0 +1,26 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\sqlite\Kernel\sqlite; + +use Drupal\KernelTests\Core\Database\DriverSpecificDatabaseTestBase; + +/** + * SQLite-specific connection tests. + * + * @group Database + */ +class ConnectionTest extends DriverSpecificDatabaseTestBase { + + /** + * Tests deprecation of ::makeSequenceName(). + * + * @group legacy + */ + public function testMakeSequenceNameDeprecation(): void { + $this->expectDeprecation("Drupal\\Core\\Database\\Connection::makeSequenceName() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3377046"); + $this->assertIsString($this->connection->makeSequenceName('foo', 'bar')); + } + +} -- GitLab