Less commonly implemented Migration methods
pre/post Import/Rollback methods
You can execute code before or after an import or rollback operation by implementing the Migration class methods preImport(), postImport(), preRollback(), or postRollback(). Each of these is called once per individual Migration - that is, if you implement preImport() in an article node migration class, it will be called after any previous migrations you're running, and immediately before the first row of your article node migration is called.
Typical actions can be the enabling / disabling of certain modules or changing configurations.
class ArticleMigration implements Migration {
...
public function preImport() {
parent::preImport();
// Code to execute before first article row is imported
}
...
public function postImport() {
parent::postImport();
// Code to execute after the last article row has been imported
}
...
public function preRollback() {
parent::preRollback();
// Code to execute before first article row is deleted
}
...
public function postRollback() {
parent::postRollback();
// Code to execute after the last article row has been deleted
}
A practical example can be found here.
IMPORTANT NOTE: If you are running your migrations through the web UI, which chunks the work into batches, if a given migration takes long enough to be broken into separate batches, these functions will be called for each batch. Also, if you have a max_execution_time set in your console, or run into memory limits, drush exhibits the same behavior. They are called each time the migration is invoked, not just the first (or last, in the complete case).
pre/post Rollback methods per Entity
The prepareRollback() and completeRollback() methods are also called from every destination handler that uses bulkRollback(). The difference from the ones above is that an array of all entity ids are passed to them, which allows you to perform operations related to the individual entities, such as deleting redirects creating during import.
class ArticleMigration implements Migration {
...
public function prepareRollback($entity_ids) {
parent::prepareRollback();
// Code to execute before an entity has been rolled back
}
...
public function completeRollback($entity_ids) {
parent::completeRollback();
// Code to execute after an entity has been rolled back.
}
...
prepareKey method
The method prepareKey() is called from the source plugin immediately after retrieving the raw data from the source and before calling prepareRow() - by default, it simply assigns the key values based on the field names passed to MigrateSQLMap(). Override this if you need to generate your own key (e.g., the source doesn't have a natural unique key). Be sure to also set any values you generate in $row. Example:
/**
* Prepare a proper unique key.
*/
public function prepareKey($source_key, $row) {
$key = array();
$row->board_id = $row->cmsid . $row->board_desc_clean;
$key['board_id'] = $row->board_id;
return $key;
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion