Package upgrade / throw new exception / version number change

Permalink
Hi there

This is my packages upgrade function
public function upgrade(){
   parent::upgrade();
   $pkg = Package::getByHandle('steff_travels'); 
   Loader::model('job');
   if(DB_TYPE != 'mysqlt'){
      throw new Exception(t('You must set DB_TYPE to mysqlt in site.php.'));
   }      
   //install job
   if(!Job::getByHandle('get_travels')){
      Job::installByPackage('get_travels', $pkg);
   }   
}


As you can see, i am checking if DB_TYPE is defined in site.php. If not, I'm throwing a new Exception. As far as good. If DB_TYPE is not defined or is not mysqlt, the Exception is thrown and the Job is not installed.
BUT => the blocks Version number has changed to the new version number out of the controller.

Example:
I have installed version 0.9.7.
Controller has 0.9.8. If i now start the upgrade, without defined DB_TYPE, the Exception is thrown and the Job is not installed. This is absolutely correct. But the package has now version 0.9.8.

So... How is it possible to get the old version back, if an exception is thrown?

Thanks a lot.
Steff

Steff
 
jbx replied on at Permalink Reply
jbx
Yeah, I came across this the other day.
You have already run parent::upgrade(), so you have already upgraded your package and updated the version number.

You need to perform your check right at the start - before you do anything else:
public function upgrade(){
   if(DB_TYPE != 'mysqlt'){
      throw new Exception(t('You must set DB_TYPE to mysqlt in site.php.'));
   }
   parent::upgrade();
   $pkg = Package::getByHandle('steff_travels'); 
   Loader::model('job');      
   //install job
   if(!Job::getByHandle('get_travels')){
      Job::installByPackage('get_travels', $pkg);
   }   
}

You may also need to exit() after throwing the exception, but not sure...

Jon
Steff replied on at Permalink Reply
Steff
Hi Jon

Thanks. But unfortunately it doesn't work. I tried with exit(). Doesn't work.

I also tried this
public function upgrade(){
   $err = false;
   if(DB_TYPE != 'mysqlt'){
      throw new Exception(t('You must set DB_TYPE to mysqlt in site.php.'));
      $err = true;
   }
   if($err == false){
      parent::upgrade();
      $pkg = Package::getByHandle('steff_travels'); 
      Loader::model('job');
      //install job
      if(!Job::getByHandle('get_travels')){
         Job::installByPackage('get_travels', $pkg);
      }
   }


no luck.

other ideas?
Mainio replied on at Permalink Best Answer Reply
Mainio
The version number upgrade is actually run in the following package function:
public function upgradeCoreData() {
   $error = true;
   if ($error) throw new Exception(t("Will not upgrade version!"));
   parent::upgradeCoreData();
}


This way you can also find out what the version number was before the upgrade because $this->pkgCurrentVersion contains the OLD version number before running the parent::upgradeCoreData().


Antti / Mainio
Steff replied on at Permalink Reply
Steff
OK. That's great.

But what is the difference from upgrade to upgradeCoreData?

Thanks
Mainio replied on at Permalink Reply
Mainio
That's just a function that should not be used by package developers but this is just something that's missing from the core so sometimes you need to use that.

The upgradeCoreData() is just for updating the package information, check the function from /concrete/models/package.php.


Antti
Steff replied on at Permalink Reply
Steff
Thanks Antti

I'll check the function.