public void CorrectlyMigratesUpward()
		{
			string serverName = @"(localdb)\mssqllocaldb";
			string databaseName = $"GalenTest_{Guid.NewGuid():N}";
			string deployedAssemblyPath = TestUtils.BuildTestAssemblyPath(2);
			var expectedMigrationHistory = new[]
			{
					"201404181533201_InitialCreate",
					"201404181719410_AddedAddresInfoToCustomer",
					"201404181726158_MoveAddressInformationIntoContactInfo",
					"201404181729406_AddedRowVersionToDomainObjectBase",
					"201404181740359_AddedMultiplePropertiesToCustomer",
			};

			var assemblyLoader = new AssemblyLoader();
			TestUtils.InitializeDatabase(assemblyLoader, deployedAssemblyPath, "Pinpoint.Test.Data.TestContext", serverName, databaseName);

			//Migration v2 to v3
			var config = new DbDeploymentManagerConfiguration()
			{
				TargetAssemblyPath=TestUtils.BuildTestAssemblyPath(3),
				Database=new DatabaseEndpoint { ServerName=serverName, DatabaseName=databaseName },
				MigrationConfig=new MigrationConfigurationInfo
				{
				    Type = "Pinpoint.Test.Data.Migrations.Configuration"
				}
			};

			try
			{
				var sut = new DbDeploymentManager(config, assemblyLoader, new SqlClientDbConnectionInfoBuilder());
				sut.Deploy();

			    Assert.AreEqual(1,
			        TestUtils.GetDeploymentHistoryRowCount(serverName, databaseName, config.MigrationConfig.Type));

                var migrationHistory = TestUtils.GetMigrationHistory(serverName, databaseName, config.MigrationConfig.Type);

				Assert.IsNotNull(migrationHistory);
				Assert.AreEqual(expectedMigrationHistory.Length, migrationHistory.Count());
				Assert.IsTrue(expectedMigrationHistory.SequenceEqual(migrationHistory));
			}
			finally
			{
				//Be sure to clean up
				TestUtils.DropDatabase(config.Database.ServerName, config.Database.DatabaseName);
			}
		}
        public DbDeploymentManager(DbDeploymentManagerConfiguration config, IAssemblyLoader assemblyLoader, IDbConnectionInfoBuilder dbConnectionInfoBuilder)
        {
            if (config.Database == null)
            {
                throw new InvalidOperationException("No database endpoint provided");
            }

            m_Config = config;
            m_AssemblyLoader = assemblyLoader;
            m_ConnectionInfoBuilder = dbConnectionInfoBuilder;
            m_TargetAssembly = new Lazy<Assembly>(LazyLoadTargetAssembly);
            m_DeployedAssembly = new Lazy<Assembly>(LazyLoadDeployedAssembly);
            m_InitializerInfo = new Lazy<ContextTypeInitializerInfo>(LazyLoadInitializerInfo);

            if (string.IsNullOrWhiteSpace(m_Config.DeploymentHistoryExtractPath))
            {
                m_Config.DeploymentHistoryExtractPath = Path.GetTempPath();
                Log.Debug(
                    "Deployment History extract path not specified.  Defaulting to temp directory {tempDirectory}.",
                    m_Config.DeploymentHistoryExtractPath);
            }
        }
		public void ArgumentsShouldBeCorrectlyMappedToConfiguration()
		{
			var connections = new[]
			{
				new DatabaseEndpoint() {DatabaseName = "Db1", ServerName = "Server1"}
			};

			var arguments = new Arguments()
			{
				Database=new DatabaseEndpoints()
				{
					Endpoints=connections
				},
				DeployedAssemblyOverridePath=@"c:\some dir\some other dir\MyAssembly1.dll",
				TargetAssemblyPath=@".\MyAssembly1.dll",
				RunServerMigrationsInTransaction=false,
				Mode=DeploymentMode.MigrationOnly,
                DeploymentHistoryExtractPath=@"c:\some dir\deployment history"
			};

			var migration = new MigrationConfigurationInfo
			{
				Type = "Galen.Enterprise.Data.MigrationConfigs.SomeContext.Configuration"
			};

			var initializer = new InitializerConfigurationInfo
			{
				Type = "Galen.Enterprise.Data.Initializers.SomeContextCreateDatabaseIfNotExists",
				ServiceAccount = new ServiceAccountInfo()
				{
					Name = "SomeWindowsAccountName",
					Domain = "SomeDomainName",
					DatabaseUser = "******",
					AccountType = "Windows"

				}
			};

			var mockConfigStore = new Moq.Mock<IDeploymentConfigurationStore>();
			mockConfigStore.Setup(m => m.Load())
				.Returns(new DeploymentConfiguration()
				{
					MigrationConfigurationInfo=migration,
					InitializerConfigurationInfo=initializer
				});

			var expected = new DbDeploymentManagerConfiguration
			{
				Database=connections.First(),
				MigrationConfig=migration,
				InitializationConfig=initializer,
				DeployedAssemblyOverridePath=@"c:\some dir\some other dir\MyAssembly1.dll",
				TargetAssemblyPath=@".\MyAssembly1.dll",
				RunServerMigrationsInTransaction=false,
				Mode=DeploymentMode.MigrationOnly,
				AuthMode=AuthenticationMode.Integrated,
                DeploymentHistoryExtractPath = @"c:\some dir\deployment history"
            };

			var sut = new ConfigurationArgumentMapper(mockConfigStore.Object);
			var actual = sut.FromArguments(arguments);

			Assert.IsNotNull(actual);
			Assert.AreEqual(expected.DeployedAssemblyOverridePath, actual.DeployedAssemblyOverridePath);
			Assert.AreEqual(expected.TargetAssemblyPath, actual.TargetAssemblyPath);
			Assert.AreEqual(expected.RunServerMigrationsInTransaction, actual.RunServerMigrationsInTransaction);
			Assert.AreEqual(expected.Mode, actual.Mode);
		    Assert.AreEqual(expected.DeploymentHistoryExtractPath, actual.DeploymentHistoryExtractPath);
			Assert.IsNull(actual.SqlLogin);
			Assert.IsNull(actual.SqlPassword);

			Assert.IsNotNull(actual.Database);
            Assert.AreEqual(expected.Database.ServerName, actual.Database.ServerName);
            Assert.AreEqual(expected.Database.DatabaseName, actual.Database.DatabaseName);

			Assert.IsNotNull(actual.MigrationConfig);
            Assert.AreEqual(migration.Type, actual.MigrationConfig.Type);

			Assert.IsNotNull(actual.InitializationConfig);
			Assert.IsNotNull(actual.InitializationConfig.ServiceAccount);
			Assert.AreEqual(initializer.Type, actual.InitializationConfig.Type);
			Assert.AreEqual(initializer.ServiceAccount.AccountType, actual.InitializationConfig.ServiceAccount.AccountType);
			Assert.AreEqual(initializer.ServiceAccount.Name, actual.InitializationConfig.ServiceAccount.Name);
			Assert.AreEqual(initializer.ServiceAccount.Domain, actual.InitializationConfig.ServiceAccount.Domain);
			Assert.AreEqual(initializer.ServiceAccount.DatabaseUser, actual.InitializationConfig.ServiceAccount.DatabaseUser);
			Assert.AreEqual(initializer.ServiceAccount.DatabaseUserPassword, actual.InitializationConfig.ServiceAccount.DatabaseUserPassword);
		}
		public void CorrectlyInitializesADatabaseAndDoesNotSeed()
		{
			string serverName = @"(localdb)\mssqllocaldb";
			string databaseName = string.Format("TestContext_{0}", Guid.NewGuid().ToString().Replace("-", string.Empty));
			var expectedMigrationHistory = new[]
			{
					"201506161504528_InitialCreate"
			};

			var config = new DbDeploymentManagerConfiguration()
			{
				Mode=DeploymentMode.InitializeOnly,
				TargetAssemblyPath=TestUtils.BuildTestContextTestAssemblyPath(1),
				Database=new DatabaseEndpoint { ServerName=serverName, DatabaseName=databaseName },
				InitializationConfig=new InitializerConfigurationInfo
				{
					Type = "Galen.Ci.EntityFramework.Initialization.CreateSecureSeededDatabaseIfNotExists`2[[Galen.Ci.EntityFramework.Tests.TestContext.Data.TestDbContext, Galen.Ci.EntityFramework.Tests.TestContext], [Galen.Ci.EntityFramework.Tests.TestContext.Data.TestDataSeeder, Galen.Ci.EntityFramework.Tests.TestContext]], Galen.Ci.EntityFramework.Initialization",
					DisableForcedSeeding = true //Disable seeding
				}
			};

			try
			{
				var sut = new DbDeploymentManager(config, new AssemblyLoader(), new SqlClientDbConnectionInfoBuilder());
				sut.Deploy();

                var migrationHistory = TestUtils.GetMigrationHistory(serverName, databaseName, "Galen.Ci.EntityFramework.Tests.TestContext.Data.Migrations.Configuration");

				Assert.IsNotNull(migrationHistory);
				Assert.AreEqual(expectedMigrationHistory.Length, migrationHistory.Count());
				Assert.IsTrue(expectedMigrationHistory.SequenceEqual(migrationHistory));

				var startingRows = TestUtils.GetRows(serverName, databaseName, "dbo.BasicEntities");

				//Now that we are deployed, let's delete some data and see if the auto-seeding re-adds it
				TestUtils.ExecuteSqlCommand(serverName, databaseName, "DELETE FROM dbo.BasicEntities WHERE ID = 2");
				var postDeleteRows = TestUtils.GetRows(serverName, databaseName, "dbo.BasicEntities");

				Assert.IsTrue(postDeleteRows.Count()==(startingRows.Count()-1));

				sut=new DbDeploymentManager(config, new AssemblyLoader(), new SqlClientDbConnectionInfoBuilder());
				sut.Deploy();

				var postSeedingRows = TestUtils.GetRows(serverName, databaseName, "dbo.BasicEntities");

				Assert.IsTrue(postSeedingRows.Count()==(startingRows.Count()-1));

                Assert.AreEqual(1, TestUtils.GetDeploymentHistoryRowCount(serverName, databaseName, "Galen.Ci.EntityFramework.Tests.TestContext.Data.Migrations.Configuration"));
			}
			finally
			{
				//Be sure to clean up
				TestUtils.DropDatabase(config.Database.ServerName, config.Database.DatabaseName);
			}
		}
		public void CorrectlyMigratesDownward()
		{
			string serverName = @"(localdb)\mssqllocaldb";
			string databaseName = $"GalenTest_{Guid.NewGuid():N}";
			string deployedAssemblyPath = TestUtils.BuildTestAssemblyPath(3);
			var expectedMigrationHistory = new[]
			{
				"201404181533201_InitialCreate",
				"201404181719410_AddedAddresInfoToCustomer",
			};

			var assemblyLoader = new AssemblyLoader();
			TestUtils.InitializeDatabase(assemblyLoader, deployedAssemblyPath, "Pinpoint.Test.Data.TestContext", serverName, databaseName);

			//Migration v3 to v1
			var config = new DbDeploymentManagerConfiguration()
			{
				TargetAssemblyPath=TestUtils.BuildTestAssemblyPath(1),
				DeployedAssemblyOverridePath=deployedAssemblyPath,      // we didn't initialize using the deployer, so this has to be specified
				Database=new DatabaseEndpoint { ServerName=serverName, DatabaseName=databaseName },
				MigrationConfig=new MigrationConfigurationInfo
					{
				    Type = "Pinpoint.Test.Data.Migrations.Configuration"
					}
			};

			try
			{
				var sut = new DbDeploymentManager(config, assemblyLoader, new SqlClientDbConnectionInfoBuilder());
				sut.Deploy();

			    Assert.AreEqual(1,
			        TestUtils.GetDeploymentHistoryRowCount(serverName, databaseName, config.MigrationConfig.Type));

                var migrationHistory = TestUtils.GetMigrationHistory(serverName, databaseName, config.MigrationConfig.Type);

				Assert.IsNotNull(migrationHistory);
				Assert.AreEqual(expectedMigrationHistory.Length, migrationHistory.Count());
				Assert.IsTrue(expectedMigrationHistory.SequenceEqual(migrationHistory));
			}
			finally
			{
				//Be sure to clean up
				TestUtils.DropDatabase(config.Database.ServerName, config.Database.DatabaseName);
			}
		}
        public void NoOpWhenInitializingOrMigratingToCurrentDeployedVersion()
        {
            const string serverName = @"(localdb)\mssqllocaldb";
            var databaseName = $"GalenTest_{Guid.NewGuid():N}";

            var assemblyLoader = new AssemblyLoader();

            var currentAssemblyPath = TestUtils.BuildTestAssemblyPath(3);

            // start at v3 - (this creates deployment history)
            var initializeOrMigrateConfig = new DbDeploymentManagerConfiguration
            {
                Mode = DeploymentMode.InitializeOrMigrate,
                TargetAssemblyPath = currentAssemblyPath,
                Database = new DatabaseEndpoint { ServerName = serverName, DatabaseName = databaseName },
                InitializationConfig = new InitializerConfigurationInfo
                {
                    Type = "Pinpoint.Test.Data.Initializers.TestContextCreateDatabaseIfNotExists"
                },
                MigrationConfig = new MigrationConfigurationInfo
                {
                    Type = "Pinpoint.Test.Data.Migrations.Configuration"
                }
            };

            var expectedMigrationHistory = new[]
            {
                "201404181533201_InitialCreate",
                "201404181719410_AddedAddresInfoToCustomer",
                "201404181726158_MoveAddressInformationIntoContactInfo",
                "201404181729406_AddedRowVersionToDomainObjectBase",
                "201404181740359_AddedMultiplePropertiesToCustomer"
            };

            try
            {
                var sut = new DbDeploymentManager(
                    initializeOrMigrateConfig,
                    assemblyLoader,
                    new SqlClientDbConnectionInfoBuilder());
                sut.Deploy();

                var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
                    serverName,
                    databaseName,
                    initializeOrMigrateConfig.MigrationConfig.Type);
                Assert.AreEqual(1, actualDeploymentHistoryRowCount);

                var migrationHistory = TestUtils.GetMigrationHistory(
                    serverName,
                    databaseName,
                    initializeOrMigrateConfig.MigrationConfig.Type);

                Assert.IsNotNull(migrationHistory);
                Assert.AreEqual(expectedMigrationHistory.Length, migrationHistory.Count());
                Assert.IsTrue(expectedMigrationHistory.SequenceEqual(migrationHistory));
            }
            catch
            {
                // clean up if there was a problem
                TestUtils.DropDatabase(
                    initializeOrMigrateConfig.Database.ServerName,
                    initializeOrMigrateConfig.Database.DatabaseName);

                throw;
            }

            try
            {
                // run another InitializeOrMigrate to the same version as what is deployed
                // (this is a close representation of how our release management works)
                var sut = new DbDeploymentManager(
                    initializeOrMigrateConfig,      // <-- reuse; same as before, nothing has changed
                    assemblyLoader,
                    new SqlClientDbConnectionInfoBuilder());
                sut.Deploy();

                // nothing should be different in deployment history
                var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
                    serverName,
                    databaseName,
                    initializeOrMigrateConfig.MigrationConfig.Type);
                Assert.AreEqual(1, actualDeploymentHistoryRowCount);

                // nothing should be different in migration history
                var migrationHistory = TestUtils.GetMigrationHistory(
                    serverName,
                    databaseName,
                    initializeOrMigrateConfig.MigrationConfig.Type);

                Assert.IsNotNull(migrationHistory);
                Assert.AreEqual(expectedMigrationHistory.Length, migrationHistory.Count());
                Assert.IsTrue(expectedMigrationHistory.SequenceEqual(migrationHistory));
            }
            finally
            {
                // be sure to clean up
                TestUtils.DropDatabase(initializeOrMigrateConfig.Database.ServerName, initializeOrMigrateConfig.Database.DatabaseName);
            }
        }
	    public void NoOpInitializationWhenInitializingToCurrentDeployedVersion()
	    {
            const string serverName = @"(localdb)\mssqllocaldb";
            var databaseName = $"GalenTest_{Guid.NewGuid():N}";

            string currentAssemblyPath = TestUtils.BuildTestAssemblyPath(3);

            var assemblyLoader = new AssemblyLoader();
            TestUtils.InitializeDatabase(assemblyLoader, currentAssemblyPath, "Pinpoint.Test.Data.TestContext", serverName, databaseName);

            var initializeConfig = new DbDeploymentManagerConfiguration
            {
                Mode = DeploymentMode.InitializeOnly,
                TargetAssemblyPath = currentAssemblyPath,      // use the current version as the target
                Database = new DatabaseEndpoint { ServerName = serverName, DatabaseName = databaseName },
                InitializationConfig = new InitializerConfigurationInfo
                {
                    Type = "Pinpoint.Test.Data.Initializers.TestContextCreateDatabaseIfNotExists"
                }
            };

            var expectedMigrationHistory = new[]
            {
                "201404181533201_InitialCreate",
                "201404181719410_AddedAddresInfoToCustomer",
                "201404181726158_MoveAddressInformationIntoContactInfo",
                "201404181729406_AddedRowVersionToDomainObjectBase",
                "201404181740359_AddedMultiplePropertiesToCustomer"
            };

            try
            {
                var sut = new DbDeploymentManager(
                    initializeConfig,
                    assemblyLoader,
                    new SqlClientDbConnectionInfoBuilder());
                sut.Deploy();

                // we expect no changes to migration history
                var migrationHistory = TestUtils.GetMigrationHistory(
                    serverName,
                    databaseName,
                    "Pinpoint.Test.Data.Migrations.Configuration");

                Assert.IsNotNull(migrationHistory);
                Assert.AreEqual(expectedMigrationHistory.Length, migrationHistory.Count());
                Assert.IsTrue(expectedMigrationHistory.SequenceEqual(migrationHistory));

                // no new deployment history should be created because no initialization nor migration should have occurred
                var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
                    serverName,
                    databaseName,
                    "Pinpoint.Test.Data.Migrations.Configuration");
                Assert.AreEqual(0, actualDeploymentHistoryRowCount);
            }
            finally
            {
                // be sure to clean up
                TestUtils.DropDatabase(initializeConfig.Database.ServerName, initializeConfig.Database.DatabaseName);
            }
        }
	    public void NoOpMigrationWhenMigratingDownwardPastInitialMigrationUsingDeploymentHistoryWithoutAnOverride()
	    {
            const string serverName = @"(localdb)\mssqllocaldb";
            var databaseName = $"GalenTest_{Guid.NewGuid():N}";

            var assemblyLoader = new AssemblyLoader();

            // start by initializing 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.AnotherTestContextCreateDatabaseIfNotExists"
                }
            };

            var expectedInitialMigrationHistory = new[]
            {
                "201404181743108_InitialCreate"
            };

            try
            {
                var sut = new DbDeploymentManager(
                    initialDeploymentConfig,
                    assemblyLoader,
                    new SqlClientDbConnectionInfoBuilder());
                sut.Deploy();

                var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
                    serverName,
                    databaseName,
                    "Pinpoint.Test.Data.AnotherTestContextMigrations.Configuration");
                Assert.AreEqual(1, actualDeploymentHistoryRowCount);

                var migrationHistory = TestUtils.GetMigrationHistory(
                    serverName,
                    databaseName,
                    "Pinpoint.Test.Data.AnotherTestContextMigrations.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;
            }

            // attempt to migrate downward from v3 past initial create using Deployment History
            // without specifying a deployed assembly override
            var downwardMigrationConfig = new DbDeploymentManagerConfiguration
            {
                TargetAssemblyPath = TestUtils.BuildTestAssemblyPath(1),
                Database = new DatabaseEndpoint { ServerName = serverName, DatabaseName = databaseName },
                MigrationConfig = new MigrationConfigurationInfo
                {
                    Type = "Pinpoint.Test.Data.AnotherTestContextMigrations.Configuration"
                }
            };

            try
            {
                var sut = new DbDeploymentManager(
                    downwardMigrationConfig,
                    assemblyLoader,
                    new SqlClientDbConnectionInfoBuilder());
                sut.Deploy();

                // we expect no changes to migration history
                var migrationHistory = TestUtils.GetMigrationHistory(
                    serverName, 
                    databaseName, 
                    downwardMigrationConfig.MigrationConfig.Type);

                Assert.IsNotNull(migrationHistory);
                Assert.AreEqual(expectedInitialMigrationHistory.Length, migrationHistory.Count());
                Assert.IsTrue(expectedInitialMigrationHistory.SequenceEqual(migrationHistory));

                // no new deployment history should be created because no migration should have occurred
                var actualDeploymentHistoryRowCount = TestUtils.GetDeploymentHistoryRowCount(
                    serverName,
                    databaseName,
                    "Pinpoint.Test.Data.AnotherTestContextMigrations.Configuration");
                Assert.AreEqual(1, actualDeploymentHistoryRowCount);
            }
            finally
            {
                // be sure to clean up
                TestUtils.DropDatabase(downwardMigrationConfig.Database.ServerName, downwardMigrationConfig.Database.DatabaseName);
            }
        }
		public void CorrectlyMigratesDownwardPastInitialMigration()
		{
			string serverName = @"(localdb)\mssqllocaldb";
			string databaseName = $"GalenTest_{Guid.NewGuid():N}";
			string deployedAssemblyPath = TestUtils.BuildTestAssemblyPath(3);

			var assemblyLoader = new AssemblyLoader();
			TestUtils.InitializeDatabase(assemblyLoader, deployedAssemblyPath, "Pinpoint.Test.Data.TestContext", serverName, databaseName);
			TestUtils.InitializeDatabase(assemblyLoader, deployedAssemblyPath, "Pinpoint.Test.Data.AnotherTestContext", serverName, databaseName);

			//Migration v3 to v1
			var config = new DbDeploymentManagerConfiguration()
			{
				TargetAssemblyPath=TestUtils.BuildTestAssemblyPath(1),
				DeployedAssemblyOverridePath=deployedAssemblyPath,      // we didn't initialize using the deployer so this is required
				Database=new DatabaseEndpoint { ServerName=serverName, DatabaseName=databaseName },
				MigrationConfig=new MigrationConfigurationInfo
				{
				    Type = "Pinpoint.Test.Data.AnotherTestContextMigrations.Configuration"
				}
			};

			try
			{
				var sut = new DbDeploymentManager(config, assemblyLoader, new SqlClientDbConnectionInfoBuilder());
				sut.Deploy();

			    Assert.AreEqual(1,
			        TestUtils.GetDeploymentHistoryRowCount(serverName, databaseName, config.MigrationConfig.Type));

                //We expect all schema related to AnotherTestContext would be deleted
                var migrationHistory = TestUtils.GetMigrationHistory(serverName, databaseName, config.MigrationConfig.Type);

				Assert.IsNotNull(migrationHistory);
				Assert.AreEqual(0, migrationHistory.Count());
			}
			finally
			{
				//Be sure to clean up
				TestUtils.DropDatabase(config.Database.ServerName, config.Database.DatabaseName);
			}
		}
        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);
            }
        }