Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore Previous State of app on database migration failure (ClickOnce)

Tags:

c#

clickonce

I want to restore previous version of C# application published using ClickOnce, if database migration fails because database wont be latest and it will not support latest version of application.

Details

I am developing an app which will be used locally in remote areas where internet is not available. A person will update his/her app every once in a while by getting internet somehow and then will deploy the app on local network. From there every one will be able to get the updated version of app. What I want now is to use database migration using this app and if the app fails It should restore to previous version. I have already used FluentMigrator for database migration and have used ClickOnce to deploy the app. I have also gone through almost every link over here to see how can I do it. I now know that its not possible using ClickOnce. Can anybody tell me some other way or may be some kind of hack?. I am using ClickOnce because of its auto update feature so don't really want to lose that functionality now. Any help will be appreciated.

like image 559
Safi Mustafa Avatar asked Sep 30 '17 14:09

Safi Mustafa


2 Answers

FluentMigrator keeps track of current version in the database. It also keeps track of latest version in the current app version. Run Migrator function and check if the latest version of Migration files in the current version is equal to the latest version stored in a database. If both are equal then Migration was successful. If they are not equal then you can run the cmd command to directly open (remove or backup) window of the control panel and go to the previous version. This is the best you can do to revert to the previous version using ClickOnce.

try {
     new MigrationsWrapper(AppManager.ConnectionString).MigrateToLatestVersion();
}
catch (Exception ex) 
{

}
LatestVersionNumber = new MigrationsWrapper(AppManager.ConnectionStringADO).LatestVersionNumber;
CurrentVersionNumber = new MigrationsWrapper(AppManager.ConnectionStringADO).CurrentVersionNumber;
if (LatestVersionNumber > CurrentVersionNumber) {

 string applicationName = ConfigurationManager.AppSettings["ApplicationName"].ToString();
 string uninstallString = GetUninstallRegistryKeyByProductName(applicationName);
 if (uninstallString != string.Empty) {
      System.Diagnostics.Process process = new System.Diagnostics.Process();
      System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
      startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
      startInfo.FileName = "cmd.exe";
      startInfo.Arguments = "/c " + uninstallString;
      process.StartInfo = startInfo;
      process.Start();

 }
} else {
 // Successfull
}
like image 105
james Avatar answered Nov 15 '22 18:11

james


So, you want to run previous version of the app if some problem happen during execution.

I don`t know solution for ClickOnce, but there is analogue for it - Squirrel. Bad news that Squirrel has no straight solution too, but it phisically stores previous version of app and you can run it and it works (I just checked it on my app).

So, there is a strategy:

  1. Migrate to the squirrel (they have a tool for it)
  2. in case of emergency - calc path to the stored previous version and run it. Relative path should be like "../app-1.1.1/myApp.exe"

But there is one thing to keep in mind. Squirrel stores previous version only if it upgraded app from it. There is no prev version after first install.

Good luck.

like image 39
Alexus1024 Avatar answered Nov 15 '22 20:11

Alexus1024