Stopping a package upgrade if requirements aren't met?

Permalink
I'm working on a package, and I need to now install an attribute type from another package. If that attribute type is installed, I want to perform the upgrade and create an attribute for the package. Otherwise, I want to return false and not upgrade the package. This is the code that I'm using:

public function upgrade() {
          $haveGeolocation = 0;
      $packages = Package::getInstalledList();
      foreach ($packages as $package){
         $handle = $package->getPackageHandle();
         if ($handle == "geolocation_attribute"){
            $haveGeolocation = 1;
         }
      }
          if ($haveGeolocation){
               $this->installGeolocationAttribute($pkg);
               parent::upgrade();
          } else {
               $message = t("Requirements not met, you must have the geolocation attribute installed.");
         throw new Exception($message);


When I try to run the upgrade, it shows the "Requirements not met" message, but it also increments the package version in the database and marks it as upgraded. Is there a different way to do this so that it won't mark the package as upgraded?

hereNT
 
hereNT replied on at Permalink Best Answer Reply
hereNT
I found there's upgrade and upgradeCoreData in the package controller. Returning an exception from upgradeCoreData worked:

public function upgradeCoreData() {
     $haveGeolocation = 0;
     $packages = Package::getInstalledList();
     foreach ($packages as $package){
          $handle = $package->getPackageHandle();
          if ($handle == "geolocation_attribute"){
               $haveGeolocation = 1;
          }
     }
     if ($haveGeolocation){
          parent::upgradeCoreData();
     } else {
          $message = t("Requirements not met, you must have the geolocation attribute installed.");
          throw new Exception($message);
          exit;