<?php

/**
 * @file
 * Migrate module installation
 */

function migrate_schema() {
  $schema = array();
  $schema['migrate_status'] = migrate_schema_status();
  $schema['migrate_log'] = migrate_schema_log();
  $schema['migrate_group'] = migrate_schema_group();
  $schema['migrate_field_mapping'] = migrate_schema_field_mapping();
  return $schema;
}

function migrate_schema_status() {
  return array(
    'description' => 'Status information for migrations',
    'fields' => array(
      'machine_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Unique machine name for migration',
      ),
      'class_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Name of class to instantiate for this migration',
      ),
      'group_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Name of group containing migration',
      ),
      'status' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Current status of migration',
      ),
      'highwater' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Highwater mark for detecting updated content',
      ),
      'arguments' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of arguments to the migration constructor',
      ),
    ),
    'primary key' => array('machine_name'),
  );
}

function migrate_schema_log() {
  return array(
    'description' => 'History of migration processes',
    'fields' => array(
      'mlid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary key for migrate_log table',
      ),
      'machine_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Unique machine name for migration',
      ),
      'process_type' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Type of migration process - 1 for import, 2 for rollback',
      ),
      'starttime' => array(
        'type' => 'int',
        'size' => 'big',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Begin time of a migration process, times 1000',
      ),
      'endtime' => array(
        'type' => 'int',
        'size' => 'big',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'End time of a migration process, times 1000',
      ),
      'initialhighwater' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Initial highwater mark',
      ),
      'finalhighwater' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Final highwater mark',
      ),
      'numprocessed' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'Number of items processed',
      ),
    ),
    'primary key' => array('mlid'),
  );
}

function migrate_schema_group() {
  return array(
    'description' => 'Information on migration groups',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Unique machine name for a migration group',
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Display name for a migration group',
      ),
      'arguments' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of arguments to the migration group',
      ),
    ),
    'primary key' => array('name'),
  );
}

function migrate_schema_field_mapping() {
  return array(
    'description' => 'History of migration processes',
    'fields' => array(
      'fmid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Unique ID for the field mapping row',
      ),
      'machine_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Parent migration for the field mapping',
      ),
      'destination_field' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Destination field for the field mapping',
      ),
      'source_field' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Source field for the field mapping',
      ),
      'options' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized MigrateFieldMapping object holding all options',
      ),
    ),
    'primary key' => array('fmid'),
  );
}

/**
 * Implements hook_uninstall().
 * Drop map/message tables, in case implementing classes did not.
 */
function migrate_uninstall() {
  // Note: If a derived Migration class defined its own map or message
  // table name not fitting this pattern, that class is solely responsible for
  // cleaning up
  // TODO: Prefix table names (db_find_tables does not do it)
  foreach (db_find_tables('migrate_map_%') as $tablename) {
    db_drop_table($tablename);
  }
  foreach (db_find_tables('migrate_message_%') as $tablename) {
    db_drop_table($tablename);
  }

  // Remove any file_usage entries we've written
  if (db_table_exists('file_usage')) {
    db_delete('file_usage')
      ->condition('module', 'migrate')
      ->execute();
  }

  // Remove variables
  variable_del('migrate_disable_autoregistration');
  variable_del('migrate_disabled_handlers');
  variable_del('migrate_deprecation_warnings');
}

/**
 * Add highwater mark
 */
function migrate_update_7001() {
  if (!db_field_exists('migrate_status', 'highwater')) {
    db_add_field('migrate_status', 'highwater', array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
          'description' => 'Highwater mark for detecting updated content',
        )
    );
  }

  $ret = t('Added highwater column to migrate_status table');
  return $ret;
}

/**
 * Add last_imported field to all map tables
 */
function migrate_update_7002() {
  foreach (db_find_tables('migrate_map_%') as $tablename) {
    if (!db_field_exists($tablename, 'last_imported')) {
      db_add_field($tablename, 'last_imported', array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'UNIX timestamp of the last time this row was imported',
      ));
    }
  }
  $ret = t('Added last_imported column to all map tables');
  return $ret;
}

/**
 * Add lastthroughput column to migrate_status
 */
function migrate_update_7003() {
  $ret = '';
  if (!db_field_exists('migrate_status', 'lastthroughput')) {
    db_add_field('migrate_status', 'lastthroughput', array(
        'type' => 'int',
        'length' => 11,
        'not null' => FALSE,
        'description' => 'Rate of success during most recent completed import (# per minute)',
      )
    );
  }

  $ret = t('Added lastthroughput column to migrate_status table');
  return $ret;
}

/**
 * Convert lastimported datetime field to lastimportedtime int field.
 */
function migrate_update_7004() {
  $ret = '';
  if (!db_field_exists('migrate_status', 'lastimportedtime')) {
    db_add_field('migrate_status', 'lastimportedtime', array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'Date and time of last completed import',
      )
    );

    if (db_field_exists('migrate_status', 'lastimported')) {
      $result = db_select('migrate_status', 'ms')
                ->fields('ms', array('machine_name', 'lastimported'))
                ->execute();
      foreach ($result as $row) {
        $lastimportedtime = strtotime($row->lastimported);
        db_update('migrate_status')
          ->fields(array('lastimportedtime' => $lastimportedtime))
          ->condition('machine_name', $row->machine_name)
          ->execute();
      }

      db_drop_field('migrate_status', 'lastimported');

      $ret .= "\n" . t('Converted lastimported datetime field to lastimportedtime int field');
    }
  }
  return $ret;
}

/**
 * Add support for history logging
 */
function migrate_update_7005() {
  $ret = '';
  if (!db_table_exists('migrate_log')) {
    $ret .= "\n" . t('Create migrate_log table');
    db_create_table('migrate_log', migrate_schema_log());
    $ret .= "\n" . t('Remove historic columns from migrate_status table');
    db_drop_field('migrate_status', 'lastthroughput');
    db_drop_field('migrate_status', 'lastimportedtime');
  }
  return $ret;
}

/**
 * Add and populate class_name field. Any existing migration code using
 * dependencies or sourceMigration() must be changed! See CHANGELOG.txt.
 */
function migrate_update_7006() {
  $ret = '';
  if (!db_field_exists('migrate_status', 'class_name')) {
    db_add_field('migrate_status', 'class_name', array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
          'description' => 'Name of class to instantiate for this migration',
        )
    );

    db_query("UPDATE {migrate_status}
              SET class_name = CONCAT(machine_name, 'Migration')
             ");
    $ret = t('Added class_name column to migrate_status table');
  }
  return $ret;
}

/**
 * Add arguments field to migrate_status table.
 */
function migrate_update_7007() {
  $ret = '';
  if (!db_field_exists('migrate_status', 'arguments')) {
    db_add_field('migrate_status', 'arguments', array(
          'type' => 'blob',
          'not null' => FALSE,
          'size' => 'big',
          'serialize' => TRUE,
          'description' => 'A serialized array of arguments to the migration constructor',
          )

    );
    $ret = t('Added arguments column to migrate_status table');
  }
  return $ret;
}

/**
 * Update map tables to reflect change of needs_update to a status column.
 */
function migrate_update_7008() {
  // Updates can be run when the module is disabled, which would mean the
  // call to migrate_migrations() will fail. Just bail in that case...
  if (!module_exists('migrate')) {
    throw new DrupalUpdateException(t('This update cannot be run while the Migrate module is disabled - you must enable Migrate to run this update.'));
  }
  $ret = '';
  foreach (migrate_migrations() as $migration) {
    if (is_a($migration, 'Migration')) {
      // Since we're now tracking failed/ignored rows in the map table,
      // destination keys need to be nullable
      $map = $migration->getMap();
      $map_connection = $map->getConnection();
      $map_table = $map->getMapTable();
      $destination = $migration->getDestination();
      $key_schema = $destination->getKeySchema();
      $index = 1;
      foreach ($key_schema as $field_schema) {
        $field = 'destid' . $index++;
        $field_schema['not null'] = FALSE;
        $map_connection->schema()->changeField($map_table, $field, $field,
                                               $field_schema);
        $ret .= "\n" . t('Changed !table.!field to be non-null',
          array('!table' => $map_table, '!field' => $field));
      }

      // Add any existing failures to the map table
      $msg_table = $map->getMessageTable();
      $msg_marked = FALSE;
      $result = $map_connection->select($msg_table, 'msg')
                ->fields('msg')
                ->condition('level', Migration::MESSAGE_INFORMATIONAL, '<>')
                ->execute();
      foreach ($result as $row) {
        $keys = array();
        $index = 1;
        foreach ($row as $field => $value) {
          if (drupal_substr($field, 0, 8) == 'sourceid') {
            $keys['sourceid' . $index++] = $value;
          }
        }
        $map_connection->merge($map_table)
                       ->key($keys)
                       ->fields(array('needs_update' => MigrateMap::STATUS_FAILED))
                       ->execute();
        $msg_marked = TRUE;
      }
      if ($msg_marked) {
        $ret .= "\n" . t('Marked failures in !table', array('!table' => $map_table));
      }
    }
  }

  return $ret;
}

/**
 * Warn that there have been incompatible changes to file handling.
 */
function migrate_update_7201() {
  return t('File field and destination handling has been completely refactored
    - if you are migrating files, you will need to change your migration
    implementation to reflect these changes. Please see
    <a href="@doc">Handling files in Drupal 7</a> for more information',
    array('@doc' => 'http://drupal.org/node/1540106'));
}

/**
 * Add rollback_action field to all map tables in the Drupal database.
 */
function migrate_update_7202() {
  // Note this won't catch any prefixed tables, or any stored in the source
  // database - ensureTables() will take care of those.
  foreach (db_find_tables('migrate_map_%') as $tablename) {
    if (!db_field_exists($tablename, 'rollback_action')) {
      db_add_field($tablename, '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',
      ));
    }
  }
  $ret = t('Added rollback_action column to all map tables');
  return $ret;
}

/**
 * Add database tracking of per-group info.
 */
function migrate_update_7203() {
  $ret = '';
  if (!db_table_exists('migrate_group')) {
    $ret .= t('Create migrate_group table') . "\n";
    db_create_table('migrate_group', migrate_schema_group());
  }
  if (!db_field_exists('migrate_status', 'group_name')) {
    $ret .= t('Add group relationship to migrate_status table'). "\n";
    db_add_field('migrate_status', 'group_name', array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => 'default',
          'description' => 'Name of group containing migration',
        )
    );
    // Populate each migration's group_name field
    $groups = array();
    foreach (migrate_migrations() as $machine_name => $migration) {
      $group_name = $migration->getGroup()->getName();
      if (empty($group_name)) {
        $group_name = 'default';
      }
      $groups[$group_name] = $group_name;
      db_update('migrate_status')
        ->fields(array('group_name' => $group_name))
        ->condition('machine_name', $machine_name)
        ->execute();
    }
    // Populate the migrate_group table
    foreach ($groups as $group_name) {
      $title = db_select('migrate_group', 'mg')
               ->fields('mg', array('title'))
               ->condition('name', $group_name)
               ->execute()
               ->fetchField();
      if (!$title) {
        db_insert('migrate_group')
          ->fields(array(
              'name' => $group_name,
              'title' => $group_name,
              'arguments' => serialize(array()),
            ))
          ->execute();
      }
    }
  }
  return $ret;
}

/**
 * Add database tracking of field mappings.
 */
function migrate_update_7204() {
  $ret = '';
  if (!db_table_exists('migrate_field_mapping')) {
    $ret = t('Create migrate_field_mapping table');
    db_create_table('migrate_field_mapping', migrate_schema_field_mapping());
  }
  return $ret;
}

/**
 * Remove obsolete autoregistration disablement.
 */
function migrate_update_7205() {
  variable_del('migrate_disable_autoregistration');
}

/**
 * Replace three-column PK with a simple serial.
 */
function migrate_update_7206() {
  if (!db_field_exists('migrate_field_mapping', 'fmid')) {
    db_drop_primary_key('migrate_field_mapping');
    db_add_field('migrate_field_mapping', 'fmid',
      array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Unique ID for the field mapping row',
      ),
      array(
        'primary key' => array('fmid'),
      )
    );
  }
}

/**
 * Make sure we remove an empty 'default' group created by the previous updates.
 */
function migrate_update_7207() {
  $rows = db_select('migrate_group', 'mg')
          ->fields('mg', array('name'))
          ->condition('name', 'default')
          ->execute()
          ->rowCount();
  if ($rows > 0) {
    $rows = db_select('migrate_status', 'ms')
            ->fields('ms', array('machine_name'))
            ->condition('group_name', 'default')
            ->execute()
            ->rowCount();
    if ($rows == 0) {
      db_delete('migrate_group')
      ->condition('name', 'default')
      ->execute();
    }
  }
}
