public void CanRenameTableWithSchema() { ExecuteWithSupportedProcessors( processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor); runner.Up(new TestCreateSchema()); runner.Up(new TestCreateAndDropTableMigrationWithSchema()); processor.TableExists("TestSchema", "TestTable2").ShouldBeTrue(); runner.Up(new TestRenameTableMigrationWithSchema()); processor.TableExists("TestSchema", "TestTable2").ShouldBeFalse(); processor.TableExists("TestSchema", "TestTable'3").ShouldBeTrue(); runner.Down(new TestRenameTableMigrationWithSchema()); processor.TableExists("TestSchema", "TestTable'3").ShouldBeFalse(); processor.TableExists("TestSchema", "TestTable2").ShouldBeTrue(); runner.Down(new TestCreateAndDropTableMigrationWithSchema()); processor.TableExists("TestSchema", "TestTable2").ShouldBeFalse(); runner.Down(new TestCreateSchema()); //processor.CommitTransaction(); }); }
public void CanRenameColumnWithSchema() { ExecuteWithSupportedProcessors( processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor); runner.Up(new TestCreateSchema()); runner.Up(new TestCreateAndDropTableMigrationWithSchema()); processor.ColumnExists("TestSchema", "TestTable2", "Name").ShouldBeTrue(); runner.Up(new TestRenameColumnMigrationWithSchema()); processor.ColumnExists("TestSchema", "TestTable2", "Name").ShouldBeFalse(); processor.ColumnExists("TestSchema", "TestTable2", "Name'3").ShouldBeTrue(); runner.Down(new TestRenameColumnMigrationWithSchema()); processor.ColumnExists("TestSchema", "TestTable2", "Name'3").ShouldBeFalse(); processor.ColumnExists("TestSchema", "TestTable2", "Name").ShouldBeTrue(); runner.Down(new TestCreateAndDropTableMigrationWithSchema()); processor.ColumnExists("TestSchema", "TestTable2", "Name").ShouldBeFalse(); runner.Down(new TestCreateSchema()); }, true, typeof(SqliteProcessor)); }
public void CanPassApplicationContext() { IMigration migration = new TestEmptyMigration(); _runner.Up(migration); Assert.AreEqual(_applicationContext, _runnerContextMock.Object.ApplicationContext, "The runner context does not have the expected application context."); Assert.AreEqual(_applicationContext, _runner.RunnerContext?.ApplicationContext, "The MigrationRunner does not have the expected application context."); Assert.AreEqual(_applicationContext, migration.ApplicationContext, "The migration does not have the expected application context."); _announcer.VerifyAll(); }
public static void Main(string[] args) { var runner = new MigrationRunner("mongodb://localhost:27017/TestMigrations", Assembly.GetExecutingAssembly(), null); // Run all collection specific migrations runner.Up("Animals"); // Run all existing migrations runner.Up(); // Roll back all applied collection specific migrations runner.Down("Animals"); // Roll back all applied migrations runner.Down(); }
public void VerifyTestMigrationSchema() { //run TestMigration migration, read, then remove... var runnerContext = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = typeof(TestMigration).Namespace }; var runner = new MigrationRunner(typeof(TestMigration).Assembly, runnerContext, Processor); runner.Up(new TestMigration()); //read schema here IList <TableDefinition> defs = SchemaDumper.ReadDbSchema(); var testWriter = new SchemaTestWriter(); var output = GetOutput(testWriter, defs); string expectedMessage = testWriter.GetMessage(4, 10, 4, 1); runner.Down(new TestMigration()); runner.VersionLoader.RemoveVersionTable(); //test output.ShouldBe(expectedMessage); }
/// <summary> //// Alteracoes grandes no banco de dados, que altera a estrutura devera ficar aqui. //// afim de evitar quebra de mapeamento de modelo/banco de dados. /// </summary> public void Migrations() { //var collection = GetUsersCollection(); //var services = collection.Find(f => f.Professional.Services.Any(s => s.Id.Equals(Guid.Empty))); //var filter = Builders<IlevusUser>.Filter.Where(x => x.Professional.Services.Any(y => y.Id == Guid.Empty)); //var update = Builders<IlevusUser>.Update.Set(x => x.Professional.Services.ElementAt(-1).Id, Guid.NewGuid()); //var result = collection.UpdateManyAsync(filter, update).Result; var runner = new MigrationRunner("localhost:27017", "ilevus", Assembly.GetExecutingAssembly().CodeBase); runner.Up(); //MongoMigration.Initialize(); //RemoveField("Financial"); //RemoveField("Professional.BirthDate"); //RemoveField("Professional.StreetNumber"); //RemoveField("Professional.Financial"); //RemoveField("Professional.Phone"); //RemoveField("Professional.BankAccount"); //var msgs = GetSystemMessagesCollection(); //FieldDefinition<sys> field = "Messages.TextFooterContent"; //var filter2 = Builders<SystemMessages>.Filter.Exists(field); //var update2 = Builders<SystemMessages>.Update.Set(field, new SystemLabel()); //collection.UpdateMany(filter, update); }
public void CanUseCustomVersionInfoDefaultSchema() { ExecuteWithSupportedProcessors(processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations.Interleaved.Pass3" }, processor); IVersionTableMetaData tableMetaData = new TestVersionTableMetaData { SchemaName = null }; //ensure table doesn't exist if (processor.TableExists(tableMetaData.SchemaName, tableMetaData.TableName)) { runner.Down(new VersionMigration(tableMetaData)); } runner.Up(new VersionMigration(tableMetaData)); processor.TableExists(null, tableMetaData.TableName).ShouldBeTrue(); runner.Down(new VersionMigration(tableMetaData)); processor.TableExists(null, tableMetaData.TableName).ShouldBeFalse(); }); }
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 void AlterTable_MigrationRequiresAutomaticDelete_AndProcessorHasUndoDisabled_ShouldNotThrow() { var tempResources = WriteOutFirebirdEmbeddedLibrariesToCurrentWorkingDirectory(); var tempFile = Path.GetTempFileName(); using (var deleter = new AutoDeleter(tempFile)) { File.Delete(tempFile); deleter.Add(tempResources); var connectionString = GetConnectionStringToTempDatabaseAt(tempFile); var runnerContext = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations" }; try { using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); options.TruncateLongNames = false; processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichCreatesTwoRelatedTables()); processor.CommitTransaction(); FbConnection.ClearPool(connection); } //---------------Assert Precondition---------------- Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after first migration"); using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichAltersTableWithFK()); processor.CommitTransaction(); } Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after second migration"); } catch (Exception ex) { try { File.Copy(tempFile, "C:\\tmp\\fm_tests.fdb", true); } catch { } throw ex; } } }
public void CanUseCustomVersionInfo() { ExecuteWithSupportedProcessors(processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), new RunnerContext(new TextWriterAnnouncer(TestContext.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations.Interleaved.Pass3" }, processor); var tableMetaData = new TestVersionTableMetaData(); //ensure table doesn't exist if (processor.TableExists(tableMetaData.SchemaName, tableMetaData.TableName)) { runner.Down(new VersionMigration(tableMetaData)); } //ensure schema doesn't exist if (processor.SchemaExists(tableMetaData.SchemaName)) { runner.Down(new VersionSchemaMigration(tableMetaData)); } runner.Up(new VersionSchemaMigration(tableMetaData)); processor.SchemaExists(tableMetaData.SchemaName).ShouldBeTrue(); runner.Up(new VersionMigration(tableMetaData)); processor.TableExists(tableMetaData.SchemaName, tableMetaData.TableName).ShouldBeTrue(); runner.Down(new VersionMigration(tableMetaData)); processor.TableExists(tableMetaData.SchemaName, tableMetaData.TableName).ShouldBeFalse(); runner.Down(new VersionSchemaMigration(tableMetaData)); processor.SchemaExists(tableMetaData.SchemaName).ShouldBeFalse(); }, true, #if NET461 typeof(SqlAnywhereProcessor), #endif typeof(SQLiteProcessor), typeof(MySqlProcessor), typeof(FirebirdProcessor)); }
public void CanApplyForeignKeyConvention() { ExecuteWithSupportedProcessors( processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor); runner.Up(new TestForeignKeyNamingConvention()); processor.ConstraintExists(null, "Users", "FK_Users_GroupId_Groups_GroupId").ShouldBeTrue(); runner.Down(new TestForeignKeyNamingConvention()); }, false, typeof(SqliteProcessor)); }
public void CanCreateAndDropIndex() { ExecuteWithSupportedProcessors( processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor); runner.Up(new TestCreateAndDropTableMigration()); processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeFalse(); runner.Up(new TestCreateAndDropIndexMigration()); processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeTrue(); runner.Down(new TestCreateAndDropIndexMigration()); processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeFalse(); runner.Down(new TestCreateAndDropTableMigration()); processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeFalse(); //processor.CommitTransaction(); }); }
public void ObsoleteAlterTable_MigrationRequiresAutomaticDelete_AndProcessorHasUndoDisabled_ShouldNotThrow() { using (var tempDb = new TemporaryDatabase(IntegrationTestOptions.Firebird, _firebirdLibraryProber)) { var connectionString = tempDb.ConnectionString; var runnerContext = new RunnerContext(new TextWriterAnnouncer(TestContext.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations" }; using (var connection = new FbConnection(connectionString)) { var announcer = new TextWriterAnnouncer(TestContext.Out) { ShowSql = true }; var options = FirebirdOptions.AutoCommitBehaviour(); options.TruncateLongNames = false; var processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichCreatesTwoRelatedTables()); processor.CommitTransaction(); FbConnection.ClearPool(connection); } //---------------Assert Precondition---------------- Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after first migration"); using (var connection = new FbConnection(connectionString)) { var announcer = new TextWriterAnnouncer(TestContext.Out) { ShowSql = true }; var options = FirebirdOptions.AutoCommitBehaviour(); var processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichAltersTableWithFK()); processor.CommitTransaction(); } Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after second migration"); } }
public void CanApplyIndexConventionWithSchema() { ExecuteWithSupportedProcessors( processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor); runner.Up(new TestIndexNamingConventionWithSchema()); processor.IndexExists("TestSchema", "Users", "IX_Users_GroupId").ShouldBeTrue(); processor.TableExists("TestSchema", "Users").ShouldBeTrue(); runner.Down(new TestIndexNamingConventionWithSchema()); processor.IndexExists("TestSchema", "Users", "IX_Users_GroupId").ShouldBeFalse(); processor.TableExists("TestSchema", "Users").ShouldBeFalse(); }); }
public void CanRunMigration() { ExecuteWithSupportedProcessors(processor => { var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor); runner.Up(new TestCreateAndDropTableMigration()); processor.TableExists(null, "TestTable").ShouldBeTrue(); // This is a hack until MigrationVersionRunner and MigrationRunner are refactored and merged together //processor.CommitTransaction(); runner.Down(new TestCreateAndDropTableMigration()); processor.TableExists(null, "TestTable").ShouldBeFalse(); }); }
public int Run() { var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s)) { ShowSql = true }; IRunnerContext migrationContext = new RunnerContext(announcer) { Profile = Constants.MigratorProfileName }; var processor = new SqlServer2014ProcessorFactory().Create(DatabaseConnectionString, announcer, new ProcessorOptions { PreviewOnly = false, Timeout = _options.TimeoutSeconds }); var runner = new MigrationRunner(Assembly.GetEntryAssembly(), migrationContext, processor); try { if (_options.GenerateEnums) { runner.Up(new EnumTableGenerator()); } if (_options.MigrationRequest.HasValue) { runner.MigrateUp(_options.MigrationRequest.Value, true); } else { runner.MigrateUp(true); } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(-1); } return(0); }
public void AlterTable_MigrationRequiresAutomaticDelete_AndProcessorHasUndoDisabled_ShouldNotThrow() { // this test was originally created to investigate an issue with foreign key names but in the process, // I found that FirebirdProcessor.CreateSequenceForIdentity doesn't respect FBOptions.UndoEnabled. Since // Undo isn't implemented for Firebird, a migration runner always has to turn it off, but even with it off, // the migrations below will fail unless CreateSequenceForIdentity is appropriately altered var tempResources = WriteOutFirebirdEmbeddedLibrariesToCurrentWorkingDirectory(); var tempFile = Path.GetTempFileName(); using (var deleter = new AutoDeleter(tempFile)) { File.Delete(tempFile); deleter.Add(tempResources); var connectionString = GetConnectionStringToTempDatabaseAt(tempFile); var runnerContext = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations" }; try { using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); options.TruncateLongNames = false; processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichCreatesTwoRelatedTables()); processor.CommitTransaction(); FbConnection.ClearPool(connection); } //---------------Assert Precondition---------------- Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after first migration"); using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichAltersTableWithFK()); processor.CommitTransaction(); } Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after second migration"); } catch (Exception ex) { try { File.Copy(tempFile, "C:\\tmp\\fm_tests.fdb", true); } catch { } throw ex; } } }
public static void Main(string[] args) { var runner = new MigrationRunner("mongodb://localhost:27017/TestMigrations", Assembly.GetExecutingAssembly().CodeBase); runner.Up(); }
public void MigrateUp(IMigration migration) { Runner.Up(migration); }