public void InvokeMgRollback_Returns_If_Latest_Iteration_Is_Zero() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); DbMock.GetLatestIteration(0); DbMock.GetAppliedScriptsForLatestIteration(new[] { new Migration { Iteration = 1, MigrationId = "one.sql" } }); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/rollback/one.sql" }); ConfigManagerMock.RollbackDirectory("migrations/rollback"); var command = new InvokeMgRollback(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <MgResult>()?.First(); Assert.False(result.Successful); }
public void InvokeMgRollout_Returns_If_All_Scripts_Are_Applied() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); ConfigManagerMock.RolloutDirectory("migrations/rollout"); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/one.sql", "migrations/two.sql" }); DbMock.GetAppliedMigrations(new Migration[] { new() { Iteration = 1, MigrationId = "one.sql" }, new() { Iteration = 1, MigrationId = "two.sql" } });
public void InvokeMgSeeding_Returns_If_All_Scripts_Are_Applied() { ConfigManagerMock.ConfigReturns(null); DbMock.SeedingTableExists(true); ConfigManagerMock.SeedersDirectory("migrations/seeders"); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/one.sql", "migrations/two.sql" }); DbMock.GetAppliedSeeders(new Seed[] { new() { SeedId = "one.sql" }, new() { SeedId = "two.sql" } });
public void InvokeMgRollback_Rollbacks_Correct_Migrations() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); DbMock.GetLatestIteration(1); DbMock.GetAppliedScriptsForLatestIteration(new[] { new Migration { Iteration = 1, MigrationId = "two" }, new Migration { Iteration = 1, MigrationId = "three" }, }); FileManagerMock.GetAllFilesInFolder(new[] { "migrations/rollback/one.sql", "migrations/rollback/two.sql", "migrations/rollback/three.sql", }); ConfigManagerMock.RollbackDirectory("migrations/rollback"); FileManagerMock.ReadAllText("migrations/rollback/two.sql", "rollback 2"); FileManagerMock.ReadAllText("migrations/rollback/three.sql", "rollback 3"); DbMock.RunTransactionAny(1); var command = new InvokeMgRollback(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <string>().ToArray(); Assert.Contains("Found 2 migrations applied in iteration 1", result); Assert.Contains("Migration one was not applied in latest iteration, skipping", result); Assert.Contains("Adding rollback of migration: two to transaction", result); FileManagerMock.VerifyReadAllText("migrations/rollback/one.sql", Times.Never()); var transactions = "rollback 2;" + Environment.NewLine + "DELETE FROM \"public\".\"MIGRATIONS\" WHERE \"MIGRATION_ID\" = 'two' AND \"ITERATION\" = '1';" + Environment.NewLine + "rollback 3;" + Environment.NewLine + "DELETE FROM \"public\".\"MIGRATIONS\" WHERE \"MIGRATION_ID\" = 'three' AND \"ITERATION\" = '1';" + Environment.NewLine; DbMock.VerifyRunTransaction(transactions); }
public void InvokeMgRollout_Returns_If_No_Scripts_Found() { ConfigManagerMock.ConfigReturns(null); DbMock.MigrationTableExists(true); FileManagerMock.GetAllFilesInFolder(Array.Empty <string>()); ConfigManagerMock.RolloutDirectory("migrations/rollout"); var command = new InvokeMgRollout(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******" }; var result = command.Invoke()?.OfType <MgResult>()?.First(); Assert.False(result.Successful); }
public void InvokeMgRollout_Creates_Migration_Table_If_CreateTableIfNotExist_Is_True() { ConfigManagerMock.ConfigReturns(null); ConfigManagerMock.RollbackDirectory("migrations/rollback"); DbMock.MigrationTableExists(false); DbMock.CreateMigrationTable(1); FileManagerMock.GetAllFilesInFolder(Array.Empty <string>()); ConfigManagerMock.RolloutDirectory("migrations/rollout"); var command = new InvokeMgRollout(GetMockedDependencies()) { Database = "database", Host = "host", Port = 1111, Schema = "public", Username = "******", CreateTableIfNotExist = true }; command.Invoke()?.OfType <string>()?.ToArray(); DbMock.VerifyCreateMigrationTable(Times.Once()); }