private async Task <bool> InitializeDatabaseAsync() { bool isSuccess = true; try { var migrator = new DbMigrator(); if (!migrator.DatabaseExists()) { // Create the database if it doesn't exist this.ShowProgressRing = true; LogClient.Info("Creating database"); await Task.Run(() => migrator.InitializeNewDatabase()); } else { // Upgrade the database if it is not the latest version if (migrator.DatabaseNeedsUpgrade()) { this.ShowProgressRing = true; LogClient.Info("Upgrading database"); await Task.Run(() => migrator.UpgradeDatabase()); } } } catch (Exception ex) { LogClient.Error("There was a problem initializing the database. Exception: {0}", ex.Message); this.errorMessage = ex.Message; isSuccess = false; } return(isSuccess); }
private async Task InitializeStorageIfRequiredAsync(string newLocation) { await Task.Run(() => { // Database file var migrator = new DbMigrator(newLocation); if (migrator.DatabaseExists()) { migrator.UpgradeDatabase(); } else { migrator.InitializeNewDatabase(); } // Notes directory string notesDirectoryPath = newLocation; if (!Directory.Exists(notesDirectoryPath)) { Directory.CreateDirectory(notesDirectoryPath); } }); }
private async Task <bool> InitializeNoteStorageDirectoryAsync() { bool isSuccess = true; var migrator = new DbMigrator(); // Move the old "Knowte.db" database file to "Notes\Notes.db". TODO: remove this later (e.g. in version 1.2) try { string oldDatabaseFile = Path.Combine(SettingsClient.ApplicationFolder(), "Knowte.db"); if (File.Exists(oldDatabaseFile) && !File.Exists(migrator.DatabaseFile)) { File.Move(oldDatabaseFile, migrator.DatabaseFile); } } catch (Exception ex) { LogClient.Error("The old database file could not be moved. Exception: {0}", ex.Message); this.errorMessage = ex.Message; isSuccess = false; } // Make sure the default note storage location exists if (!Directory.Exists(ApplicationPaths.DefaultNoteStorageLocation)) { Directory.CreateDirectory(ApplicationPaths.DefaultNoteStorageLocation); } // Further verification is only needed when not using the default storage location if (ApplicationPaths.IsUsingDefaultStorageLocation) { return(true); } try { await Task.Run(() => { if (!Directory.Exists(ApplicationPaths.CurrentNoteStorageLocation)) { LogClient.Warning("Note storage location '{0}' could not be found.", ApplicationPaths.CurrentNoteStorageLocation); isSuccess = false; } if (!migrator.DatabaseExists()) { LogClient.Warning("Database file '{0}' could not be found.", migrator.DatabaseFile); isSuccess = false; } if (!isSuccess) { // Restore the default storage location SettingsClient.Set <string>("General", "NoteStorageLocation", ""); LogClient.Warning("Default note storage location was restored."); // Allow the application to start up after the default storage location was restored isSuccess = true; } }); } catch (Exception ex) { LogClient.Error("There was a problem initializing the note storage location. Exception: {0}", ex.Message); this.errorMessage = ex.Message; isSuccess = false; } return(isSuccess); }