123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- <?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();
- }
- }
- }
|