public virtual IReadOnlyList <MigrationOperation> Diff([NotNull] IModel sourceModel, [NotNull] IModel targetModel) { Check.NotNull(sourceModel, "sourceModel"); Check.NotNull(targetModel, "targetModel"); _sourceMapping = _databaseBuilder.GetMapping(sourceModel); _targetMapping = _databaseBuilder.GetMapping(targetModel); _operations = new MigrationOperationCollection(); DiffSequences(); DiffTables(); // TODO: Add more unit tests for the operation order. HandleTransitiveRenames(); return (((IEnumerable <MigrationOperation>)_operations.Get <DropIndexOperation>()) .Concat(_operations.Get <DropForeignKeyOperation>()) .Concat(_operations.Get <DropPrimaryKeyOperation>()) .Concat(_operations.Get <DropColumnOperation>()) .Concat(_operations.Get <DropTableOperation>()) .Concat(_operations.Get <MoveTableOperation>()) .Concat(_operations.Get <RenameTableOperation>()) .Concat(_operations.Get <RenameColumnOperation>()) .Concat(_operations.Get <RenameIndexOperation>()) .Concat(_operations.Get <AlterColumnOperation>()) .Concat(_operations.Get <CreateTableOperation>()) .Concat(_operations.Get <AddColumnOperation>()) .Concat(_operations.Get <AddPrimaryKeyOperation>()) .Concat(_operations.Get <AddForeignKeyOperation>()) .Concat(_operations.Get <CreateIndexOperation>()) .ToArray()); }
public virtual ModelDatabaseMapping GetMapping([NotNull] IModel model) { Check.NotNull(model, "model"); return _mappingCache.GetOrAdd(model, m => { // TODO: Consider making this lazy since we don't want to load the whole model just to // save changes to a single entity. var database = new DatabaseModel(); var mapping = new ModelDatabaseMapping(m, database); foreach (var entityType in m.EntityTypes) { var table = BuildTable(database, entityType); mapping.Map(entityType, table); foreach (var property in entityType.Properties) { mapping.Map(property, BuildColumn(table, property)); } var primaryKey = entityType.GetKey(); mapping.Map(primaryKey, BuildPrimaryKey(database, primaryKey)); foreach (var index in entityType.Indexes) { mapping.Map(index, BuildIndex(database, index)); } } foreach (var entityType in m.EntityTypes) { foreach (var foreignKey in entityType.ForeignKeys) { mapping.Map(foreignKey, BuildForeignKey(database, foreignKey)); } } return mapping; }); }
public virtual IReadOnlyList <MigrationOperation> Diff([NotNull] IModel sourceModel, [NotNull] IModel targetModel) { Check.NotNull(sourceModel, "sourceModel"); Check.NotNull(targetModel, "targetModel"); _sourceMapping = _databaseBuilder.GetMapping(sourceModel); _targetMapping = _databaseBuilder.GetMapping(targetModel); _operations = new MigrationOperationCollection(); DiffSequences(); DiffTables(); // TODO: Needs to handle name reuse between renames and circular renames. // TODO: Add unit tests for rename column conflict and operation order. HandleRenameConflicts(); return (((IEnumerable <MigrationOperation>)_operations.Get <DropIndexOperation>()) .Concat(_operations.Get <DropForeignKeyOperation>()) .Concat(_operations.Get <DropPrimaryKeyOperation>()) .Concat(_operations.Get <DropDefaultConstraintOperation>()) .Concat(_operations.Get <MoveTableOperation>()) .Concat(_operations.Get <RenameTableOperation>()) .Concat(_operations.Get <RenameColumnOperation>()) .Concat(_operations.Get <RenameIndexOperation>()) .Concat(_operations.Get <CreateTableOperation>()) .Concat(_operations.Get <AddColumnOperation>()) .Concat(_operations.Get <AlterColumnOperation>()) .Concat(_operations.Get <AddDefaultConstraintOperation>()) .Concat(_operations.Get <AddPrimaryKeyOperation>()) .Concat(_operations.Get <AddForeignKeyOperation>()) .Concat(_operations.Get <CreateIndexOperation>()) .Concat(_operations.Get <DropColumnOperation>()) .Concat(_operations.Get <DropTableOperation>()) .ToArray()); }