public void CorrectlyMigratesDownwardUsingDeploymentHistoryCreatedByInitialization()
{
const string serverName = @"(localdb)\mssqllocaldb";
var databaseName = $"GalenTest_{Guid.NewGuid():N}";
var assemblyLoader = new AssemblyLoader();
// start by initialization to v3
var initialDeploymentConfig = new DbDeploymentManagerConfiguration
{
Mode = DeploymentMode.InitializeOnly,
TargetAssemblyPath = TestUtils.BuildTestAssemblyPath(3),
Database = new DatabaseEndpoint { ServerName = serverName, DatabaseName = databaseName },
InitializationConfig = new InitializerConfigurationInfo
{
Type = "Pinpoint.Test.Data.Initializers.TestContextCreateDatabaseIfNotExists"
}
};
var expectedInitialMigrationHistory = new[]
{
"201404181533201_InitialCreate",
"201404181719410_AddedAddresInfoToCustomer",
"201404181726158_MoveAddressInformationIntoContactInfo",
"201404181729406_AddedRowVersionToDomainObjectBase",
"201404181740359_AddedMultiplePropertiesToCustomer"
};
try
{
var sut = new DbDeploymentManager(
initialDeploymentConfig,
assemblyLoader,
new SqlClientDbConnectionInfoBuilder());
sut.Deploy();
var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
serverName,
databaseName,
"Pinpoint.Test.Data.Migrations.Configuration");
Assert.AreEqual(1, actualDeploymentHistoryRowCount);
var migrationHistory = TestUtils.GetMigrationHistory(
serverName,
databaseName,
"Pinpoint.Test.Data.Migrations.Configuration");
Assert.IsNotNull(migrationHistory);
Assert.AreEqual(expectedInitialMigrationHistory.Length, migrationHistory.Count());
Assert.IsTrue(expectedInitialMigrationHistory.SequenceEqual(migrationHistory));
}
catch
{
// clean up if there was a problem
TestUtils.DropDatabase(
initialDeploymentConfig.Database.ServerName,
initialDeploymentConfig.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);
}
}