public void RollbackToVersionZeroShouldDeleteVersionInfoTable() { Assert.NotNull(_runner.VersionLoader.VersionTableMetaData.TableName); _runner.RollbackToVersion(0); _fakeVersionLoader.DidRemoveVersionTableGetCalled.ShouldBeTrue(); }
public void CanMigrateASpecificVersionDown() { try { ExecuteWithSupportedProcessors(processor => { MigrationRunner runner = SetupMigrationRunner(processor); runner.MigrateUp(1); runner.VersionLoader.VersionInfo.HasAppliedMigration(1).ShouldBeTrue(); processor.TableExists(null, "Users").ShouldBeTrue(); }, false, typeof(SqliteProcessor)); ExecuteWithSupportedProcessors(processor => { MigrationRunner testRunner = SetupMigrationRunner(processor); testRunner.MigrateDown(0); testRunner.VersionLoader.VersionInfo.HasAppliedMigration(1).ShouldBeFalse(); processor.TableExists(null, "Users").ShouldBeFalse(); }, false, typeof(SqliteProcessor)); } finally { ExecuteWithSupportedProcessors(processor => { MigrationRunner testRunner = SetupMigrationRunner(processor); testRunner.RollbackToVersion(0); }, false); } }
public void MigrateUpWithTaggedMigrationsShouldOnlyApplyMatchedMigrations() { ExecuteWithSupportedProcessors(processor => { var assembly = typeof(TenantATable).Assembly; var runnerContext = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = typeof(TenantATable).Namespace, Tags = new[] { "TenantA" } }; var runner = new MigrationRunner(assembly, runnerContext, processor); try { runner.MigrateUp(false); processor.TableExists(null, "TenantATable").ShouldBeTrue(); processor.TableExists(null, "NormalTable").ShouldBeTrue(); processor.TableExists(null, "TenantBTable").ShouldBeFalse(); processor.TableExists(null, "TenantAandBTable").ShouldBeTrue(); } finally { runner.RollbackToVersion(0); } }); }
public void CanSilentlyFail() { try { var processorOptions = new Mock <IMigrationProcessorOptions>(); processorOptions.SetupGet(x => x.PreviewOnly).Returns(false); var processor = new Mock <IMigrationProcessor>(); processor.Setup(x => x.Process(It.IsAny <CreateForeignKeyExpression>())).Throws(new Exception("Error")); processor.Setup(x => x.Process(It.IsAny <DeleteForeignKeyExpression>())).Throws(new Exception("Error")); processor.Setup(x => x.Options).Returns(processorOptions.Object); var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor.Object) { SilentlyFail = true }; runner.Up(new TestForeignKeySilentFailure()); runner.CaughtExceptions.Count.ShouldBeGreaterThan(0); runner.Down(new TestForeignKeySilentFailure()); runner.CaughtExceptions.Count.ShouldBeGreaterThan(0); } finally { ExecuteWithSupportedProcessors(processor => { MigrationRunner testRunner = SetupMigrationRunner(processor); testRunner.RollbackToVersion(0); }, false); } }
public static void MigrateDown(string connectionString, long?migrationId) { var announcer = GetAnnouncer(); var assembly = Assembly.GetAssembly(typeof(Seed)); var migrationContext = new RunnerContext(announcer) { Namespace = typeof(Seed).Namespace }; var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 }; var factory = new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory(); using (var processor = factory.Create(connectionString, announcer, options)) { var runner = new MigrationRunner(assembly, migrationContext, processor); if (migrationId.HasValue) { runner.RollbackToVersion(migrationId.Value); } else { runner.Rollback(int.MaxValue); } } }
public void ValidateVersionOrderShouldThrowExceptionIfUnappliedMigrationVersionIsLessThanLatestAppliedMigration() { // Using SqlServer instead of SqlLite as versions not deleted from VersionInfo table when using Sqlite. var excludedProcessors = new[] { typeof(SqliteProcessor), typeof(MySqlProcessor), typeof(PostgresProcessor) }; var assembly = typeof(User).Assembly; var runnerContext1 = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = typeof(Migrations.Interleaved.Pass2.User).Namespace }; var runnerContext2 = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = typeof(Migrations.Interleaved.Pass3.User).Namespace }; VersionOrderInvalidException caughtException = null; try { ExecuteWithSupportedProcessors(processor => { var migrationRunner = new MigrationRunner(assembly, runnerContext1, processor); migrationRunner.MigrateUp(); }, false, excludedProcessors); ExecuteWithSupportedProcessors(processor => { var migrationRunner = new MigrationRunner(assembly, runnerContext2, processor); migrationRunner.ValidateVersionOrder(); }, false, excludedProcessors); } catch (VersionOrderInvalidException ex) { caughtException = ex; } finally { ExecuteWithSupportedProcessors(processor => { var migrationRunner = new MigrationRunner(assembly, runnerContext2, processor); migrationRunner.RollbackToVersion(0); }, true, excludedProcessors); } caughtException.ShouldNotBeNull(); caughtException.InvalidMigrations.Count().ShouldBe(1); var keyValuePair = caughtException.InvalidMigrations.First(); keyValuePair.Key.ShouldBe(200909060935); ((MigrationWithMetaDataAdapter)keyValuePair.Value).Migration.ShouldBeOfType <UserEmail>(); }
private static void CleanupTestSqlServerDatabase(SqlConnection connection, SqlServerProcessor origProcessor) { if (origProcessor.WasCommitted) { connection.Close(); var cleanupProcessor = new SqlServerProcessor(connection, new SqlServer2008Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions(), new SqlServerDbFactory()); MigrationRunner cleanupRunner = SetupMigrationRunner(cleanupProcessor); cleanupRunner.RollbackToVersion(0); } else { origProcessor.RollbackTransaction(); } }
public void CanRunMigrations() { ExecuteWithSupportedProcessors(processor => { MigrationRunner runner = SetupMigrationRunner(processor); runner.MigrateUp(false); runner.VersionLoader.VersionInfo.HasAppliedMigration(1).ShouldBeTrue(); runner.VersionLoader.VersionInfo.HasAppliedMigration(2).ShouldBeTrue(); runner.VersionLoader.VersionInfo.HasAppliedMigration(3).ShouldBeTrue(); runner.VersionLoader.VersionInfo.HasAppliedMigration(4).ShouldBeTrue(); runner.VersionLoader.VersionInfo.Latest().ShouldBe(4); runner.RollbackToVersion(0, false); }); }
public void CanMigrateASpecificVersion() { ExecuteWithSupportedProcessors(processor => { MigrationRunner runner = SetupMigrationRunner(processor); try { runner.MigrateUp(1, false); runner.VersionLoader.VersionInfo.HasAppliedMigration(1).ShouldBeTrue(); processor.TableExists(null, "Users").ShouldBeTrue(); } finally { runner.RollbackToVersion(0, false); } }); }
public void RollbackAllShouldRemoveVersionInfoTable() { ExecuteWithSupportedProcessors(processor => { MigrationRunner runner = SetupMigrationRunner(processor); runner.MigrateUp(2); processor.TableExists(runner.VersionLoader.VersionTableMetaData.SchemaName, runner.VersionLoader.VersionTableMetaData.TableName).ShouldBeTrue(); }); ExecuteWithSupportedProcessors(processor => { MigrationRunner runner = SetupMigrationRunner(processor); runner.RollbackToVersion(0); processor.TableExists(runner.VersionLoader.VersionTableMetaData.SchemaName, runner.VersionLoader.VersionTableMetaData.TableName).ShouldBeFalse(); }); }
public void ValidateVersionOrderShouldDoNothingIfUnappliedMigrationVersionIsGreaterThanLatestAppliedMigration() { // Using SqlServer instead of SqlLite as versions not deleted from VersionInfo table when using Sqlite. var excludedProcessors = new[] { typeof(SqliteProcessor), typeof(MySqlProcessor), typeof(PostgresProcessor) }; var assembly = typeof(User).Assembly; var runnerContext1 = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = typeof(Migrations.Interleaved.Pass2.User).Namespace }; var runnerContext2 = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = typeof(Migrations.Interleaved.Pass3.User).Namespace }; try { ExecuteWithSupportedProcessors(processor => { var migrationRunner = new MigrationRunner(assembly, runnerContext1, processor); migrationRunner.MigrateUp(3); }, false, excludedProcessors); ExecuteWithSupportedProcessors(processor => { var migrationRunner = new MigrationRunner(assembly, runnerContext2, processor); Assert.DoesNotThrow(migrationRunner.ValidateVersionOrder); }, false, excludedProcessors); } finally { ExecuteWithSupportedProcessors(processor => { var migrationRunner = new MigrationRunner(assembly, runnerContext2, processor); migrationRunner.RollbackToVersion(0); }, true, excludedProcessors); } }