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); }
public async Task MigrateAsync(string sourceFolder, bool deleteDestination) { var sourceFactory = new SQLiteConnectionFactory(sourceFolder); // SQLiteConnectionFactory that points to the source database file var sourceMigrator = new DbMigrator(sourceFolder); // DbMigrator that points to the source database file List <Notebook> sourceNotebooks; List <Database.Entities.Note> sourceNotes; await Task.Run(() => { // Make sure the source database is at the latest version if (sourceMigrator.DatabaseNeedsUpgrade()) { sourceMigrator.UpgradeDatabase(); } // Get source Notebooks and Notes using (var sourceConn = sourceFactory.GetConnection()) { sourceNotebooks = sourceConn.Table <Notebook>().ToList(); sourceNotes = sourceConn.Table <Database.Entities.Note>().ToList(); } // If required, delete all destination Note files. if (deleteDestination) { foreach (string file in Directory.GetFiles(ApplicationPaths.CurrentNoteStorageLocation, "*.xaml")) { File.Delete(file); } } // Restore using (var destinationConn = this.factory.GetConnection()) { // If required, delete all Notebooks and Notes from the destination database. if (deleteDestination) { destinationConn.Query <Notebook>("DELETE FROM Notebook;"); destinationConn.Query <Database.Entities.Note>("DELETE FROM Note;"); } // Get destination Notebook information List <string> destinationNotebookIds = destinationConn.Table <Notebook>().ToList().Select(n => n.Id).ToList(); List <string> destinationNotebookTitles = destinationConn.Table <Notebook>().ToList().Select(n => n.Title).ToList(); // Restore only the Notebooks that don't exist (Id AND Title can't exist at destination. Both need to be unique). foreach (Notebook sourceNotebook in sourceNotebooks) { if (!destinationNotebookIds.Contains(sourceNotebook.Id) && !destinationNotebookTitles.Contains(sourceNotebook.Title)) { destinationConn.Insert(sourceNotebook); } } // Get destination note information List <string> destinationNoteIds = destinationConn.Table <Database.Entities.Note>().ToList().Select(n => n.Id).ToList(); List <string> destinationNoteTitles = destinationConn.Table <Database.Entities.Note>().ToList().Select(n => n.Title).ToList(); // Restore only the Notes which don't exist (Id AND Title can't exist at destination. Both need to be unique). foreach (Database.Entities.Note sourceNote in sourceNotes) { string sourceNoteFile = Path.Combine(sourceFolder, sourceNote.Id + ".xaml"); if (!destinationNoteIds.Contains(sourceNote.Id) && !destinationNoteTitles.Contains(sourceNote.Title)) { File.Copy(sourceNoteFile, Path.Combine(ApplicationPaths.CurrentNoteStorageLocation, sourceNote.Id + ".xaml"), true); destinationConn.Insert(sourceNote); } } // Clear links to missing notebooks destinationConn.Execute("UPDATE Note SET NotebookId = '' WHERE NotebookId NOT IN (SELECT Id FROM Notebook);"); } }); }