public void Diff_finds_index_with_unique_updated() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); var index = entityType.Indexes[0]; index.IsUnique = false; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(2, operations.Count); Assert.IsType <DropIndexOperation>(operations[0]); Assert.IsType <CreateIndexOperation>(operations[1]); var dropIndexOperation = (DropIndexOperation)operations[0]; var createIndexOperation = (CreateIndexOperation)operations[1]; Assert.Equal("dbo.MyTable0", dropIndexOperation.TableName); Assert.Equal("MyIndex", dropIndexOperation.IndexName); Assert.Equal("dbo.MyTable0", createIndexOperation.TableName); Assert.Equal("MyIndex", createIndexOperation.IndexName); Assert.False(createIndexOperation.IsUnique); }
public void Diff_finds_updated_primary_key() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); entityType.SetKey(entityType.GetProperty("MyProperty")); entityType.GetKey().StorageName = "MyNewPK"; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(2, operations.Count); Assert.IsType <DropPrimaryKeyOperation>(operations[0]); Assert.IsType <AddPrimaryKeyOperation>(operations[1]); var dropPrimaryKeyOperation = (DropPrimaryKeyOperation)operations[0]; var addPrimaryKeyOperation = (AddPrimaryKeyOperation)operations[1]; Assert.Equal("dbo.MyTable0", dropPrimaryKeyOperation.TableName); Assert.Equal("MyPK0", dropPrimaryKeyOperation.PrimaryKeyName); Assert.Equal("dbo.MyTable0", addPrimaryKeyOperation.TableName); Assert.Equal("MyNewPK", addPrimaryKeyOperation.PrimaryKeyName); Assert.Equal(new[] { "MyColumn" }, addPrimaryKeyOperation.ColumnNames); }
public void Diff_handles_table_rename_conflicts() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var dependentEntityType = targetModel.GetEntityType("Dependent"); var principalEntityType = targetModel.GetEntityType("Principal"); targetModel.RemoveEntityType(dependentEntityType); principalEntityType.StorageName = dependentEntityType.StorageName; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(3, operations.Count); Assert.IsType <RenameTableOperation>(operations[0]); Assert.IsType <RenameTableOperation>(operations[1]); Assert.IsType <DropTableOperation>(operations[2]); var renameTableOperation0 = (RenameTableOperation)operations[0]; var renameTableOperation1 = (RenameTableOperation)operations[1]; var dropTableOperation = (DropTableOperation)operations[2]; Assert.Equal("dbo.MyTable0", renameTableOperation0.TableName); Assert.Equal("__mig_tmp__0", renameTableOperation0.NewTableName); Assert.Equal("dbo.MyTable1", renameTableOperation1.TableName); Assert.Equal("MyTable0", renameTableOperation1.NewTableName); Assert.Equal("dbo.__mig_tmp__0", dropTableOperation.TableName); }
protected virtual IMigrationMetadata CreateMigration([NotNull] string migrationName) { Check.NotEmpty(migrationName, "migrationName"); var sourceModel = MigrationAssembly.Model; var targetModel = ContextConfiguration.Model; IReadOnlyList <MigrationOperation> upgradeOperations, downgradeOperations; if (sourceModel != null) { upgradeOperations = ModelDiffer.Diff(sourceModel, targetModel); downgradeOperations = ModelDiffer.Diff(targetModel, sourceModel); } else { upgradeOperations = ModelDiffer.CreateSchema(targetModel); downgradeOperations = ModelDiffer.DropSchema(targetModel); } return (new MigrationMetadata(CreateMigrationId(migrationName), ContextConfiguration.Context.GetType()) { TargetModel = targetModel, UpgradeOperations = upgradeOperations, DowngradeOperations = downgradeOperations }); }
public void Diff_handles_transitive_table_renames() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var dependentEntityType = targetModel.GetEntityType("Dependent"); var principalEntityType = targetModel.GetEntityType("Principal"); var principalTableName = principalEntityType.TableName(); principalEntityType.SetTableName(dependentEntityType.TableName()); dependentEntityType.SetTableName(principalTableName); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(3, operations.Count); Assert.IsType <RenameTableOperation>(operations[0]); Assert.IsType <RenameTableOperation>(operations[1]); Assert.IsType <RenameTableOperation>(operations[2]); var renameTableOperation0 = (RenameTableOperation)operations[0]; var renameTableOperation1 = (RenameTableOperation)operations[1]; var renameTableOperation2 = (RenameTableOperation)operations[2]; Assert.Equal("dbo.MyTable0", renameTableOperation0.TableName); Assert.Equal("__mig_tmp__0", renameTableOperation0.NewTableName); Assert.Equal("dbo.MyTable1", renameTableOperation1.TableName); Assert.Equal("MyTable0", renameTableOperation1.NewTableName); Assert.Equal("dbo.__mig_tmp__0", renameTableOperation2.TableName); Assert.Equal("MyTable1", renameTableOperation2.NewTableName); }
public void Diff_finds_dropped_table() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var principalEntityType = targetModel.GetEntityType("Principal"); var dependentEntityType = new EntityType("OldDependent") { StorageName = "dbo.MyOldTable" }; var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int)); sourceModel.AddEntityType(dependentEntityType); dependentProperty.Annotations.Add(new Annotation( MetadataExtensions.Annotations.StorageTypeName, "int")); dependentEntityType.SetKey(dependentProperty); dependentEntityType.GetKey().StorageName = "MyOldPK"; var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty); foreignKey.StorageName = "MyOldFK"; foreignKey.Annotations.Add(new Annotation( MetadataExtensions.Annotations.CascadeDelete, "True")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <DropTableOperation>(operations[0]); var dropTableOperation = (DropTableOperation)operations[0]; Assert.Equal("dbo.MyOldTable", dropTableOperation.TableName); }
public void Model_differ_breaks_foreign_key_cycles() { var model = new Entity.Metadata.Model(); var firstType = model.AddEntityType("First"); var firstKey = firstType.SetPrimaryKey(firstType.AddProperty("ID", typeof(int), true)); var firstFk = firstType.AddProperty("FK", typeof(int), true); var secondType = model.AddEntityType("Second"); var secondKey = secondType.SetPrimaryKey(secondType.AddProperty("ID", typeof(int), true)); var secondFk = secondType.AddProperty("FK", typeof(int), true); firstType.AddForeignKey(firstFk, secondKey); secondType.AddForeignKey(secondFk, firstKey); var modelDiffer = new ModelDiffer(new RelationalTypeMapper()); var result = modelDiffer.GetDifferences(null, model); Assert.Equal(3, result.Count); var firstOperation = result[0] as CreateTableOperation; var secondOperation = result[1] as CreateTableOperation; var thirdOperation = result[2] as AddForeignKeyOperation; Assert.NotNull(firstOperation); Assert.NotNull(secondOperation); Assert.NotNull(thirdOperation); Assert.Equal(0, firstOperation.ForeignKeys.Count); Assert.Equal(1, secondOperation.ForeignKeys.Count); Assert.Equal(firstOperation.Name, thirdOperation.Table); }
public Migrator( [NotNull] DbContextConfiguration contextConfiguration, [NotNull] HistoryRepository historyRepository, [NotNull] MigrationAssembly migrationAssembly, [NotNull] ModelDiffer modelDiffer, [NotNull] IMigrationOperationSqlGeneratorFactory ddlSqlGeneratorFactory, [NotNull] SqlGenerator dmlSqlGenerator, [NotNull] SqlStatementExecutor sqlExecutor) { Check.NotNull(contextConfiguration, "contextConfiguration"); Check.NotNull(historyRepository, "historyRepository"); Check.NotNull(migrationAssembly, "migrationAssembly"); Check.NotNull(modelDiffer, "modelDiffer"); Check.NotNull(ddlSqlGeneratorFactory, "ddlSqlGeneratorFactory"); Check.NotNull(dmlSqlGenerator, "dmlSqlGenerator"); Check.NotNull(sqlExecutor, "sqlExecutor"); _contextConfiguration = contextConfiguration; _historyRepository = historyRepository; _migrationAssembly = migrationAssembly; _modelDiffer = modelDiffer; _ddlSqlGeneratorFactory = ddlSqlGeneratorFactory; _dmlSqlGenerator = dmlSqlGenerator; _sqlExecutor = sqlExecutor; }
protected virtual MigrationInfo CreateMigration([NotNull] string migrationName) { Check.NotEmpty(migrationName, "migrationName"); var sourceModel = MigrationAssembly.ModelSnapshot?.Model; var targetModel = _model; IReadOnlyList <MigrationOperation> upgradeOperations, downgradeOperations; if (sourceModel != null) { upgradeOperations = ModelDiffer.Diff(sourceModel, targetModel); downgradeOperations = ModelDiffer.Diff(targetModel, sourceModel); } else { upgradeOperations = ModelDiffer.CreateSchema(targetModel); downgradeOperations = ModelDiffer.DropSchema(targetModel); } return (new MigrationInfo(CreateMigrationId(migrationName)) { TargetModel = targetModel, UpgradeOperations = upgradeOperations, DowngradeOperations = downgradeOperations }); }
public void Diff_finds_created_table() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var principalEntityType = targetModel.GetEntityType("Principal"); var dependentEntityType = new EntityType("NewDependent") { StorageName = "dbo.MyNewTable" }; var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int)); targetModel.AddEntityType(dependentEntityType); dependentProperty.Annotations.Add(new Annotation( MetadataExtensions.Annotations.StorageTypeName, "int")); dependentEntityType.SetKey(dependentProperty); dependentEntityType.GetKey().StorageName = "MyNewPK"; var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty); foreignKey.StorageName = "MyNewFK"; foreignKey.Annotations.Add(new Annotation( MetadataExtensions.Annotations.CascadeDelete, "True")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(2, operations.Count); Assert.IsType <CreateTableOperation>(operations[0]); Assert.IsType <AddForeignKeyOperation>(operations[1]); var createTableOperation = (CreateTableOperation)operations[0]; Assert.Equal("dbo.MyNewTable", createTableOperation.Table.Name); Assert.Equal(1, createTableOperation.Table.Columns.Count); Assert.Equal("Id", createTableOperation.Table.Columns[0].Name); Assert.Equal(typeof(int), createTableOperation.Table.Columns[0].ClrType); Assert.NotNull(createTableOperation.Table.PrimaryKey); Assert.Equal("MyNewPK", createTableOperation.Table.PrimaryKey.Name); Assert.Equal(1, createTableOperation.Table.ForeignKeys.Count); Assert.Equal("MyNewFK", createTableOperation.Table.ForeignKeys[0].Name); Assert.True(createTableOperation.Table.ForeignKeys[0].CascadeDelete); var addForeignKeyOperation = (AddForeignKeyOperation)operations[1]; Assert.Equal("MyNewFK", addForeignKeyOperation.ForeignKeyName); Assert.Equal("dbo.MyNewTable", addForeignKeyOperation.TableName); Assert.Equal("dbo.MyTable1", addForeignKeyOperation.ReferencedTableName); Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ColumnNames); Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ReferencedColumnNames); Assert.True(addForeignKeyOperation.CascadeDelete); }
public void DiffSource_creates_operations() { var operations = new ModelDiffer(new DatabaseBuilder()).DiffSource(CreateModel()); Assert.Equal(3, operations.Count); var createTableOperation0 = (CreateTableOperation)operations[0]; var createTableOperation1 = (CreateTableOperation)operations[1]; var addForeignKeyOperation = (AddForeignKeyOperation)operations[2]; Assert.Equal("dbo.MyTable0", createTableOperation0.Table.Name); Assert.Equal("dbo.MyTable1", createTableOperation1.Table.Name); Assert.Equal("MyFK", addForeignKeyOperation.ForeignKeyName); }
public void DiffTarget_creates_operations() { var operations = new ModelDiffer(new DatabaseBuilder()).DiffTarget(CreateModel()); Assert.Equal(3, operations.Count); var dropForeignKeyOperation = (DropForeignKeyOperation)operations[0]; var dropTableOperation0 = (DropTableOperation)operations[1]; var dropTableOperation1 = (DropTableOperation)operations[2]; Assert.Equal("MyFK", dropForeignKeyOperation.ForeignKeyName); Assert.Equal("dbo.MyTable0", dropTableOperation0.TableName); Assert.Equal("dbo.MyTable1", dropTableOperation1.TableName); }
public void DropSchema_creates_operations() { var operations = new ModelDiffer(new DatabaseBuilder()).DropSchema(CreateModel()); Assert.Equal(3, operations.Count); var dropForeignKeyOperation = (DropForeignKeyOperation)operations[0]; var dropTableOperation0 = (DropTableOperation)operations[1]; var dropTableOperation1 = (DropTableOperation)operations[2]; Assert.Equal("MyFK", dropForeignKeyOperation.ForeignKeyName); Assert.Equal("dbo.MyTable0", dropTableOperation0.TableName); Assert.Equal("dbo.MyTable1", dropTableOperation1.TableName); }
public SqlServerDataStoreCreator( [NotNull] SqlServerConnection connection, [NotNull] ModelDiffer modelDiffer, [NotNull] SqlServerMigrationOperationSqlGenerator sqlGenerator, [NotNull] SqlStatementExecutor statementExecutor) { Check.NotNull(connection, "connection"); Check.NotNull(modelDiffer, "modelDiffer"); Check.NotNull(sqlGenerator, "sqlGenerator"); Check.NotNull(statementExecutor, "statementExecutor"); _connection = connection; _modelDiffer = modelDiffer; _sqlGenerator = sqlGenerator; _statementExecutor = statementExecutor; }
public void CreateSchema_creates_operations() { var operations = new ModelDiffer(new DatabaseBuilder()).CreateSchema(CreateModel()); Assert.Equal(4, operations.Count); var createTableOperation0 = (CreateTableOperation)operations[0]; var createTableOperation1 = (CreateTableOperation)operations[1]; var addForeignKeyOperation = (AddForeignKeyOperation)operations[2]; var createIndexOperation = (CreateIndexOperation)operations[3]; Assert.Equal("dbo.MyTable0", createTableOperation0.Table.Name); Assert.Equal("dbo.MyTable1", createTableOperation1.Table.Name); Assert.Equal("MyFK", addForeignKeyOperation.ForeignKeyName); Assert.Equal("MyIndex", createIndexOperation.IndexName); }
public MigrationScaffolder( [NotNull] DbContextConfiguration contextConfiguration, [NotNull] MigrationAssembly migrationAssembly, [NotNull] ModelDiffer modelDiffer, [NotNull] MigrationCodeGenerator migrationCodeGenerator) { Check.NotNull(contextConfiguration, "contextConfiguration"); Check.NotNull(migrationAssembly, "migrationAssembly"); Check.NotNull(modelDiffer, "modelDiffer"); Check.NotNull(migrationCodeGenerator, "migrationCodeGenerator"); _contextConfiguration = contextConfiguration; _migrationAssembly = migrationAssembly; _modelDiffer = modelDiffer; _migrationCodeGenerator = migrationCodeGenerator; }
public MyMigrationScaffolder( DbContextConfiguration contextConfiguration, MigrationAssembly migrationAssembly, ModelDiffer modelDiffer, MigrationCodeGenerator migrationCodeGenerator, Action <string, string, string> migrationValidation, Action <string, string> modelValidation) : base( contextConfiguration, migrationAssembly, modelDiffer, migrationCodeGenerator) { _migrationValidation = migrationValidation; _modelValidation = modelValidation; }
public SQLiteDataStoreCreator( [NotNull] SQLiteConnection connection, [NotNull] SqlStatementExecutor executor, [NotNull] SQLiteMigrationOperationSqlGeneratorFactory generatorFactory, [NotNull] ModelDiffer modelDiffer) { Check.NotNull(connection, "connection"); Check.NotNull(executor, "executor"); Check.NotNull(generatorFactory, "generatorFactory"); Check.NotNull(modelDiffer, "modelDiffer"); _connection = connection; _executor = executor; _generatorFactory = generatorFactory; _modelDiffer = modelDiffer; }
public SQLiteDataStoreCreator( [NotNull] SQLiteConnection connection, [NotNull] SqlStatementExecutor executor, [NotNull] SQLiteMigrationOperationSqlGenerator generator, [NotNull] ModelDiffer modelDiffer) { Check.NotNull(connection, "connection"); Check.NotNull(executor, "executor"); Check.NotNull(generator, "generator"); Check.NotNull(modelDiffer, "modelDiffer"); _connection = connection; _executor = executor; _generator = generator; _modelDiffer = modelDiffer; }
public void Diff_finds_renamed_table() { var sourceModel = CreateModel(); var targetModel = CreateModel(); targetModel.GetEntityType("Dependent").StorageName = "dbo.MyNewTable0"; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <RenameTableOperation>(operations[0]); var renameTableOperation = (RenameTableOperation)operations[0]; Assert.Equal("dbo.MyTable0", renameTableOperation.TableName); Assert.Equal("MyNewTable0", renameTableOperation.NewTableName); }
public void Diff_finds_renamed_column() { var sourceModel = CreateModel(); var targetModel = CreateModel(); targetModel.GetEntityType("Dependent").GetProperty("Id").SetColumnName("NewId"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <RenameColumnOperation>(operations[0]); var renameColumnOperation = (RenameColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", renameColumnOperation.TableName); Assert.Equal("Id", renameColumnOperation.ColumnName); Assert.Equal("NewId", renameColumnOperation.NewColumnName); }
public SqlServerMigrator( [NotNull] DbContextConfiguration contextConfiguration, [NotNull] HistoryRepository historyRepository, [NotNull] MigrationAssembly migrationAssembly, [NotNull] ModelDiffer modelDiffer, [NotNull] SqlServerMigrationOperationSqlGeneratorFactory sqlGeneratorFactory, [NotNull] SqlServerSqlGenerator sqlGenerator, [NotNull] SqlStatementExecutor sqlStatementExecutor) : base( contextConfiguration, historyRepository, migrationAssembly, modelDiffer, sqlGeneratorFactory, sqlGenerator, sqlStatementExecutor) { }
public void Diff_finds_dropped_column() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = sourceModel.GetEntityType("Dependent").AddProperty("MyOldProperty", typeof(string)); property.StorageName = "MyOldColumn"; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <DropColumnOperation>(operations[0]); var dropColumnOperation = (DropColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropColumnOperation.TableName); Assert.Equal("MyOldColumn", dropColumnOperation.ColumnName); }
public void Diff_finds_dropped_foreign_key() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); entityType.RemoveForeignKey(entityType.ForeignKeys[0]); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <DropForeignKeyOperation>(operations[0]); var dropForeignKeyOperation = (DropForeignKeyOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropForeignKeyOperation.TableName); Assert.Equal("MyFK", dropForeignKeyOperation.ForeignKeyName); }
public void Diff_finds_renamed_table() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var dependentEntity = targetModel.GetEntityType("Dependent"); dependentEntity.SetTableName("MyNewTable0"); dependentEntity.SetSchema("dbo"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<RenameTableOperation>(operations[0]); var renameTableOperation = (RenameTableOperation)operations[0]; Assert.Equal("dbo.MyTable0", renameTableOperation.TableName); Assert.Equal("MyNewTable0", renameTableOperation.NewTableName); }
public void Diff_finds_removed_index() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); entityType.RemoveIndex(entityType.Indexes[0]); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <DropIndexOperation>(operations[0]); var dropIndexOperation = (DropIndexOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropIndexOperation.TableName); Assert.Equal("MyIndex", dropIndexOperation.IndexName); }
public MyMigrationScaffolder( DbContext context, IDbContextOptions options, IModel model, MigrationAssembly migrationAssembly, ModelDiffer modelDiffer, MigrationCodeGenerator migrationCodeGenerator, Action <string, string, string> migrationValidation, Action <string, string> modelValidation) : base( context, options, model, migrationAssembly, modelDiffer, migrationCodeGenerator) { _migrationValidation = migrationValidation; _modelValidation = modelValidation; }
public void Diff_finds_moved_table() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var dependentEntity = targetModel.GetEntityType("Dependent"); dependentEntity.SetTableName("MyTable0"); dependentEntity.SetSchema("newdbo"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <MoveTableOperation>(operations[0]); var moveTableOperation = (MoveTableOperation)operations[0]; Assert.Equal("dbo.MyTable0", moveTableOperation.TableName); Assert.Equal("newdbo", moveTableOperation.NewSchema); }
public void Diff_finds_altered_column_nullable_flag() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = targetModel.GetEntityType("Dependent").GetProperty("MyProperty"); property.IsNullable = false; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <AlterColumnOperation>(operations[0]); var alterColumnOperation = (AlterColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", alterColumnOperation.TableName); Assert.Equal("MyColumn", alterColumnOperation.NewColumn.Name); Assert.False(alterColumnOperation.NewColumn.IsNullable); }
public void Diff_finds_added_column() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = targetModel.GetEntityType("Dependent").AddProperty("MyNewProperty", typeof(string)); property.SetColumnName("MyNewColumn"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <AddColumnOperation>(operations[0]); var addColumnOperation = (AddColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", addColumnOperation.TableName); Assert.Equal("MyNewColumn", addColumnOperation.Column.Name); Assert.Equal(typeof(string), addColumnOperation.Column.ClrType); }
public void Diff_finds_dropped_default_constraint_with_sql() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = sourceModel.GetEntityType("Dependent").GetProperty("MyProperty"); property.Annotations.Add(new Annotation( MetadataExtensions.Annotations.ColumnDefaultSql, "MyDefaultSql")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <DropDefaultConstraintOperation>(operations[0]); var dropDefaultConstraintOperation = (DropDefaultConstraintOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropDefaultConstraintOperation.TableName); Assert.Equal("MyColumn", dropDefaultConstraintOperation.ColumnName); }
public void Diff_handles_transitive_index_renames() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var principalEntityType = sourceModel.GetEntityType("Principal"); var property = principalEntityType.AddProperty("P1", typeof(string)); var index = principalEntityType.AddIndex(property); index.SetIndexName("IX1"); property = principalEntityType.AddProperty("P2", typeof(string)); index = principalEntityType.AddIndex(property); index.SetIndexName("IX2"); principalEntityType = targetModel.GetEntityType("Principal"); property = principalEntityType.AddProperty("P1", typeof(string)); index = principalEntityType.AddIndex(property); index.SetIndexName("IX2"); property = principalEntityType.AddProperty("P2", typeof(string)); index = principalEntityType.AddIndex(property); index.SetIndexName("IX1"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(3, operations.Count); Assert.IsType <RenameIndexOperation>(operations[0]); Assert.IsType <RenameIndexOperation>(operations[1]); Assert.IsType <RenameIndexOperation>(operations[2]); var renameIndexOperation0 = (RenameIndexOperation)operations[0]; var renameIndexOperation1 = (RenameIndexOperation)operations[1]; var renameIndexOperation2 = (RenameIndexOperation)operations[2]; Assert.Equal("IX1", renameIndexOperation0.IndexName); Assert.Equal("__mig_tmp__0", renameIndexOperation0.NewIndexName); Assert.Equal("IX2", renameIndexOperation1.IndexName); Assert.Equal("IX1", renameIndexOperation1.NewIndexName); Assert.Equal("__mig_tmp__0", renameIndexOperation2.IndexName); Assert.Equal("IX2", renameIndexOperation2.NewIndexName); }
public void Diff_finds_added_foreign_key() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = sourceModel.GetEntityType("Dependent"); entityType.RemoveForeignKey(entityType.ForeignKeys[0]); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <AddForeignKeyOperation>(operations[0]); var addForeignKeyOperation = (AddForeignKeyOperation)operations[0]; Assert.Equal("dbo.MyTable0", addForeignKeyOperation.TableName); Assert.Equal("MyFK", addForeignKeyOperation.ForeignKeyName); Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ColumnNames); Assert.True(addForeignKeyOperation.CascadeDelete); }
public void Diff_finds_altered_column_data_type() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = targetModel.GetEntityType("Dependent").GetProperty("MyProperty"); property.Annotations.Add(new Annotation( MetadataExtensions.Annotations.StorageTypeName, "nvarchar(10)")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType <AlterColumnOperation>(operations[0]); var alterColumnOperation = (AlterColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", alterColumnOperation.TableName); Assert.Equal("MyColumn", alterColumnOperation.NewColumn.Name); Assert.Equal("nvarchar(10)", alterColumnOperation.NewColumn.DataType); }
public void Diff_finds_dropped_table() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var principalEntityType = targetModel.GetEntityType("Principal"); var dependentEntityType = new EntityType("OldDependent"); dependentEntityType.SetTableName("MyOldTable"); dependentEntityType.SetSchema("dbo"); var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int)); sourceModel.AddEntityType(dependentEntityType); dependentProperty.Annotations.Add(new Annotation( MetadataExtensions.Annotations.StorageTypeName, "int")); dependentEntityType.SetKey(dependentProperty); dependentEntityType.GetKey().SetKeyName("MyOldPK"); var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty); foreignKey.SetKeyName("MyOldFK"); foreignKey.Annotations.Add(new Annotation( MetadataExtensions.Annotations.CascadeDelete, "True")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<DropTableOperation>(operations[0]); var dropTableOperation = (DropTableOperation)operations[0]; Assert.Equal("dbo.MyOldTable", dropTableOperation.TableName); }
public void Diff_finds_dropped_column() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = sourceModel.GetEntityType("Dependent").AddProperty("MyOldProperty", typeof(string)); property.SetColumnName("MyOldColumn"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<DropColumnOperation>(operations[0]); var dropColumnOperation = (DropColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropColumnOperation.TableName); Assert.Equal("MyOldColumn", dropColumnOperation.ColumnName); }
public void Diff_finds_renamed_column() { var sourceModel = CreateModel(); var targetModel = CreateModel(); targetModel.GetEntityType("Dependent").GetProperty("Id").SetColumnName("NewId"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<RenameColumnOperation>(operations[0]); var renameColumnOperation = (RenameColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", renameColumnOperation.TableName); Assert.Equal("Id", renameColumnOperation.ColumnName); Assert.Equal("NewId", renameColumnOperation.NewColumnName); }
public void Diff_finds_altered_column_clr_type() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); var property = entityType.GetProperty("MyProperty"); entityType.RemoveProperty(property); property = entityType.AddProperty("MyProperty", typeof(double)); property.SetColumnName("MyColumn"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<AlterColumnOperation>(operations[0]); var alterColumnOperation = (AlterColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", alterColumnOperation.TableName); Assert.Equal("MyColumn", alterColumnOperation.NewColumn.Name); Assert.Equal(typeof(double), alterColumnOperation.NewColumn.ClrType); }
public void Diff_finds_altered_column_nullable_flag() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = targetModel.GetEntityType("Dependent").GetProperty("MyProperty"); property.IsNullable = false; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<AlterColumnOperation>(operations[0]); var alterColumnOperation = (AlterColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", alterColumnOperation.TableName); Assert.Equal("MyColumn", alterColumnOperation.NewColumn.Name); Assert.False(alterColumnOperation.NewColumn.IsNullable); }
public void Diff_finds_dropped_foreign_key() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); entityType.RemoveForeignKey(entityType.ForeignKeys[0]); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<DropForeignKeyOperation>(operations[0]); var dropForeignKeyOperation = (DropForeignKeyOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropForeignKeyOperation.TableName); Assert.Equal("MyFK", dropForeignKeyOperation.ForeignKeyName); }
public void Diff_finds_altered_column_data_type() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = targetModel.GetEntityType("Dependent").GetProperty("MyProperty"); property.Annotations.Add(new Annotation( MetadataExtensions.Annotations.StorageTypeName, "nvarchar(10)")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<AlterColumnOperation>(operations[0]); var alterColumnOperation = (AlterColumnOperation)operations[0]; Assert.Equal("dbo.MyTable0", alterColumnOperation.TableName); Assert.Equal("MyColumn", alterColumnOperation.NewColumn.Name); Assert.Equal("nvarchar(10)", alterColumnOperation.NewColumn.DataType); }
public void Diff_handles_transitive_index_renames() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var principalEntityType = sourceModel.GetEntityType("Principal"); var property = principalEntityType.AddProperty("P1", typeof(string)); var index = principalEntityType.AddIndex(property); index.SetIndexName("IX1"); property = principalEntityType.AddProperty("P2", typeof(string)); index = principalEntityType.AddIndex(property); index.SetIndexName("IX2"); principalEntityType = targetModel.GetEntityType("Principal"); property = principalEntityType.AddProperty("P1", typeof(string)); index = principalEntityType.AddIndex(property); index.SetIndexName("IX2"); property = principalEntityType.AddProperty("P2", typeof(string)); index = principalEntityType.AddIndex(property); index.SetIndexName("IX1"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(3, operations.Count); Assert.IsType<RenameIndexOperation>(operations[0]); Assert.IsType<RenameIndexOperation>(operations[1]); Assert.IsType<RenameIndexOperation>(operations[2]); var renameIndexOperation0 = (RenameIndexOperation)operations[0]; var renameIndexOperation1 = (RenameIndexOperation)operations[1]; var renameIndexOperation2 = (RenameIndexOperation)operations[2]; Assert.Equal("IX1", renameIndexOperation0.IndexName); Assert.Equal("__mig_tmp__0", renameIndexOperation0.NewIndexName); Assert.Equal("IX2", renameIndexOperation1.IndexName); Assert.Equal("IX1", renameIndexOperation1.NewIndexName); Assert.Equal("__mig_tmp__0", renameIndexOperation2.IndexName); Assert.Equal("IX2", renameIndexOperation2.NewIndexName); }
public void Diff_finds_dropped_default_constraint_with_sql() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var property = sourceModel.GetEntityType("Dependent").GetProperty("MyProperty"); property.Annotations.Add(new Annotation( MetadataExtensions.Annotations.ColumnDefaultSql, "MyDefaultSql")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<DropDefaultConstraintOperation>(operations[0]); var dropDefaultConstraintOperation = (DropDefaultConstraintOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropDefaultConstraintOperation.TableName); Assert.Equal("MyColumn", dropDefaultConstraintOperation.ColumnName); }
public void Diff_finds_index_with_unique_updated() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); var index = entityType.Indexes[0]; index.IsUnique = false; var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(2, operations.Count); Assert.IsType<DropIndexOperation>(operations[0]); Assert.IsType<CreateIndexOperation>(operations[1]); var dropIndexOperation = (DropIndexOperation)operations[0]; var createIndexOperation = (CreateIndexOperation)operations[1]; Assert.Equal("dbo.MyTable0", dropIndexOperation.TableName); Assert.Equal("MyIndex", dropIndexOperation.IndexName); Assert.Equal("dbo.MyTable0", createIndexOperation.TableName); Assert.Equal("MyIndex", createIndexOperation.IndexName); Assert.False(createIndexOperation.IsUnique); }
public void Diff_handles_transitive_table_renames() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var dependentEntityType = targetModel.GetEntityType("Dependent"); var principalEntityType = targetModel.GetEntityType("Principal"); var principalTableName = principalEntityType.TableName(); principalEntityType.SetTableName(dependentEntityType.TableName()); dependentEntityType.SetTableName(principalTableName); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(3, operations.Count); Assert.IsType<RenameTableOperation>(operations[0]); Assert.IsType<RenameTableOperation>(operations[1]); Assert.IsType<RenameTableOperation>(operations[2]); var renameTableOperation0 = (RenameTableOperation)operations[0]; var renameTableOperation1 = (RenameTableOperation)operations[1]; var renameTableOperation2 = (RenameTableOperation)operations[2]; Assert.Equal("dbo.MyTable0", renameTableOperation0.TableName); Assert.Equal("__mig_tmp__0", renameTableOperation0.NewTableName); Assert.Equal("dbo.MyTable1", renameTableOperation1.TableName); Assert.Equal("MyTable0", renameTableOperation1.NewTableName); Assert.Equal("dbo.__mig_tmp__0", renameTableOperation2.TableName); Assert.Equal("MyTable1", renameTableOperation2.NewTableName); }
public void Diff_finds_renamed_index() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); var index = entityType.Indexes[0]; index.SetIndexName("MyNewIndex"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<RenameIndexOperation>(operations[0]); var renameIndexOperation = (RenameIndexOperation)operations[0]; Assert.Equal("dbo.MyTable0", renameIndexOperation.TableName); Assert.Equal("MyIndex", renameIndexOperation.IndexName); Assert.Equal("MyNewIndex", renameIndexOperation.NewIndexName); }
public void Diff_finds_removed_index() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); entityType.RemoveIndex(entityType.Indexes[0]); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<DropIndexOperation>(operations[0]); var dropIndexOperation = (DropIndexOperation)operations[0]; Assert.Equal("dbo.MyTable0", dropIndexOperation.TableName); Assert.Equal("MyIndex", dropIndexOperation.IndexName); }
public void Diff_finds_added_index() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = sourceModel.GetEntityType("Dependent"); entityType.RemoveIndex(entityType.Indexes[0]); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<CreateIndexOperation>(operations[0]); var createIndexOperation = (CreateIndexOperation)operations[0]; Assert.Equal("dbo.MyTable0", createIndexOperation.TableName); Assert.Equal("MyIndex", createIndexOperation.IndexName); Assert.Equal(new[] { "Id" }, createIndexOperation.ColumnNames); Assert.True(createIndexOperation.IsUnique); }
public MyMigrationScaffolder( DbContextConfiguration contextConfiguration, MigrationAssembly migrationAssembly, ModelDiffer modelDiffer, MigrationCodeGenerator migrationCodeGenerator, Action<string, string, string> migrationValidation, Action<string, string> modelValidation) : base( contextConfiguration, migrationAssembly, modelDiffer, migrationCodeGenerator) { _migrationValidation = migrationValidation; _modelValidation = modelValidation; }
public void Diff_finds_created_table() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var principalEntityType = targetModel.GetEntityType("Principal"); var dependentEntityType = new EntityType("NewDependent"); dependentEntityType.SetTableName("MyNewTable"); dependentEntityType.SetSchema("dbo"); var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int)); targetModel.AddEntityType(dependentEntityType); dependentProperty.Annotations.Add(new Annotation( MetadataExtensions.Annotations.StorageTypeName, "int")); dependentEntityType.SetKey(dependentProperty); dependentEntityType.GetKey().SetKeyName("MyNewPK"); var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty); foreignKey.SetKeyName("MyNewFK"); foreignKey.Annotations.Add(new Annotation( MetadataExtensions.Annotations.CascadeDelete, "True")); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(2, operations.Count); Assert.IsType<CreateTableOperation>(operations[0]); Assert.IsType<AddForeignKeyOperation>(operations[1]); var createTableOperation = (CreateTableOperation)operations[0]; Assert.Equal("dbo.MyNewTable", createTableOperation.Table.Name); Assert.Equal(1, createTableOperation.Table.Columns.Count); Assert.Equal("Id", createTableOperation.Table.Columns[0].Name); Assert.Equal(typeof(int), createTableOperation.Table.Columns[0].ClrType); Assert.NotNull(createTableOperation.Table.PrimaryKey); Assert.Equal("MyNewPK", createTableOperation.Table.PrimaryKey.Name); Assert.Equal(1, createTableOperation.Table.ForeignKeys.Count); Assert.Equal("MyNewFK", createTableOperation.Table.ForeignKeys[0].Name); Assert.True(createTableOperation.Table.ForeignKeys[0].CascadeDelete); var addForeignKeyOperation = (AddForeignKeyOperation)operations[1]; Assert.Equal("MyNewFK", addForeignKeyOperation.ForeignKeyName); Assert.Equal("dbo.MyNewTable", addForeignKeyOperation.TableName); Assert.Equal("dbo.MyTable1", addForeignKeyOperation.ReferencedTableName); Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ColumnNames); Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ReferencedColumnNames); Assert.True(addForeignKeyOperation.CascadeDelete); }
public void Diff_finds_updated_primary_key() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = targetModel.GetEntityType("Dependent"); entityType.SetKey(entityType.GetProperty("MyProperty")); entityType.GetKey().SetKeyName("MyNewPK"); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(2, operations.Count); Assert.IsType<DropPrimaryKeyOperation>(operations[0]); Assert.IsType<AddPrimaryKeyOperation>(operations[1]); var dropPrimaryKeyOperation = (DropPrimaryKeyOperation)operations[0]; var addPrimaryKeyOperation = (AddPrimaryKeyOperation)operations[1]; Assert.Equal("dbo.MyTable0", dropPrimaryKeyOperation.TableName); Assert.Equal("MyPK0", dropPrimaryKeyOperation.PrimaryKeyName); Assert.Equal("dbo.MyTable0", addPrimaryKeyOperation.TableName); Assert.Equal("MyNewPK", addPrimaryKeyOperation.PrimaryKeyName); Assert.Equal(new[] { "MyColumn" }, addPrimaryKeyOperation.ColumnNames); }
public void Diff_finds_added_foreign_key() { var sourceModel = CreateModel(); var targetModel = CreateModel(); var entityType = sourceModel.GetEntityType("Dependent"); entityType.RemoveForeignKey(entityType.ForeignKeys[0]); var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel); Assert.Equal(1, operations.Count); Assert.IsType<AddForeignKeyOperation>(operations[0]); var addForeignKeyOperation = (AddForeignKeyOperation)operations[0]; Assert.Equal("dbo.MyTable0", addForeignKeyOperation.TableName); Assert.Equal("MyFK", addForeignKeyOperation.ForeignKeyName); Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ColumnNames); Assert.True(addForeignKeyOperation.CascadeDelete); }