12

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.

Safi Mustafa
  • 515
  • 7
  • 22
  • which database are you using? are you using SQL queries to insert? – Ubiquitous Developers Oct 06 '17 at 11:35
  • I am using Sql server. For Migration I am using FluentMigrator. If somehow it fails I want to restore to previous version of the app. I know I can do it using control panel. But how to control it from the app? – Safi Mustafa Oct 06 '17 at 11:40
  • The answer with the 70 upvotes doesn't work for you? https://stackoverflow.com/questions/199348/how-can-i-roll-back-a-clickonce-application – George Vovos Oct 06 '17 at 13:09
  • @GeorgeVovos I dont have the same scenario as different users can be on different versions. – Safi Mustafa Oct 08 '17 at 10:51
  • I think you try to solve an issue that needs to be approached on a different level. If your updates fail, more testing is needed. If the remote areas are company networks, their IT needs to ensure quality before rollout. – grek40 Oct 11 '17 at 06:55
  • Also it's not really clear, who decides on the downgrade and what needs to be downgraded... in terms of migrations, let me just say: if the upgrade can fail, the downgrade can fail too (and will probably be even less reliable) – grek40 Oct 11 '17 at 06:56
  • @grek40 My Migrations are running transnational queries. So If some thing goes wrong it wont be upgraded. The only thing to do in this case would be to downgrade app version. – Safi Mustafa Oct 11 '17 at 07:13
  • @SafiMustafa are you absolutely sure that a failed migration will keep the before-migration state instead of some partially applied migration? In case the user was 3 migrations behind and only the 3rd migration fails, are the 2 other migrations also rolled back? – grek40 Oct 11 '17 at 07:16
  • @grek40 Yes I have enforced that and I am absolutely sure. – Safi Mustafa Oct 11 '17 at 07:46
  • Does the requirement apply to individual installations within a single local network deployment or would the 'current' version be the same within a local network but different between different local networks? – grek40 Oct 11 '17 at 09:25
  • It can be different in the same local network. – Safi Mustafa Oct 11 '17 at 09:42

3 Answers3

1

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.

Alexus1024
  • 447
  • 5
  • 7
1

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
}
Mushahid Hussain
  • 4,052
  • 11
  • 43
  • 62
0

From the description the different versions of the application are sharing a single database. As a new version of the application goes live on the machine it needs to patch/migrate a shared database. If that fails the application doesn't install. Let's hope it fails without corrupting the database also.

There are clickonce events that can be hoocked into. Create a Custom Installer maybe.

https://msdn.microsoft.com/en-us/library/system.deployment.application.applicationdeployment_events.aspx

https://msdn.microsoft.com/en-us/library/system.deployment.application.applicationdeployment.aspx

Publishing ClickOnce Applications https://msdn.microsoft.com/en-us/library/748fh114.aspx

Walkthrough: Creating a Custom Installer for a ClickOnce Application https://msdn.microsoft.com/en-us/library/dd997001.aspx

Hope that's helpful.

null
  • 429
  • 1
  • 3
  • 10