From ad6cdc12d92965f92865d888714a7fe19f0456b0 Mon Sep 17 00:00:00 2001 From: Henrik Danielsson <42902-twod@users.noreply.drupalcode.org> Date: Wed, 15 Feb 2023 16:24:15 +0000 Subject: [PATCH] Issue #3330911: Table destination with use_auto_increment with batch_size gives a type error --- src/Plugin/migrate/destination/Table.php | 5 +-- .../Kernel/MigrateTableIncrementBatchTest.php | 2 +- .../MigrateTableIncrementEvenBatchTest.php | 2 +- .../src/Kernel/MigrateTableIncrementTest.php | 44 +++++++++++++++++++ 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Plugin/migrate/destination/Table.php b/src/Plugin/migrate/destination/Table.php index 87424b4b..5c46f602 100755 --- a/src/Plugin/migrate/destination/Table.php +++ b/src/Plugin/migrate/destination/Table.php @@ -183,11 +183,8 @@ class Table extends DestinationBase implements ContainerFactoryPluginInterface, elseif ($batch_inserts && $fieldInfo['use_auto_increment']) { if (count($this->rowsToInsert) === 0) { // Get the highest existing ID, so we will create IDs above it. - $this->lastId = $this->dbConnection->query("SELECT MAX($field) AS MaxId FROM {{$this->tableName}}") + $this->lastId = (int) $this->dbConnection->query("SELECT MAX($field) AS MaxId FROM {{$this->tableName}}") ->fetchField(); - if (!$this->lastId) { - $this->lastId = 0; - } } $id = ++$this->lastId; $ids[$field] = $id; diff --git a/tests/src/Kernel/MigrateTableIncrementBatchTest.php b/tests/src/Kernel/MigrateTableIncrementBatchTest.php index 7d07f8b0..b3ab1f5f 100755 --- a/tests/src/Kernel/MigrateTableIncrementBatchTest.php +++ b/tests/src/Kernel/MigrateTableIncrementBatchTest.php @@ -16,6 +16,6 @@ final class MigrateTableIncrementBatchTest extends MigrateTableIncrementTest { * * @var int */ - protected $batchSize = 2; + protected int $batchSize = 2; } diff --git a/tests/src/Kernel/MigrateTableIncrementEvenBatchTest.php b/tests/src/Kernel/MigrateTableIncrementEvenBatchTest.php index b858cdde..fc8497e0 100755 --- a/tests/src/Kernel/MigrateTableIncrementEvenBatchTest.php +++ b/tests/src/Kernel/MigrateTableIncrementEvenBatchTest.php @@ -16,6 +16,6 @@ final class MigrateTableIncrementEvenBatchTest extends MigrateTableIncrementTest * * @var int */ - protected $batchSize = 3; + protected int $batchSize = 3; } diff --git a/tests/src/Kernel/MigrateTableIncrementTest.php b/tests/src/Kernel/MigrateTableIncrementTest.php index ab83a782..14ce618f 100755 --- a/tests/src/Kernel/MigrateTableIncrementTest.php +++ b/tests/src/Kernel/MigrateTableIncrementTest.php @@ -27,6 +27,13 @@ class MigrateTableIncrementTest extends MigrateTestBase { */ protected static $modules = ['migrate_plus']; + /** + * The batch size to configure. + * + * @var int + */ + protected int $batchSize = 1; + /** * {@inheritdoc} */ @@ -98,6 +105,7 @@ class MigrateTableIncrementTest extends MigrateTestBase { 'destination' => [ 'plugin' => 'table', 'table_name' => static::TABLE_NAME, + 'batch_size' => $this->batchSize, 'id_fields' => [ 'id' => [ 'type' => 'integer', @@ -142,4 +150,40 @@ class MigrateTableIncrementTest extends MigrateTestBase { $this->assertCount(3, $values); } + /** + * Tests table destination with data already in the table. + * + * @param array $definition + * The migration definition. + * + * @dataProvider tableDestinationMigration + * + * @throws \Drupal\migrate\MigrateException + */ + public function testTableDestinationWithExistingData(array $definition) { + $this->connection->insert(static::TABLE_NAME) + ->fields([ + 'id' => 5, + 'data1' => 'Dummy initial value', + 'data2' => 'Dummy initial value2', + ]) + ->execute(); + $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); + + $executable = new MigrateExecutable($migration, $this); + $executable->import(); + + $values = $this->connection->select(static::TABLE_NAME) + ->fields(static::TABLE_NAME) + ->execute() + ->fetchAllAssoc('data1'); + + $this->assertEquals(5, $values['Dummy initial value']->id); + $this->assertEquals(6, $values['dummy1 value1']->id); + $this->assertEquals(7, $values['dummy1 value2']->id); + $this->assertEquals(8, $values['dummy1 value3']->id); + $this->assertEquals('dummy2 value3', $values['dummy1 value3']->data2); + $this->assertCount(4, $values); + } + } -- GitLab