Skip to content

Commit

Permalink
Provide basic set of migration including examples.
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Borchert <[email protected]>
  • Loading branch information
Stefan Borchert committed Jul 1, 2017
1 parent fae2891 commit 221c3f7
Show file tree
Hide file tree
Showing 13 changed files with 787 additions and 1 deletion.
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Migration Starter
# undpaul Migrate

This project aims to give you a jump-start on building migrations.

Expand All @@ -7,6 +7,64 @@ This project aims to give you a jump-start on building migrations.
Clone the project into `modules/custom` of your Drupal installation and run
`composer install` within the modules directory.

### Adding additional databases as migration source

`drush upm-da {key} {database name} --username={name} --password={password}`

## Usage

_up_migrate_ comes with some base classes usefull for building migrations.

### ID maps

#### FastSql

Since the mapping tables create by _Migrate_ do not have proper unique indexes,
the ID map "fastsql" alters the definitions of the map tables and add proper
indexes. This increases migration performance significant.
_up_migrate_ sets the ID map for **all** migrations if they do not specify the
property `idMap` for themself.

### Source plugins

_up_migrate_ comes with a set of pre-defined source plugins.

#### SqlBase

The abstract class `\Drupal\up_migrate\Plugin\migrate\source\SqlBase` is meant
to be the parent class for all custom migration sources. It allows configuring
sources from directly within a migration yml file.

Example for definition in _migrate_plus.migration.{id}.yml_:

source:
table:
name: users
alias: u
ids:
uid: integer

This sets the base table of the migration source to a table named "users". The
table can be referenced by using the alias "u" and have one unique key named
"uid".

Example for definition in custom source plugin (must extend `SqlBase`)

/**
* @MigrateSource(
* id = "upm_examples__user",
* table = {
* "name": "users",
* "alias": "u",
* "ids": {
* "uid": "integer"
* }
* }
* )
*/

For more complex ID definitions simply override the function `getIds()`.

#### UserMerge

@todo
65 changes: 65 additions & 0 deletions src/Plugin/Derivative/MigrationGroupMenuLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Drupal\up_migrate\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides menu links for migration groups.
*/
class MigrationGroupMenuLink extends DeriverBase implements ContainerDeriverInterface {

/**
* The config storage.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $configStorage;

/**
* Constructs a MigrationGroupMenuLink instance.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $config_storage
* The config storage.
*/
public function __construct(ConfigEntityStorageInterface $config_storage) {
$this->configStorage = $config_storage;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity.manager')->getStorage('migration_group')
);
}

/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$links = [];

$groups = $this->configStorage->loadMultiple();
/* @var $group \Drupal\migrate_plus\Entity\MigrationGroupInterface */
foreach ($groups as $group) {
$menu_link_id = "migration_group.{$group->id()}";
$links[$menu_link_id] = [
'id' => $menu_link_id,
'title' => $group->label(),
'description' => $group->get('description'),
'route_name' => 'entity.migration.list',
'route_parameters' => [
'migration_group' => $group->id(),
],
] + $base_plugin_definition;
}

return $links;
}

}
45 changes: 45 additions & 0 deletions src/Plugin/migrate/id_map/FastSql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drupal\up_migrate\Plugin\migrate\id_map;

use Drupal\migrate\Plugin\migrate\id_map\Sql;

/**
* Defines a faster SQL based ID map implementation.
*
* Creates unique keys over source columns so joins and lookups are much faster.
*
* @PluginID("fastsql")
*/
class FastSql extends Sql {

/**
* {@inheritdoc}
*/
protected function ensureTables() {
parent::ensureTables();

if ($this->getDatabase()->schema()->indexExists($this->mapTableName, 'source')) {
return;
}
// Add unique index over all source columns.
$count = 1;
$string_fields = 0;
$unique_fields = [];
foreach ($this->migration->getSourcePlugin()->getIds() as $id_definition) {
$unique_fields[] = 'sourceid' . $count++;
if (isset($id_definition['type']) && ('string' === $id_definition['type'])) {
$string_fields++;
}
}

if ($string_fields > 2) {
// Do not create unique index over more than 2 string fields. Otherwise
// there is a great chance the key is too long (>767 bytes).
return;
}

$this->getDatabase()->schema()->addUniqueKey($this->mapTableName, 'source', $unique_fields);
}

}
40 changes: 40 additions & 0 deletions src/Plugin/migrate/process/StringReplace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Drupal\up_migrate\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
* This plugin allows string replacements in the source value.
*
* Example:
* <code>
* process:
* file_destination:
* plugin: up_string_replace
* # Remove unused parts in path.
* 'field/image': ''
* # Move all files into sub-directory.
* 'public:https://': 'public:https://imported'
* </code>
*
* @MigrateProcessPlugin(
* id = "up_string_replace"
* )
*/
class StringReplace extends ProcessPluginBase {

/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$replacements = $this->configuration['replacements'] ?: [];
if (!is_array($replacements)) {
$replacements = [$replacements];
}
return strtr($value, $replacements);
}

}
Loading

0 comments on commit 221c3f7

Please sign in to comment.