Skip to content
Snippets Groups Projects
Commit ad6cdc12 authored by Henrik Danielsson's avatar Henrik Danielsson Committed by Lucas Hedding
Browse files

Issue #3330911: Table destination with use_auto_increment with batch_size gives a type error

parent cbf70e84
No related branches found
No related tags found
1 merge request!71Issue #3330911: Table destination with use_auto_increment with batch_size gives a type error
...@@ -183,11 +183,8 @@ class Table extends DestinationBase implements ContainerFactoryPluginInterface, ...@@ -183,11 +183,8 @@ class Table extends DestinationBase implements ContainerFactoryPluginInterface,
elseif ($batch_inserts && $fieldInfo['use_auto_increment']) { elseif ($batch_inserts && $fieldInfo['use_auto_increment']) {
if (count($this->rowsToInsert) === 0) { if (count($this->rowsToInsert) === 0) {
// Get the highest existing ID, so we will create IDs above it. // 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(); ->fetchField();
if (!$this->lastId) {
$this->lastId = 0;
}
} }
$id = ++$this->lastId; $id = ++$this->lastId;
$ids[$field] = $id; $ids[$field] = $id;
......
...@@ -16,6 +16,6 @@ final class MigrateTableIncrementBatchTest extends MigrateTableIncrementTest { ...@@ -16,6 +16,6 @@ final class MigrateTableIncrementBatchTest extends MigrateTableIncrementTest {
* *
* @var int * @var int
*/ */
protected $batchSize = 2; protected int $batchSize = 2;
} }
...@@ -16,6 +16,6 @@ final class MigrateTableIncrementEvenBatchTest extends MigrateTableIncrementTest ...@@ -16,6 +16,6 @@ final class MigrateTableIncrementEvenBatchTest extends MigrateTableIncrementTest
* *
* @var int * @var int
*/ */
protected $batchSize = 3; protected int $batchSize = 3;
} }
...@@ -27,6 +27,13 @@ class MigrateTableIncrementTest extends MigrateTestBase { ...@@ -27,6 +27,13 @@ class MigrateTableIncrementTest extends MigrateTestBase {
*/ */
protected static $modules = ['migrate_plus']; protected static $modules = ['migrate_plus'];
/**
* The batch size to configure.
*
* @var int
*/
protected int $batchSize = 1;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -98,6 +105,7 @@ class MigrateTableIncrementTest extends MigrateTestBase { ...@@ -98,6 +105,7 @@ class MigrateTableIncrementTest extends MigrateTestBase {
'destination' => [ 'destination' => [
'plugin' => 'table', 'plugin' => 'table',
'table_name' => static::TABLE_NAME, 'table_name' => static::TABLE_NAME,
'batch_size' => $this->batchSize,
'id_fields' => [ 'id_fields' => [
'id' => [ 'id' => [
'type' => 'integer', 'type' => 'integer',
...@@ -142,4 +150,40 @@ class MigrateTableIncrementTest extends MigrateTestBase { ...@@ -142,4 +150,40 @@ class MigrateTableIncrementTest extends MigrateTestBase {
$this->assertCount(3, $values); $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);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment