/// <summary> /// Updates the database version. /// </summary> /// <param name="asyncDbConnection">The asynchronous database connection.</param> /// <param name="databaseVersion">The current database version.</param> public static async Task UpdateAsync(SQLiteAsyncConnection asyncDbConnection, DatabaseVersion databaseVersion) { Logger.Debug($"Updating database version to {databaseVersion.Version}..."); await asyncDbConnection.UpdateWithChildrenAsync(databaseVersion).ConfigureAwait(false); }
/// <summary> /// Initializes the database. /// </summary> public async Task InitDatabaseAsync() { if(IsInitialized) { return; } Logger.Info("Initializing database..."); DatabaseVersion newVersion = new DatabaseVersion { Version = CurrentDatabaseVersion }; await Connection.LockAsync().ConfigureAwait(false); try { var databaseVersionTableInfo = Connection.Connection.GetTableInfo("DatabaseVersion"); if(!databaseVersionTableInfo.Any()) { Logger.Debug("Creating a new database..."); await DatabaseVersion.CreateTablesAsync(Connection.AsyncConnection).ConfigureAwait(false); } DatabaseVersion oldVersion = await DatabaseVersion.GetAsync(Connection.AsyncConnection).ConfigureAwait(false); if(null == oldVersion) { Logger.Debug("DatabaseVersion.Get returned null!?!"); throw new InvalidOperationException("DatabaseVersion.Get returned null!?!"); } Logger.Debug($"Old database version: {oldVersion.Version}, current database version: {CurrentDatabaseVersion}"); // TODO: find a way to make this transactional // so that we can roll it back on error and avoid updating the db version await GearItem.InitDatabaseAsync(Connection.AsyncConnection, oldVersion.Version, newVersion.Version).ConfigureAwait(false); await GearSystem.InitDatabaseAsync(Connection.AsyncConnection, oldVersion.Version, newVersion.Version).ConfigureAwait(false); await GearCollection.InitDatabaseAsync(Connection.AsyncConnection, oldVersion.Version, newVersion.Version).ConfigureAwait(false); await Meal.InitDatabaseAsync(Connection.AsyncConnection, oldVersion.Version, newVersion.Version).ConfigureAwait(false); await TripItinerary.InitDatabaseAsync(Connection.AsyncConnection, oldVersion.Version, newVersion.Version).ConfigureAwait(false); await TripPlan.InitDatabaseAsync(Connection.AsyncConnection, oldVersion.Version, newVersion.Version).ConfigureAwait(false); newVersion.DatabaseVersionId = oldVersion.DatabaseVersionId; await DatabaseVersion.UpdateAsync(Connection.AsyncConnection, newVersion).ConfigureAwait(false); } finally { Connection.Release(); } IsInitialized = true; }