Rolling back package installation on error

Permalink
All,

I've built a package that installs a custom page type, some attributes on it, a couple of blocks and a few themes.

While testing I realized that even when Concrete 5 encounters an error during installation, it still places the package under 'Installed'. At this point the package won't work because it's not actually installed correctly.

Is there a way to rollback the installation on error or somehow instruct C5 to do this?

 
12345j replied on at Permalink Reply
12345j
just uninstall and then reinstall the package.
shashi replied on at Permalink Reply
I was wondering how to do this in code. I don't want to bother my users with the uninstall/re-install steps.
12345j replied on at Permalink Reply
12345j
fix the installation errors? sorry i'm not sure I understand the problem.
shashi replied on at Permalink Reply
Sorry sounds like I didn't explain the use case clearly.

What I want to do is detect in my code that the installation of my package encountered an error. I want to catch the error in my code and roll-back any changes I've made to the site so far, also using code.

For example, when I was installing the package earlier today, it threw an error at the point where I was trying to add some AttributeKeys with handles that already existed in the database. By then I had already installed a custom theme and a block.

At this point, instead of continuing the installation, I want to uninstall the theme and the block and undo all changes I've made to the database. When I am done, the database should look like it did before I started the installation.

Normally I do installs and upgrades within transactions or checkpoints so that I can roll-back to a certain point in the installation or roll-back the entire installation. My question is whether there is a way to do this with C5.

If I am doing the installation myself on a test site, I can fix the installation errors. If the package is being installed on another site by someone else, I don't want to leave their site in an inconsistent state.
Mainio replied on at Permalink Reply
Mainio
You should be able to stop the installation process by just throwing the exception further

public function install() {
   $pkg = parent::install();
   try {
      // Your installation
   } catch (Exception $e) {
      $pkg->uninstall();
      // Maybe log your error?
      throw $e;
   }
}


This way the installation view will show an error with the message that the exception contains.

However, I'm not sure whether that works in 100% of the cases. E.g. if your installation just stops on runtime error, it won't probably throw you an exception to catch.

But, if your other code handles exception throwing correctly, it should work ok.


Antti / Mainio