/// <summary> /// Updates database if needed. /// </summary> /// <param name="backupBeforeUpdate">If true, backups database before udpating.</param> public async Task UpdateDatabaseAsync() { Version databaseVersion = VersionAccess.GetVersionInfo(); if (databaseVersion < new Version(1, 4, 0, 0)) { if (isDatabaseRecreated) { throw new Exception(string.Format("InitialDatabase version {0} is outdated.", databaseVersion.ToString())); } string Msg = string.Format("Database v{0} is outdated. Do you wish to delete it and recreate an updated database? All your personal data will be lost.", databaseVersion.ToString(3)); if (MessageBox.Show(owner, Msg, "Database Update", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) { throw new Exception(string.Format("Database is outdated.")); } GC.Collect(); await TryUntilTimeout(() => FileOperationAPIWrapper.MoveToRecycleBin(AppPaths.DatabasePath), 10000); isDatabaseRecreated = true; await EnsureAvailableAsync(); // UpdateDatabaseToVersion(backupBeforeUpdate, 0, 9, 3, 0); } }
/// <summary> /// Checks server and database availability. /// If there is no database file, copy and attach initial database. /// Update database if it is outdated. /// </summary> /// <remarks>If connection fails, the exception must be handled by the caller.</remarks> public async Task EnsureAvailableAsync() { Version CurrentVersion = null; try { // Test query. CurrentVersion = await Task.Run(() => VersionAccess.GetVersionInfo()); } catch { } if (CurrentVersion == null) { // Check if database exists. If it doesn't, create blank database. if (!File.Exists(AppPaths.DatabasePath) || new FileInfo(AppPaths.DatabasePath).Length == 0) { await CreateNewDatabaseAsync(); } await TryUntilTimeout(() => VersionAccess.GetVersionInfo(), 10000); } // If database connection is successfull, ensure database file is up to date. await UpdateDatabaseAsync(); }