diff --git a/core/includes/database.inc b/core/includes/database.inc index ca3ca6eea6..1e498cc2e4 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -481,6 +481,7 @@ function db_close(array $options = []) { * @see \Drupal\Core\Database\Connection::nextId() */ function db_next_id($existing_id = 0) { + @trigger_error('db_next_id() 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 and call nextId() on it. For example, $injected_database->nextId($existing_id). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED); return Database::getConnection()->nextId($existing_id); } diff --git a/core/includes/form.inc b/core/includes/form.inc index dacae875bd..2a801814bd 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -824,7 +824,7 @@ function batch_process($redirect = NULL, Url $url = NULL, $redirect_callback = N // Assign an arbitrary id: don't rely on a serial column in the 'batch' // table, since non-progressive batches skip database storage completely. - $batch['id'] = db_next_id(); + $batch['id'] = \Drupal::database()->nextId();; // Move operations to a job queue. Non-progressive batches will use a // memory-based queue. diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php index 5d2d7001d4..53b3bcf153 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php @@ -9,6 +9,13 @@ */ class DatabaseLegacyTest extends DatabaseTestBase { + /** + * The modules to enable. + * + * @var array + */ + public static $modules = ['database_test', 'system']; + /** * Tests the db_table_exists() function. * @@ -37,6 +44,16 @@ public function testDbDropTable() { $this->assertFalse(db_drop_table('temp_test_table')); } + /** + * Tests deprecation of the db_next_id() function. + * + * @expectedDeprecation db_next_id() 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 and call nextId() on it. For example, $injected_database->nextId($existing_id). See https://www.drupal.org/node/2993033 + */ + public function testDbNextId() { + $this->installSchema('system', 'sequences'); + $this->assertEquals(1001, db_next_id(1000)); + } + /** * Tests the db_change_field() function is deprecated. * diff --git a/core/tests/Drupal/KernelTests/Core/Database/NextIdTest.php b/core/tests/Drupal/KernelTests/Core/Database/NextIdTest.php index 129d50ba00..9adb214b92 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/NextIdTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/NextIdTest.php @@ -2,21 +2,23 @@ namespace Drupal\KernelTests\Core\Database; -use Drupal\KernelTests\KernelTestBase; - /** * Tests the sequences API. * * @group Database */ -class NextIdTest extends KernelTestBase { +class NextIdTest extends DatabaseTestBase { /** * The modules to enable. + * * @var array */ - public static $modules = ['system']; + public static $modules = ['database_test', 'system']; + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); $this->installSchema('system', 'sequences'); @@ -26,13 +28,13 @@ protected function setUp() { * Tests that the sequences API works. */ public function testDbNextId() { - $first = db_next_id(); - $second = db_next_id(); + $first = $this->connection->nextId(); + $second = $this->connection->nextId(); // We can test for exact increase in here because we know there is no // other process operating on these tables -- normally we could only // expect $second > $first. $this->assertEqual($first + 1, $second, 'The second call from a sequence provides a number increased by one.'); - $result = db_next_id(1000); + $result = $this->connection->nextId(1000); $this->assertEqual($result, 1001, 'Sequence provides a larger number than the existing ID.'); }