Пример #1
0
        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();
            });
        }
Пример #2
0
        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
            });
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
            });
        }