Commit cd369d53 authored by heddn's avatar heddn Committed by heddn

Issue #3006094 by heddn, jcnventura, DanChadwick: Add migrate source for sql table

parent c9cb639c
<?php
namespace Drupal\migrate_plus\Plugin\migrate\source;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Plugin\MigrationInterface;
/**
* Source plugin for retrieving data via URLs.
*
* @MigrateSource(
* id = "table"
* )
*/
class Table extends SqlBase {
const TABLE_ALIAS = 't';
/**
* The name of the destination table.
*
* @var string
*/
protected $tableName;
/**
* IDMap compatible array of id fields.
*
* @var array
*/
protected $idFields;
/**
* Array of fields present on the destination table.
*
* @var array
*/
protected $fields;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state);
$this->tableName = $configuration['table_name'];
// Insert alias in id_fields.
foreach ($configuration['id_fields'] as &$field) {
$field['alias'] = static::TABLE_ALIAS;
}
$this->idFields = $configuration['id_fields'];
$this->fields = isset($configuration['fields']) ? $configuration['fields'] : [];
}
/**
* {@inheritdoc}
*/
public function query() {
return $this->select($this->tableName, static::TABLE_ALIAS)->fields(static::TABLE_ALIAS, $this->fields);
}
/**
* {@inheritdoc}
*/
public function fields() {
return $this->fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
if (empty($this->idFields)) {
throw new MigrateException('Id fields are required for a table source');
}
return $this->idFields;
}
}
......@@ -135,7 +135,7 @@ class Xml extends DataParserPluginBase {
* A \SimpleXmlElement when the document is parseable, or false if a
* parsing error occurred.
*
* @throws MigrateException
* @throws \Drupal\migrate\MigrateException
*/
protected function getSimpleXml() {
$node = $this->reader->expand();
......
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\migrate_plus\Kernel;
use Drupal\Core\Database\Database;
use Drupal\migrate\MigrateExecutable;
use Drupal\Tests\migrate\Kernel\MigrateTestBase;
......@@ -13,7 +12,8 @@ use Drupal\Tests\migrate\Kernel\MigrateTestBase;
*/
class MigrateTableTest extends MigrateTestBase {
const TABLE_NAME = 'migrate_test_destination_table';
const SOURCE_TABLE_NAME = 'migrate_test_source_table';
const DEST_TABLE_NAME = 'migrate_test_destination_table';
/**
* The database connection.
......@@ -30,76 +30,88 @@ class MigrateTableTest extends MigrateTestBase {
protected function setUp() {
parent::setUp();
$this->connection = Database::getConnection();
$this->connection->schema()->createTable(static::TABLE_NAME, [
'description' => 'Test table',
'fields' => [
'data' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
'data2' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
'data3' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
$this->connection = $this->container->get('database');
$connections = [
static::SOURCE_TABLE_NAME => $this->sourceDatabase,
static::DEST_TABLE_NAME => $this->connection,
];
foreach ($connections as $table => $connection) {
$connection->schema()->createTable($table, [
'description' => 'Test table',
'fields' => [
'data' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
'data2' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
'data3' => [
'type' => 'varchar',
'length' => '32',
'not null' => TRUE,
],
],
'primary key' => ['data'],
]);
}
$query = $this->sourceDatabase->insert(static::SOURCE_TABLE_NAME)
->fields(['data', 'data2', 'data3']);
$values = [
[
'data' => 'dummy value',
'data2' => 'dummy2 value',
'data3' => 'dummy3 value',
],
[
'data' => 'dummy value2',
'data2' => 'dummy2 value2',
'data3' => 'dummy3 value2',
],
[
'data' => 'dummy value3',
'data2' => 'dummy2 value3',
'data3' => 'dummy3 value3',
],
'primary key' => ['data'],
]);
];
foreach ($values as $record) {
$query->values($record);
}
$query->execute();
}
/**
* {@inheritdoc}
*/
protected function tearDown() {
$this->connection->schema()->dropTable(static::TABLE_NAME);
$this->sourceDatabase->schema()->dropTable(static::SOURCE_TABLE_NAME);
$this->connection->schema()->dropTable(static::DEST_TABLE_NAME);
parent::tearDown();
}
/**
* Create a minimally valid migration with some source data.
*
* @return array
* The migration definition.
* Tests table migration.
*/
protected function getTableDestinationMigration() {
public function testTableMigration() {
$definition = [
'id' => 'migration_table_test',
'migration_tags' => ['Testing'],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'data' => 'dummy value',
'data2' => 'dummy2 value',
'data3' => 'dummy3 value',
],
[
'data' => 'dummy value2',
'data2' => 'dummy2 value2',
'data3' => 'dummy3 value2',
],
[
'data' => 'dummy value3',
'data2' => 'dummy2 value3',
'data3' => 'dummy3 value3',
],
],
'ids' => [
'plugin' => 'table',
'table_name' => static::SOURCE_TABLE_NAME,
'id_fields' => [
'data' => ['type' => 'string'],
],
],
'destination' => [
'plugin' => 'table',
'table_name' => static::TABLE_NAME,
'id_fields' => ['data' => ['type' => 'string']],
'table_name' => static::DEST_TABLE_NAME,
'id_fields' => [
'data' => ['type' => 'string'],
],
],
'process' => [
'data' => 'data',
......@@ -107,20 +119,13 @@ class MigrateTableTest extends MigrateTestBase {
'data3' => 'data3',
],
];
return $definition;
}
/**
* Tests table destination.
*/
public function testTableDestination() {
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($this->getTableDestinationMigration());
$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)
$values = $this->connection->select(static::DEST_TABLE_NAME)
->fields(static::DEST_TABLE_NAME)
->execute()
->fetchAllAssoc('data');
......@@ -129,31 +134,11 @@ class MigrateTableTest extends MigrateTestBase {
$this->assertEquals('dummy2 value2', $values['dummy value2']->data2);
$this->assertEquals('dummy3 value3', $values['dummy value3']->data3);
$this->assertEquals(3, count($values));
}
/**
* Tests table rollback.
*/
public function testTableRollback() {
$this->testTableDestination();
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($this->getTableDestinationMigration());
$executable = new MigrateExecutable($migration, $this);
$executable->import();
$values = $this->connection->select(static::TABLE_NAME)
->fields(static::TABLE_NAME)
->execute()
->fetchAllAssoc('data');
$this->assertEquals('dummy value', $values['dummy value']->data);
$this->assertEquals(3, count($values));
// Now rollback.
$executable->rollback();
$values = $this->connection->select(static::TABLE_NAME)
->fields(static::TABLE_NAME)
$values = $this->connection->select(static::DEST_TABLE_NAME)
->fields(static::DEST_TABLE_NAME)
->execute()
->fetchAllAssoc('data');
......
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