public void Can_add_and_retrieve_migrations() { var updateDatabaseOperation = new UpdateDatabaseOperation(new List <DbQueryCommandTree>()); updateDatabaseOperation.AddMigration("M1", new List <MigrationOperation>()); updateDatabaseOperation.AddMigration("M2", new List <MigrationOperation>()); Assert.Equal(2, updateDatabaseOperation.Migrations.Count); }
public void Can_add_and_retrieve_migrations() { var updateDatabaseOperation = new UpdateDatabaseOperation(new List<DbQueryCommandTree>()); updateDatabaseOperation.AddMigration("M1", new List<MigrationOperation>()); updateDatabaseOperation.AddMigration("M2", new List<MigrationOperation>()); Assert.Equal(2, updateDatabaseOperation.Migrations.Count); }
public void AddMigration_should_validate_preconditions() { var updateDatabaseOperation = new UpdateDatabaseOperation(new List <DbQueryCommandTree>()); Assert.Equal( new ArgumentException(Strings.ArgumentIsNullOrWhitespace("migrationId")).Message, Assert.Throws <ArgumentException>(() => updateDatabaseOperation.AddMigration(null, null)).Message); Assert.Equal( "operations", Assert.Throws <ArgumentNullException>(() => updateDatabaseOperation.AddMigration("M", null)).ParamName); }
public void AddMigration_should_validate_preconditions() { var updateDatabaseOperation = new UpdateDatabaseOperation(new List<DbQueryCommandTree>()); Assert.Equal( new ArgumentException(Strings.ArgumentIsNullOrWhitespace("migrationId")).Message, Assert.Throws<ArgumentException>(() => updateDatabaseOperation.AddMigration(null, null)).Message); Assert.Equal( "operations", Assert.Throws<ArgumentNullException>(() => updateDatabaseOperation.AddMigration("M", null)).ParamName); }
public void Generate_can_handle_update_database_operations() { var migrationSqlGenerator = new SqlCeMigrationSqlGenerator(); var providerInvariantName = ProviderRegistry.SqlCe4_ProviderInfo.ProviderInvariantName; var historyRepository = new HistoryRepository( new SqlCeConnectionFactory(providerInvariantName) .CreateConnection("Foo").ConnectionString, DbProviderFactories.GetFactory(providerInvariantName), "MyKey", null); var updateDatabaseOperation = new UpdateDatabaseOperation(historyRepository.CreateDiscoveryQueryTrees().ToList()); updateDatabaseOperation.AddMigration("M1", new []{ new DropColumnOperation("Customers", "Foo") }); var sql = migrationSqlGenerator.Generate(new[] { updateDatabaseOperation }, "2008").Join(s => s.Sql, Environment.NewLine); Assert.Equal(@"ALTER TABLE [Customers] DROP COLUMN [Foo]", sql); }
public void Generate_can_handle_update_database_operations() { var migrationSqlGenerator = new SqlServerMigrationSqlGenerator(); var historyRepository = new HistoryRepository( Mock.Of<InternalContextForMock>(), new SqlConnectionFactory().CreateConnection("Foo").ConnectionString, DbProviderFactories.GetFactory(ProviderRegistry.Sql2008_ProviderInfo.ProviderInvariantName), "MyKey", null, HistoryContext.DefaultFactory, schemas: new[] { "dbo", "foo", "bar" }); var updateDatabaseOperation = new UpdateDatabaseOperation(historyRepository.CreateDiscoveryQueryTrees().ToList()); updateDatabaseOperation.AddMigration( "V1", new MigrationOperation[] { new DropColumnOperation("Customers", "Foo"), new CreateProcedureOperation("Foo", "Bar") }); updateDatabaseOperation.AddMigration( "V2", new MigrationOperation[] { new AddColumnOperation("Customers", new ColumnModel(PrimitiveTypeKind.String) { Name = "C" }), new CreateProcedureOperation("bar", "baz") }); var sql = migrationSqlGenerator.Generate(new[] { updateDatabaseOperation }, "2008").Join(s => s.Sql, Environment.NewLine); Assert.Equal(@"DECLARE @CurrentMigration [nvarchar](max) IF object_id('[dbo].[__MigrationHistory]') IS NOT NULL SELECT @CurrentMigration = (SELECT TOP (1) [Project1].[MigrationId] AS [MigrationId] FROM ( SELECT [Extent1].[MigrationId] AS [MigrationId] FROM [dbo].[__MigrationHistory] AS [Extent1] WHERE [Extent1].[ContextKey] = N'MyKey' ) AS [Project1] ORDER BY [Project1].[MigrationId] DESC) IF object_id('[foo].[__MigrationHistory]') IS NOT NULL SELECT @CurrentMigration = (SELECT TOP (1) [Project1].[MigrationId] AS [MigrationId] FROM ( SELECT [Extent1].[MigrationId] AS [MigrationId] FROM [foo].[__MigrationHistory] AS [Extent1] WHERE [Extent1].[ContextKey] = N'MyKey' ) AS [Project1] ORDER BY [Project1].[MigrationId] DESC) IF object_id('[bar].[__MigrationHistory]') IS NOT NULL SELECT @CurrentMigration = (SELECT TOP (1) [Project1].[MigrationId] AS [MigrationId] FROM ( SELECT [Extent1].[MigrationId] AS [MigrationId] FROM [bar].[__MigrationHistory] AS [Extent1] WHERE [Extent1].[ContextKey] = N'MyKey' ) AS [Project1] ORDER BY [Project1].[MigrationId] DESC) IF @CurrentMigration IS NULL SET @CurrentMigration = '0' IF @CurrentMigration < 'V1' BEGIN DECLARE @var0 nvarchar(128) SELECT @var0 = name FROM sys.default_constraints WHERE parent_object_id = object_id(N'Customers') AND col_name(parent_object_id, parent_column_id) = 'Foo'; IF @var0 IS NOT NULL EXECUTE('ALTER TABLE [Customers] DROP CONSTRAINT [' + @var0 + ']') ALTER TABLE [Customers] DROP COLUMN [Foo] EXECUTE(' CREATE PROCEDURE [Foo] AS BEGIN Bar END ') END IF @CurrentMigration < 'V2' BEGIN ALTER TABLE [Customers] ADD [C] [nvarchar](max) EXECUTE(' CREATE PROCEDURE [bar] AS BEGIN baz END ') END ", sql); }