public virtual IReadOnlyList <MigrationOperation> CreateSchema([NotNull] IModel model) { Check.NotNull(model, "model"); _operations = new MigrationOperationCollection(); _operations.AddRange(GetSequences(model) .Select(s => OperationFactory.CreateSequenceOperation(s))); _operations.AddRange(model.EntityTypes .Select(t => OperationFactory.CreateTableOperation(t))); // TODO: GitHub#1107 _operations.AddRange(_operations.Get <CreateTableOperation>() .SelectMany(o => o.ForeignKeys)); _operations.AddRange(_operations.Get <CreateTableOperation>() .SelectMany(o => o.Indexes)); return(OperationProcessor.Process(_operations, new Metadata.Model(), model)); }
private void HandleTransitiveRenames() { const string temporaryNamePrefix = "__mig_tmp__"; var temporaryNameIndex = 0; _operations.Set(HandleTransitiveRenames( _operations.Get <RenameSequenceOperation>(), op => null, op => op.SequenceName, op => new SchemaQualifiedName(op.NewSequenceName, op.SequenceName.Schema), op => new SchemaQualifiedName(temporaryNamePrefix + temporaryNameIndex++, op.SequenceName.Schema), (parentName, name, newName) => new RenameSequenceOperation(name, SchemaQualifiedName.Parse(newName).Name))); _operations.Set(HandleTransitiveRenames( _operations.Get <RenameTableOperation>(), op => null, op => op.TableName, op => new SchemaQualifiedName(op.NewTableName, op.TableName.Schema), op => new SchemaQualifiedName(temporaryNamePrefix + temporaryNameIndex++, op.TableName.Schema), (parentName, name, newName) => new RenameTableOperation(name, SchemaQualifiedName.Parse(newName).Name))); _operations.Set(HandleTransitiveRenames( _operations.Get <RenameColumnOperation>(), op => op.TableName, op => op.ColumnName, op => op.NewColumnName, op => temporaryNamePrefix + temporaryNameIndex++, (parentName, name, newName) => new RenameColumnOperation(parentName, name, newName))); _operations.Set(HandleTransitiveRenames( _operations.Get <RenameIndexOperation>(), op => op.TableName, op => op.IndexName, op => op.NewIndexName, op => temporaryNamePrefix + temporaryNameIndex++, (parentName, name, newName) => new RenameIndexOperation(parentName, name, newName))); }
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 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()); }