Skip to content

Instantly share code, notes, and snippets.

@holtzermann17
Created June 28, 2011 16:36
Show Gist options
  • Save holtzermann17/1051550 to your computer and use it in GitHub Desktop.
Save holtzermann17/1051550 to your computer and use it in GitHub Desktop.
Drupal Migration
These files import ONE database table (users) into Drupal.
Some key issues to note for the record:
* getting the name of the hook right in planetmath.module
(it should be MODULENAME_migrate_api).
* make sure to clear the cache after the module is
imported (drush cc all). This should help identify any
coding errors!
* Figuring out what to do in the .install file
<?php
/**
* @file
* Adjusting the beer example to work for PlanetMath
* Things that aren't immediately clear: how to execute the appropriate SQL command?
*/
abstract class PMMigration extends Migration {
public function __construct() {
parent::__construct();
$this->team = array(
new MigrateTeamMember('Joe Corneli', 'holtzermann17@gmail.com', t('PM Board Member')),
);
// ':id:' adjusted by assigning ->issueNumber(1234) to a mapping.
$this->issuePattern = 'http://trac.mathweb.org/planetary/ticket/:id:';
}
}
/**
* to import the data you do:
* drush migrate-import PMUser
* or this can be handled through the web interface at admin/content/migrate
*/
class PMUserMigration extends PMMigration {
public function __construct() {
parent::__construct();
$this->description = t('PM user data');
$this->map = new MigrateSQLMap($this->machineName,
array('uid' => array(
'type' => 'int',
'length' => 11,
'not null' => TRUE,
'description' => 'User ID.'
)
),
MigrateDestinationUser::getKeySchema()
);
// planetmath_users is the name of the table to select from, mpu is a local alias
$query = db_select('planetmath_users', 'mpu')
->fields('mpu', array('uid', 'password', 'email', 'sig', 'joined', 'username', 'active', 'prefs', 'preamble', 'surname', 'forename', 'lastip', 'karma', 'bio', 'last', 'city', 'state', 'country', 'homepage', 'score'));
$this->source = new MigrateSourceSQL($query);
$this->destination = new MigrateDestinationUser();
// Mapped fields
/**
* Hopefully the direct mapping of uids will work OK
* even given some previous users. (Hm, not sure
* about this, will experiment more later.)
*/
// $this->addSimpleMappings(array('uid'));
$this->addFieldMapping('pass', 'password');
$this->addFieldMapping('mail', 'email');
$this->addFieldMapping('init', 'email');
$this->addFieldMapping('signature', 'sig');
$this->addFieldMapping('signature_format')
->defaultValue('filtered_html');
$this->addFieldMapping('created', 'joined');
$this->addFieldMapping('name', 'username');
$this->addFieldMapping('login', 'last');
$this->addFieldMapping('access', 'last');
// making everyone an "active" user in drupal terms (i.e. not blocked)
$this->addFieldMapping('status')
->defaultValue(1);
$this->addFieldMapping('picture')
->defaultValue(0);
// Rather than leaving these things unmapped, we'll try creating some new fields to hold them.
$this->addFieldMapping('planetmath_migrate_prefs', 'prefs');
$this->addFieldMapping('planetmath_migrate_preamble', 'preamble');
$this->addFieldMapping('planetmath_migrate_surname', 'surname');
$this->addFieldMapping('planetmath_migrate_forename', 'forename');
$this->addFieldMapping('planetmath_migrate_lastip', 'lastip');
$this->addFieldMapping('planetmath_migrate_bio', 'bio');
$this->addFieldMapping('planetmath_migrate_city', 'city');
$this->addFieldMapping('planetmath_migrate_state', 'state');
$this->addFieldMapping('planetmath_migrate_country', 'country');
$this->addFieldMapping('planetmath_migrate_homepage', 'homepage');
$this->addFieldMapping('planetmath_migrate_score', 'score');
// Unmapped source fields
/**
* I'm not particularly interested in the old
* rating system; let's start ratings over from
* scratch later.
*/
$this->addFieldMapping(NULL, 'karma')
->issueGroup(t('DNM'));
// Unmapped destination fields
// NOTE: it should be possible to infer "name" from forename+surname,
// just need to see how to compute during migration
$this->addUnmigratedDestinations(array('theme',
'timezone',
'language',
'data'));
}
}
name = "PlanetMath Migration"
description = "Testing migration for PlanetMath."
package = "Development"
core = 7.x
dependencies[] = taxonomy
dependencies[] = image
dependencies[] = comment
dependencies[] = migrate
dependencies[] = list
dependencies[] = number
files[] = planetmath.module
files[] = planetmath.inc
; Information added by drupal.org packaging script on 2011-05-26
version = "7.x-2.1"
core = "7.x"
project = "migrate"
datestamp = "1306420317"
<?php
/**
* @file
* Set up the migration baseball example module.
*/
function planetmath_enable() {
Migration::registerMigration('PMUserMigration');
}
function planetmath_uninstall() {
//
}
function planetmath_disable() {
Migration::deregisterMigration('PMUserMigration');
}
<?php
/*
* You must implement hook_migrate_api(), setting the API level to 2, for
* your migration classes to be recognized by the Migrate module.
*/
function planetmath_migrate_api() {
$api = array(
'api' => 2,
);
return $api;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment