示例#1
0
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        /// <summary>
        /// Gets called on App start and performs update task e.g. migrate the DB to a new format.
        /// </summary>
        public static async Task OnAppStartAsync()
        {
            // For debugging:

            /*using (MainDbContext ctx = new MainDbContext())
             * {
             *  if (ctx.Accounts.Count() <= 0)
             *  {
             *      await ctx.RecreateDb();
             *  }
             * }*/
            PackageVersion versionLastStart = GetLastStartedVersion();

            // Check if version != 0.0.0.0 => first ever start of the App:
            if (!(versionLastStart.Major == 0 && versionLastStart.Major == versionLastStart.Minor && versionLastStart.Minor == versionLastStart.Revision && versionLastStart.Revision == versionLastStart.Build) || Settings.GetSettingBoolean(SettingsConsts.INITIALLY_STARTED))
            {
                if (!Compare(versionLastStart, GetPackageVersion()))
                {
                    if (versionLastStart.Major <= 0 && versionLastStart.Minor < 31)
                    {
                        try
                        {
                            Logger.Info("Started updating DB to version 0.31.0.0.");
                            Logger.Info("Clearing old application data...");
                            await ApplicationData.Current.ClearAsync();

                            Logger.Info("Old application data cleared.");
                            Logger.Info("Finished updating DB to version 0.31.0.0.");
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Error during updating DB to version 0.31.0.0", e);
                        }
                    }

                    if (versionLastStart.Major <= 0 && versionLastStart.Minor < 38)
                    {
                        try
                        {
                            Logger.Info("Started updating DB to version 0.38.0.0.");
                            Logger.Info("Clearing old application data...");
                            await ApplicationData.Current.ClearAsync();

                            Logger.Info("Old application data cleared.");
                            Logger.Info("Finished updating DB to version 0.38.0.0.");
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Error during updating DB to version 0.38.0.0", e);
                        }
                    }

                    if (versionLastStart.Major <= 0 && versionLastStart.Minor < 39)
                    {
                        try
                        {
                            Logger.Info("Started updating DB to version 0.39.0.0.");
                            Logger.Info("Resetting the DB...");
                            using (MainDbContext ctx = new MainDbContext())
                            {
                                await ctx.RecreateDb();
                            }
                            Logger.Info("DB reset.");
                            Logger.Info("Finished updating DB to version 0.39.0.0.");
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Error during updating DB to version 0.39.0.0", e);
                        }
                    }

                    if (versionLastStart.Major <= 0 && versionLastStart.Minor < 41)
                    {
                        try
                        {
                            Logger.Info("Started updating DB to version 0.41.0.0.");
                            Logger.Info("Resetting the DB...");
                            using (MainDbContext ctx = new MainDbContext())
                            {
                                if (!ctx.ApplyMigration(new Migration_0_41_0_0()))
                                {
#if !DEBUG
                                    Logger.Error("Since migration to the new DB failed for version 0.41.0.0, we are recreating the DB");
                                    await ctx.RecreateDb();
#endif
                                }
                            }
                            Logger.Info("DB reset.");
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Error during updating DB to version 0.41.0.0", e);
                        }

                        Logger.Info("Cleaning up old password vault entries...");
                        using (MainDbContext ctx = new MainDbContext())
                        {
                            Vault.CleanupVault(ctx.Accounts.Select(a => a.bareJid).ToList());
                        }
                        Logger.Info("Password vauled cleaned up.");
                        Logger.Info("Finished updating DB to version 0.41.0.0.");
                    }
                }
            }
            SetVersion(GetPackageVersion());
        }