2

Say I want to stop a migration that I'd like to stop on preImport under similar circumstances as a dependency not being fulfilled, is there a way to do this?

I would like to stop the migration and prevent postImport from running, preferably from preImport().

public function preImport() {
  parent::preImport();
  if($this->externalDependency == 0){
  // Stop the migration, and maybe even throw an error.
  }
}
3
  • 2
    throw a MigrateException maybe?
    – Clive
    Oct 23, 2014 at 14:26
  • That's a route I guess I'll have to try out. It's strange, because all of migrate has pretty good documentation, but I see no mention of anything anywhere regarding the answer to my question. Oct 23, 2014 at 14:27
  • 1
    See line 259 of base.inc, I suspect the message level helps to control what action is taken when it's raised
    – Clive
    Oct 23, 2014 at 14:31

2 Answers 2

2

According to my comment on Luke solution, here is the complete code to interrupt you Migration in Migrate these days.

public function preImport() {
  parent::preImport();
  if($this->externalDependency == 0){
  throw new MigrateException('Dependencies were unfulfilled.');
  }
}

public function postImport() {
  parent::postImport();
  if($this->externalDependency == 0){
  return false;
  }
  // My code here
}

public function processImport(array $options = array()) {
  try {
    $result = parent::processImport($options);
    return $result;
  }
  catch (MigrateException $e) {
    $this->endProcess();
    drupal_set_message($e->getMessage(), 'error');
    return MigrationBase::RESULT_STOPPED;
  }
}
0

MigrateException is the answer (kudos to Clive).
postImport() still runs even after the exception was thrown, so I just have to do the same check on postImport like so.

public function preImport() {
  parent::preImport();
  if($this->externalDependency == 0){
  throw new MigrateException('Dependencies were unfulfilled.');
  }
}

public function postImport() {
  parent::postImport();
  if($this->externalDependency == 0){
  return false;
  }
  // My code here
}
1
  • FYI it does not work anymore. preImport() call is not wrapped in a try/ctach block in Migrate 2.7 If you still want to interrupt your migration at this moment, you also have to overwrite processImport() and wrap a parent::processImport() call in your own try/catch.
    – DuaelFr
    Aug 19, 2015 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.