diff --git a/src/Plugin/migrate/destination/Table.php b/src/Plugin/migrate/destination/Table.php index 87424b4b75ef261fd490228f2bfca794eb27982b..5c46f602b3fa9c69e1597cca7fe250c40bae4a5f 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 7d07f8b083827b755e82f1b34b9c1515a0cb55ce..b3ab1f5f30e82200ad7333019b4794eab6fffabb 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 b858cddeda0d2d04c72c2a338c4e9a1582fb61e1..fc8497e05cd6b52de2709d73f204905a9761286e 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 ab83a7824e058d4db5dfbe4579d8483758628f4b..14ce618fe96a254ddcdffe2fe977d7a7f2948596 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); + } + }