public void CorrectlyMigratesDownwardUsingDeploymentHistoryCreatedByMigration()
{
const string serverName = @"(localdb)\mssqllocaldb";
var databaseName = $"GalenTest_{Guid.NewGuid():N}";
var assemblyLoader = new AssemblyLoader();
// start at v1
var initialDeploymentAssemblyPath = TestUtils.BuildTestAssemblyPath(1);
TestUtils.InitializeDatabase(assemblyLoader, initialDeploymentAssemblyPath, "Pinpoint.Test.Data.TestContext", serverName, databaseName);
// migrate upward from v1 to v3
var upwardMigrationConfig = new DbDeploymentManagerConfiguration
{
TargetAssemblyPath = TestUtils.BuildTestAssemblyPath(3),
Database = new DatabaseEndpoint { ServerName = serverName, DatabaseName = databaseName },
MigrationConfig = new MigrationConfigurationInfo
{
Type = "Pinpoint.Test.Data.Migrations.Configuration"
}
};
var expectedUpwardMigrationHistory = new[]
{
"201404181533201_InitialCreate",
"201404181719410_AddedAddresInfoToCustomer",
"201404181726158_MoveAddressInformationIntoContactInfo",
"201404181729406_AddedRowVersionToDomainObjectBase",
"201404181740359_AddedMultiplePropertiesToCustomer"
};
try
{
var sut = new DbDeploymentManager(
upwardMigrationConfig,
assemblyLoader,
new SqlClientDbConnectionInfoBuilder());
sut.Deploy();
var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
serverName,
databaseName,
upwardMigrationConfig.MigrationConfig.Type);
Assert.AreEqual(1, actualDeploymentHistoryRowCount);
var migrationHistory = TestUtils.GetMigrationHistory(
serverName,
databaseName,
upwardMigrationConfig.MigrationConfig.Type);
Assert.IsNotNull(migrationHistory);
Assert.AreEqual(expectedUpwardMigrationHistory.Length, migrationHistory.Count());
Assert.IsTrue(expectedUpwardMigrationHistory.SequenceEqual(migrationHistory));
}
catch
{
// be sure to clean up
TestUtils.DropDatabase(
upwardMigrationConfig.Database.ServerName,
upwardMigrationConfig.Database.DatabaseName);
throw;
}
// migrate downward from v3 to v1 using Deployment History
var downwardMigrationConfig = new DbDeploymentManagerConfiguration
{
TargetAssemblyPath = TestUtils.BuildTestAssemblyPath(1),
Database = new DatabaseEndpoint { ServerName = serverName, DatabaseName = databaseName },
MigrationConfig = new MigrationConfigurationInfo
{
Type = "Pinpoint.Test.Data.Migrations.Configuration"
}
};
var expectedDownwardMigrationHistory = new[]
{
"201404181533201_InitialCreate",
"201404181719410_AddedAddresInfoToCustomer",
};
try
{
var sut = new DbDeploymentManager(
downwardMigrationConfig,
assemblyLoader,
new SqlClientDbConnectionInfoBuilder());
sut.Deploy();
var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
serverName,
databaseName,
downwardMigrationConfig.MigrationConfig.Type);
Assert.AreEqual(2, actualDeploymentHistoryRowCount);
var migrationHistory = TestUtils.GetMigrationHistory(
serverName,
databaseName,
downwardMigrationConfig.MigrationConfig.Type);
Assert.IsNotNull(migrationHistory);
Assert.AreEqual(expectedDownwardMigrationHistory.Length, migrationHistory.Count());
Assert.IsTrue(expectedDownwardMigrationHistory.SequenceEqual(migrationHistory));
}
finally
{
//Be sure to clean up
TestUtils.DropDatabase(downwardMigrationConfig.Database.ServerName, downwardMigrationConfig.Database.DatabaseName);
}
}