You are here

protected function Sql::ensureTables in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/migrate/src/Plugin/migrate/id_map/Sql.php \Drupal\migrate\Plugin\migrate\id_map\Sql::ensureTables()

Create the map and message tables if they don't already exist.

1 call to Sql::ensureTables()
Sql::init in core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
Initialize the plugin.

File

core/modules/migrate/src/Plugin/migrate/id_map/Sql.php, line 280
Contains \Drupal\migrate\Plugin\migrate\id_map\Sql.

Class

Sql
Defines the sql based ID map implementation.

Namespace

Drupal\migrate\Plugin\migrate\id_map

Code

protected function ensureTables() {
  if (!$this
    ->getDatabase()
    ->schema()
    ->tableExists($this->mapTableName)) {

    // Generate appropriate schema info for the map and message tables,
    // and map from the source field names to the map/msg field names.
    $count = 1;
    $source_id_schema = array();
    $pks = array();
    foreach ($this->migration
      ->getSourcePlugin()
      ->getIds() as $id_definition) {
      $mapkey = 'sourceid' . $count++;
      $source_id_schema[$mapkey] = $this
        ->getFieldSchema($id_definition);
      $source_id_schema[$mapkey]['not null'] = TRUE;

      // With InnoDB, utf8mb4-based primary keys can't be over 191 characters.
      // Use ASCII-based primary keys instead.
      if (isset($source_id_schema[$mapkey]['type']) && $source_id_schema[$mapkey]['type'] == 'varchar') {
        $source_id_schema[$mapkey]['type'] = 'varchar_ascii';
      }
      $pks[] = $mapkey;
    }
    $fields = $source_id_schema;

    // Add destination identifiers to map table.
    // TODO: How do we discover the destination schema?
    $count = 1;
    foreach ($this->migration
      ->getDestinationPlugin()
      ->getIds() as $id_definition) {

      // Allow dest identifier fields to be NULL (for IGNORED/FAILED
      // cases).
      $mapkey = 'destid' . $count++;
      $fields[$mapkey] = $this
        ->getFieldSchema($id_definition);
      $fields[$mapkey]['not null'] = FALSE;
    }
    $fields['source_row_status'] = array(
      'type' => 'int',
      'size' => 'tiny',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => MigrateIdMapInterface::STATUS_IMPORTED,
      'description' => 'Indicates current status of the source row',
    );
    $fields['rollback_action'] = array(
      'type' => 'int',
      'size' => 'tiny',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => MigrateIdMapInterface::ROLLBACK_DELETE,
      'description' => 'Flag indicating what to do for this item on rollback',
    );
    $fields['last_imported'] = array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
      'description' => 'UNIX timestamp of the last time this row was imported',
    );
    $fields['hash'] = array(
      'type' => 'varchar',
      'length' => '64',
      'not null' => FALSE,
      'description' => 'Hash of source row data, for detecting changes',
    );
    $schema = array(
      'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
      'fields' => $fields,
    );
    if ($pks) {
      $schema['primary key'] = $pks;
    }
    $this
      ->getDatabase()
      ->schema()
      ->createTable($this->mapTableName, $schema);

    // Now do the message table.
    if (!$this
      ->getDatabase()
      ->schema()
      ->tableExists($this
      ->messageTableName())) {
      $fields = array();
      $fields['msgid'] = array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      );
      $fields += $source_id_schema;
      $fields['level'] = array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      );
      $fields['message'] = array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
      );
      $schema = array(
        'description' => 'Messages generated during a migration process',
        'fields' => $fields,
        'primary key' => array(
          'msgid',
        ),
      );
      if ($pks) {
        $schema['indexes']['sourcekey'] = $pks;
      }
      $this
        ->getDatabase()
        ->schema()
        ->createTable($this
        ->messageTableName(), $schema);
    }
  }
  else {

    // Add any missing columns to the map table.
    if (!$this
      ->getDatabase()
      ->schema()
      ->fieldExists($this->mapTableName, 'rollback_action')) {
      $this
        ->getDatabase()
        ->schema()
        ->addField($this->mapTableName, 'rollback_action', array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Flag indicating what to do for this item on rollback',
      ));
    }
    if (!$this
      ->getDatabase()
      ->schema()
      ->fieldExists($this->mapTableName, 'hash')) {
      $this
        ->getDatabase()
        ->schema()
        ->addField($this->mapTableName, 'hash', array(
        'type' => 'varchar',
        'length' => '64',
        'not null' => FALSE,
        'description' => 'Hash of source row data, for detecting changes',
      ));
    }
  }
}