Commit 92f54a94 authored by mikeryan's avatar mikeryan Committed by heddn
Browse files

Issue #2949271 by webflo, mikeryan, chriscalip, heddn: Add feature id_mapping...

Issue #2949271 by webflo, mikeryan, chriscalip, heddn: Add feature id_mapping for destination plugin table
parent ef8e5d26
......@@ -110,9 +110,14 @@ class Table extends DestinationBase implements ContainerFactoryPluginInterface {
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = []) {
$id = $row->getSourceIdValues();
if (count($id) != count($this->idFields)) {
throw new MigrateSkipProcessException('All the id fields are required for a table migration.');
$ids = [];
foreach ($this->idFields as $field => $fieldInfo) {
if ($row->hasDestinationProperty($field)) {
$ids[$field] = $row->getDestinationProperty($field);
}
elseif (!$row->hasDestinationProperty($field) && empty($fieldInfo['use_auto_increment'])) {
throw new MigrateSkipProcessException('All the id fields are required for a table migration.');
}
}
$values = $row->getDestination();
......@@ -121,12 +126,26 @@ class Table extends DestinationBase implements ContainerFactoryPluginInterface {
$values = array_intersect_key($values, $this->fields);
}
$status = $this->dbConnection->merge($this->tableName)
->key($id)
->fields($values)
->execute();
// Row contains empty id field with use_auto_increment enabled.
if (count($ids) < count($this->idFields)) {
$status = $id = $this->dbConnection->insert($this->tableName)
->fields($values)
->execute();
foreach ($this->idFields as $field => $fieldInfo) {
if (isset($fieldInfo['use_auto_increment']) && $fieldInfo['use_auto_increment'] === TRUE && !$row->hasDestinationProperty($field)) {
$row->setDestinationProperty($field, $id);
$ids[$field] = $id;
}
}
}
else {
$status = $this->dbConnection->merge($this->tableName)
->key($ids)
->fields($values)
->execute();
}
return $status ? $id : NULL;
return $status ? $ids : NULL;
}
/**
......
<?php
namespace Drupal\Tests\migrate_plus\Kernel;
use Drupal\Core\Database\Database;
use Drupal\migrate\MigrateExecutable;
use Drupal\Tests\migrate\Kernel\MigrateTestBase;
/**
* Tests migration destination table with auto-increment keys.
*
* @group migrate
*/
class MigrateTableIncrementTest extends MigrateTestBase {
const TABLE_NAME = 'migrate_test_destination_table';
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* {@inheritdoc}
*/
public static $modules = ['migrate_plus'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->connection = $this->container->get('database');
$this->connection->schema()->createTable(static::TABLE_NAME, [
'description' => 'Test table',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
],
'data1' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
'data2' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
],
'primary key' => ['id'],
]);
}
/**
* {@inheritdoc}
*/
protected function tearDown() {
$this->connection->schema()->dropTable(static::TABLE_NAME);
parent::tearDown();
}
/**
* Create a minimally valid migration with some source data.
*
* @return array
* The migration definition.
*/
public function tableDestinationMigration() {
return [
'dummy table' => [
[
'id' => 'migration_table_test',
'migration_tags' => ['Testing'],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'data1' => 'dummy1 value1',
'data2' => 'dummy2 value1',
],
[
'data1' => 'dummy1 value2',
'data2' => 'dummy2 value2',
],
[
'data1' => 'dummy1 value3',
'data2' => 'dummy2 value3',
],
],
'ids' => [
'data1' => ['type' => 'string'],
],
],
'destination' => [
'plugin' => 'table',
'table_name' => static::TABLE_NAME,
'id_fields' => [
'id' => [
'type' => 'integer',
'use_auto_increment' => TRUE,
],
],
],
'process' => [
'data1' => 'data1',
'data2' => 'data2',
],
],
],
];
}
/**
* Tests table destination.
*
* @param array $definition
* The migration definition.
*
* @dataProvider tableDestinationMigration
*
* @throws \Drupal\migrate\MigrateException
*/
public function testTableDestination(array $definition) {
$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(1, $values['dummy1 value1']->id);
$this->assertEquals(2, $values['dummy1 value2']->id);
$this->assertEquals(3, $values['dummy1 value3']->id);
$this->assertEquals('dummy2 value3', $values['dummy1 value3']->data2);
$this->assertCount(3, $values);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment