public virtual void ApplyMigrations(string targetMigration = null) { var connection = _connection.DbConnection; _logger.Value.LogVerbose(Strings.UsingConnection(connection.Database, connection.DataSource)); var migrations = _migrationAssembly.Migrations; var appliedMigrationEntries = _historyRepository.GetAppliedMigrations(); var appliedMigrations = new List <Migration>(); var unappliedMigrations = new List <Migration>(); foreach (var migraion in migrations) { if (appliedMigrationEntries.Any( e => string.Equals(e.MigrationId, migraion.Id, StringComparison.OrdinalIgnoreCase))) { appliedMigrations.Add(migraion); } else { unappliedMigrations.Add(migraion); } } IEnumerable <Migration> migrationsToApply; IEnumerable <Migration> migrationsToRevert; if (string.IsNullOrEmpty(targetMigration)) { migrationsToApply = unappliedMigrations; migrationsToRevert = Enumerable.Empty <Migration>(); } else if (targetMigration == InitialDatabase) { migrationsToApply = Enumerable.Empty <Migration>(); migrationsToRevert = appliedMigrations; } else { targetMigration = _idGenerator.ResolveId(targetMigration, migrations); migrationsToApply = unappliedMigrations .Where(m => string.Compare(m.Id, targetMigration, StringComparison.OrdinalIgnoreCase) <= 0); migrationsToRevert = appliedMigrations .Where(m => string.Compare(m.Id, targetMigration, StringComparison.OrdinalIgnoreCase) > 0) .OrderByDescending(m => m.Id); } bool first; var checkFirst = true; foreach (var migration in migrationsToApply) { var batches = ApplyMigration(migration).ToList(); first = false; if (checkFirst) { first = migration == migrations[0]; if (first && !_historyRepository.Exists()) { // TODO: Consider removing check above and always using "if not exists" batches.Insert(0, new SqlBatch(_historyRepository.Create(ifNotExists: false))); } checkFirst = false; } _logger.Value.LogInformation(Strings.ApplyingMigration(migration.Id)); Execute(batches, first); } foreach (var migration in migrationsToRevert) { _logger.Value.LogInformation(Strings.RevertingMigration(migration.Id)); Execute(RevertMigration(migration)); } }
public virtual void Migrate(string targetMigration = null) { var connection = _connection.DbConnection; _logger.Value.LogVerbose(Strings.UsingConnection(connection.Database, connection.DataSource)); var migrations = _migrationAssembly.Migrations; var appliedMigrationEntries = _historyRepository.GetAppliedMigrations(); var appliedMigrations = new List <Migration>(); var unappliedMigrations = new List <Migration>(); foreach (var migraion in migrations) { if (appliedMigrationEntries.Any( e => string.Equals(e.MigrationId, migraion.Id, StringComparison.OrdinalIgnoreCase))) { appliedMigrations.Add(migraion); } else { unappliedMigrations.Add(migraion); } } IReadOnlyList <Migration> migrationsToApply; IReadOnlyList <Migration> migrationsToRevert; if (string.IsNullOrEmpty(targetMigration)) { migrationsToApply = unappliedMigrations; migrationsToRevert = new Migration[0]; } else if (targetMigration == Migration.InitialDatabase) { migrationsToApply = new Migration[0]; migrationsToRevert = appliedMigrations.OrderByDescending(m => m.Id).ToList(); } else { targetMigration = _migrationAssembly.GetMigration(targetMigration).Id; migrationsToApply = unappliedMigrations .Where(m => string.Compare(m.Id, targetMigration, StringComparison.OrdinalIgnoreCase) <= 0) .ToList(); migrationsToRevert = appliedMigrations .Where(m => string.Compare(m.Id, targetMigration, StringComparison.OrdinalIgnoreCase) > 0) .OrderByDescending(m => m.Id) .ToList(); } bool first; var checkFirst = true; foreach (var migration in migrationsToApply) { var batches = new List <SqlBatch>(GenerateUpSql(migration)); first = false; if (checkFirst) { first = migration == migrations[0]; if (first && !_historyRepository.Exists()) { // TODO: Prepend to first batch instead batches.Insert(0, new SqlBatch(_historyRepository.GetCreateScript())); } checkFirst = false; } _logger.Value.LogInformation(Strings.ApplyingMigration(migration.Id)); Execute(batches, first); } for (var i = 0; i < migrationsToRevert.Count; i++) { var migration = migrationsToRevert[i]; _logger.Value.LogInformation(Strings.RevertingMigration(migration.Id)); Execute(GenerateDownSql( migration, i != migrationsToRevert.Count - 1 ? migrationsToRevert[i + 1] : null)); } }