You are here

protected function MigrateSQLMap::ensureTables in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/sources/sqlmap.inc \MigrateSQLMap::ensureTables()

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

1 call to MigrateSQLMap::ensureTables()
MigrateSQLMap::__construct in plugins/sources/sqlmap.inc
Constructor.

File

plugins/sources/sqlmap.inc, line 147
Defines a Drupal db-based implementation of MigrateMap.

Class

MigrateSQLMap
@file Defines a Drupal db-based implementation of MigrateMap.

Code

protected function ensureTables() {
  if (!$this->ensured) {
    if (!$this->connection
      ->schema()
      ->tableExists($this->mapTable)) {

      // 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_key_schema = array();
      $pks = array();
      foreach ($this->sourceKey as $field_schema) {
        $mapkey = 'sourceid' . $count++;
        $source_key_schema[$mapkey] = $field_schema;
        $source_key_schema[$mapkey]['not null'] = TRUE;
        $pks[] = $mapkey;
      }
      $fields = $source_key_schema;

      // Add destination keys to map table.
      // TODO: How do we discover the destination schema?
      $count = 1;
      foreach ($this->destinationKey as $field_schema) {

        // Allow dest key fields to be NULL (for IGNORED/FAILED cases).
        $field_schema['not null'] = FALSE;
        $field_schema['default'] = NULL;
        $mapkey = 'destid' . $count++;
        $fields[$mapkey] = $field_schema;
      }
      $fields['needs_update'] = array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => MigrateMap::STATUS_IMPORTED,
        'description' => 'Indicates current status of the source row',
      );
      $fields['rollback_action'] = array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => MigrateMap::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' => '32',
        'not null' => FALSE,
        'description' => 'Hash of source row data, for detecting changes',
      );
      $schema = array(
        'description' => t('Mappings from source key to destination key'),
        'fields' => $fields,
        'primary key' => $pks,
      );
      $this->connection
        ->schema()
        ->createTable($this->mapTable, $schema);

      // Now for the message table
      $fields = array();
      $fields['msgid'] = array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      );
      $fields += $source_key_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' => t('Messages generated during a migration process'),
        'fields' => $fields,
        'primary key' => array(
          'msgid',
        ),
        'indexes' => array(
          'sourcekey' => $pks,
        ),
      );
      $this->connection
        ->schema()
        ->createTable($this->messageTable, $schema);
    }
    else {

      // Add any missing columns to the map table.
      if (!$this->connection
        ->schema()
        ->fieldExists($this->mapTable, 'rollback_action')) {
        $this->connection
          ->schema()
          ->addField($this->mapTable, '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->connection
        ->schema()
        ->fieldExists($this->mapTable, 'hash')) {
        $this->connection
          ->schema()
          ->addField($this->mapTable, 'hash', array(
          'type' => 'varchar',
          'length' => '32',
          'not null' => FALSE,
          'description' => 'Hash of source row data, for detecting changes',
        ));
      }
    }
    $this->ensured = TRUE;
  }
}