public void ApplyMigrations() { long lastMigrationOrder = 0; if (!dbQuery.TableExists(RowDataGatewayHelper.GetTableName(typeof(MigrationHistoryGateway)))) { new AddedMigrationHistoryEntity { DbQuery = dbQuery }.Up(); } else { lastMigrationOrder = GetLastMigrationOrder(); } var migrations = migrationProvider.GetMigrationsAfter(lastMigrationOrder); HandleMigrations(migrations, (migration, type) => { migration.Up(); new MigrationHistoryGateway(dbQuery) { MigrationName = type.Name, MigrationOrder = DbMigrationHelper.GetMigrationOrder(type) }.Save(); }); }
public void FindAndDeleteMigrationsAfter(long migrationOrder) { var tableName = RowDataGatewayHelper.GetTableName(typeof(MigrationHistoryGateway)); var propertyName = nameof(MigrationHistoryGateway.MigrationOrder); dbQuery.ExecuteCommand($"delete from {tableName} where {propertyName} > @{propertyName}", new SqlParameter { ParameterName = $"@{propertyName}", Value = migrationOrder }); }
public MigrationHistoryGateway FindLastMigration() { var tableName = RowDataGatewayHelper.GetTableName(typeof(MigrationHistoryGateway)); var byProperty = nameof(MigrationHistoryGateway.MigrationOrder); var instance = new MigrationHistoryGateway(dbQuery); var result = dbQuery.ExecuteReader($"select * from {tableName} where {byProperty} = (select MAX({byProperty}) from {tableName})", instance.Load); if (!result) { return(null); } return(instance); }
public void Downgrade(long toMigrationOrder) { if (!dbQuery.TableExists(RowDataGatewayHelper.GetTableName(typeof(MigrationHistoryGateway)))) { throw new InvalidOperationException("Migration history wasn't found."); } var migrations = migrationProvider.GetMigrationsAfter(toMigrationOrder); HandleMigrations(migrations, (migration, type) => { migration.Down(); gatewayFinder.FindAndDeleteMigrationsAfter(toMigrationOrder); }); }