Skip to content

Commit

Permalink
Clean up Migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
bricelam committed Jul 8, 2020
1 parent 69c10fe commit 45fe7cd
Show file tree
Hide file tree
Showing 24 changed files with 140 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,16 @@ protected virtual void Generate([NotNull] AddUniqueConstraintOperation operation
}

/// <summary>
/// Generates code for an <see cref="CreateCheckConstraintOperation" />.
/// Generates code for an <see cref="AddCheckConstraintOperation" />.
/// </summary>
/// <param name="operation"> The operation. </param>
/// <param name="builder"> The builder code is added to. </param>
protected virtual void Generate([NotNull] CreateCheckConstraintOperation operation, [NotNull] IndentedStringBuilder builder)
protected virtual void Generate([NotNull] AddCheckConstraintOperation operation, [NotNull] IndentedStringBuilder builder)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

builder.AppendLine(".CreateCheckConstraint(");
builder.AppendLine(".AddCheckConstraint(");

using (builder.Indent())
{
Expand Down
23 changes: 5 additions & 18 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class MigrationsModelDiffer : IMigrationsModelDiffer
{
typeof(AddForeignKeyOperation),
typeof(CreateIndexOperation),
typeof(CreateCheckConstraintOperation)
typeof(AddCheckConstraintOperation)
};

private IUpdateAdapter _sourceUpdateAdapter;
Expand Down Expand Up @@ -662,7 +662,7 @@ protected virtual IEnumerable<MigrationOperation> Add(
.Cast<AddUniqueConstraintOperation>());
createTableOperation.CheckConstraints.AddRange(
target.CheckConstraints.SelectMany(c => Add(c, diffContext))
.Cast<CreateCheckConstraintOperation>());
.Cast<AddCheckConstraintOperation>());

diffContext.AddCreate(target, createTableOperation);

Expand Down Expand Up @@ -1395,20 +1395,7 @@ protected virtual IEnumerable<MigrationOperation> Add(
[NotNull] ITableIndex target,
[NotNull] DiffContext diffContext)
{
var targetTable = target.Table;

var operation = new CreateIndexOperation
{
Name = target.Name,
Schema = targetTable.Schema,
Table = targetTable.Name,
Columns = target.Columns.Select(c => c.Name).ToArray(),
IsUnique = target.IsUnique,
Filter = target.Filter
};
operation.AddAnnotations(target.GetAnnotations());

yield return operation;
yield return CreateIndexOperation.For(target);
}

/// <summary>
Expand Down Expand Up @@ -1477,7 +1464,7 @@ protected virtual IEnumerable<MigrationOperation> Add([NotNull] ICheckConstraint
{
var targetEntityType = target.EntityType;

var operation = new CreateCheckConstraintOperation
var operation = new AddCheckConstraintOperation
{
Name = target.Name,
Sql = target.Sql,
Expand Down Expand Up @@ -2127,7 +2114,7 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations(
var dataOperations = GetDataOperations(forSource: true, changedTableMappings, entriesWithRemovedMappings, diffContext)
.Concat(GetDataOperations(forSource: false, changedTableMappings, entriesWithRemovedMappings, diffContext));

foreach(var operation in dataOperations)
foreach (var operation in dataOperations)
{
yield return operation;
}
Expand Down
30 changes: 23 additions & 7 deletions src/EFCore.Relational/Migrations/MigrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public virtual AlterOperationBuilder<AlterColumnOperation> AlterColumn<T>(
Precision = precision,
Scale = scale,
IsStored = stored,
OldColumn = new ColumnOperation
OldColumn = new AddColumnOperation
{
ClrType = oldClrType ?? typeof(T),
ColumnType = oldType,
Expand Down Expand Up @@ -516,7 +516,7 @@ public virtual AlterOperationBuilder<AlterSequenceOperation> AlterSequence(
MinValue = minValue,
MaxValue = maxValue,
IsCyclic = cyclic,
OldSequence = new SequenceOperation
OldSequence = new CreateSequenceOperation
{
IncrementBy = oldIncrementBy,
MinValue = oldMinValue,
Expand Down Expand Up @@ -550,7 +550,7 @@ public virtual AlterOperationBuilder<AlterTableOperation> AlterTable(
Schema = schema,
Name = name,
Comment = comment,
OldTable = new TableOperation { Comment = oldComment }
OldTable = new CreateTableOperation { Comment = oldComment }
};
Operations.Add(operation);

Expand Down Expand Up @@ -695,22 +695,38 @@ public virtual OperationBuilder<CreateSequenceOperation> CreateSequence<T>(
}

/// <summary>
/// Builds an <see cref="CreateCheckConstraintOperation" /> to create a new check constraint.
/// Builds an <see cref="AddCheckConstraintOperation" /> to create a new check constraint.
/// </summary>
/// <param name="name"> The check constraint name. </param>
/// <param name="table"> The name of the table for the check constraint. </param>
/// <param name="sql"> The constraint sql for the check constraint. </param>
/// <param name="schema"> The schema that contains the check constraint, or <see langword="null" /> to use the default schema. </param>
/// <returns> A builder to allow annotations to be added to the operation. </returns>
public virtual OperationBuilder<CreateCheckConstraintOperation> CreateCheckConstraint(
[Obsolete("Use AddCheckConstraint")]
public virtual OperationBuilder<AddCheckConstraintOperation> CreateCheckConstraint(
[NotNull] string name,
[NotNull] string table,
[NotNull] string sql,
[CanBeNull] string schema = null)
=> AddCheckConstraint(name, table, sql, schema);

/// <summary>
/// Builds an <see cref="AddCheckConstraintOperation" /> to add a new check constraint to a table.
/// </summary>
/// <param name="name"> The check constraint name. </param>
/// <param name="table"> The name of the table for the check constraint. </param>
/// <param name="sql"> The constraint sql for the check constraint. </param>
/// <param name="schema"> The schema that contains the check constraint, or <see langword="null" /> to use the default schema. </param>
/// <returns> A builder to allow annotations to be added to the operation. </returns>
public virtual OperationBuilder<AddCheckConstraintOperation> AddCheckConstraint(
[NotNull] string name,
[NotNull] string table,
[NotNull] string sql,
[CanBeNull] string schema = null)
{
Check.NotEmpty(name, nameof(name));

var operation = new CreateCheckConstraintOperation
var operation = new AddCheckConstraintOperation
{
Schema = schema,
Name = name,
Expand All @@ -719,7 +735,7 @@ public virtual OperationBuilder<CreateCheckConstraintOperation> CreateCheckConst
};
Operations.Add(operation);

return new OperationBuilder<CreateCheckConstraintOperation>(operation);
return new OperationBuilder<AddCheckConstraintOperation>(operation);
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static readonly
{ typeof(AlterDatabaseOperation), (g, o, m, b) => g.Generate((AlterDatabaseOperation)o, m, b) },
{ typeof(AlterSequenceOperation), (g, o, m, b) => g.Generate((AlterSequenceOperation)o, m, b) },
{ typeof(AlterTableOperation), (g, o, m, b) => g.Generate((AlterTableOperation)o, m, b) },
{ typeof(CreateCheckConstraintOperation), (g, o, m, b) => g.Generate((CreateCheckConstraintOperation)o, m, b) },
{ typeof(AddCheckConstraintOperation), (g, o, m, b) => g.Generate((AddCheckConstraintOperation)o, m, b) },
{ typeof(CreateIndexOperation), (g, o, m, b) => g.Generate((CreateIndexOperation)o, m, b) },
{ typeof(CreateSequenceOperation), (g, o, m, b) => g.Generate((CreateSequenceOperation)o, m, b) },
{ typeof(CreateTableOperation), (g, o, m, b) => g.Generate((CreateTableOperation)o, m, b) },
Expand Down Expand Up @@ -277,14 +277,14 @@ protected virtual void Generate(
}

/// <summary>
/// Builds commands for the given <see cref="CreateCheckConstraintOperation" /> by making calls on the given
/// Builds commands for the given <see cref="AddCheckConstraintOperation" /> by making calls on the given
/// <see cref="MigrationCommandListBuilder" />, and then terminates the final command.
/// </summary>
/// <param name="operation"> The operation. </param>
/// <param name="model"> The target model which may be <see langword="null" /> if the operations exist without a model. </param>
/// <param name="builder"> The command builder to use to build the commands. </param>
protected virtual void Generate(
[NotNull] CreateCheckConstraintOperation operation,
[NotNull] AddCheckConstraintOperation operation,
[CanBeNull] IModel model,
[NotNull] MigrationCommandListBuilder builder)
{
Expand Down Expand Up @@ -1710,13 +1710,13 @@ protected virtual void CreateTableCheckConstraints(
}

/// <summary>
/// Generates a SQL fragment for a check constraint of an <see cref="CreateCheckConstraintOperation" />.
/// Generates a SQL fragment for a check constraint of an <see cref="AddCheckConstraintOperation" />.
/// </summary>
/// <param name="operation"> The operation. </param>
/// <param name="model"> The target model which may be <see langword="null" /> if the operations exist without a model. </param>
/// <param name="builder"> The command builder to use to add the SQL fragment. </param>
protected virtual void CheckConstraint(
[NotNull] CreateCheckConstraintOperation operation,
[NotNull] AddCheckConstraintOperation operation,
[CanBeNull] IModel model,
[NotNull] MigrationCommandListBuilder builder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Migrations.Operations
/// A <see cref="MigrationOperation" /> for creating a new check constraint.
/// </summary>
[DebuggerDisplay("ALTER TABLE {Table} ADD CONSTRAINT {Name} CHECK")]
public class CreateCheckConstraintOperation : MigrationOperation
public class AddCheckConstraintOperation : MigrationOperation
{
/// <summary>
/// The name of the check constraint.
Expand Down
15 changes: 0 additions & 15 deletions src/EFCore.Relational/Migrations/Operations/AddColumnOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using JetBrains.Annotations;

namespace Microsoft.EntityFrameworkCore.Migrations.Operations
{
Expand All @@ -12,19 +11,5 @@ namespace Microsoft.EntityFrameworkCore.Migrations.Operations
[DebuggerDisplay("ALTER TABLE {Table} ADD {Name}")]
public class AddColumnOperation : ColumnOperation
{
/// <summary>
/// The column name.
/// </summary>
public virtual string Name { get; [param: NotNull] set; }

/// <summary>
/// The schema that contains the table, or <see langword="null" /> if the default schema should be used.
/// </summary>
public virtual string Schema { get; [param: CanBeNull] set; }

/// <summary>
/// The table to which the column will be added.
/// </summary>
public virtual string Table { get; [param: NotNull] set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,10 @@ namespace Microsoft.EntityFrameworkCore.Migrations.Operations
[DebuggerDisplay("ALTER TABLE {Table} ALTER COLUMN {Name}")]
public class AlterColumnOperation : ColumnOperation, IAlterMigrationOperation
{
/// <summary>
/// The name of the column.
/// </summary>
public virtual string Name { get; [param: NotNull] set; }

/// <summary>
/// The schema that contains the table, or <see langword="null" /> if the default schema should be used.
/// </summary>
public virtual string Schema { get; [param: CanBeNull] set; }

/// <summary>
/// The table which contains the column.
/// </summary>
public virtual string Table { get; [param: NotNull] set; }

/// <summary>
/// An operation representing the column as it was before being altered.
/// </summary>
public virtual ColumnOperation OldColumn { get; [param: NotNull] set; } = new ColumnOperation();
public virtual ColumnOperation OldColumn { get; [param: NotNull] set; } = new AddColumnOperation();

/// <inheritdoc />
IMutableAnnotatable IAlterMigrationOperation.OldAnnotations => OldColumn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public class AlterDatabaseOperation : DatabaseOperation, IAlterMigrationOperatio
/// <summary>
/// An operation representing the database as it was before being altered.
/// </summary>
public virtual DatabaseOperation OldDatabase { get; } = new DatabaseOperation();
public virtual DatabaseOperation OldDatabase { get; } = new CreateDatabaseOperation();

/// <inheritdoc />
IMutableAnnotatable IAlterMigrationOperation.OldAnnotations => OldDatabase;

private sealed class CreateDatabaseOperation : DatabaseOperation
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AlterSequenceOperation : SequenceOperation, IAlterMigrationOperatio
/// <summary>
/// An operation representing the sequence as it was before being altered.
/// </summary>
public virtual SequenceOperation OldSequence { get; [param: NotNull] set; } = new SequenceOperation();
public virtual SequenceOperation OldSequence { get; [param: NotNull] set; } = new CreateSequenceOperation();

/// <inheritdoc />
IMutableAnnotatable IAlterMigrationOperation.OldAnnotations => OldSequence;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,10 @@ namespace Microsoft.EntityFrameworkCore.Migrations.Operations
[DebuggerDisplay("ALTER TABLE {Name}")]
public class AlterTableOperation : TableOperation, IAlterMigrationOperation
{
/// <summary>
/// The name of the table.
/// </summary>
public virtual string Name { get; [param: NotNull] set; }

/// <summary>
/// The schema that contains the table, or <see langword="null" /> if the default schema should be used.
/// </summary>
public virtual string Schema { get; [param: CanBeNull] set; }

/// <summary>
/// An operation representing the table as it was before being altered.
/// </summary>
public virtual TableOperation OldTable { get; [param: NotNull] set; } = new TableOperation();
public virtual TableOperation OldTable { get; [param: NotNull] set; } = new CreateTableOperation();

/// <inheritdoc />
IMutableAnnotatable IAlterMigrationOperation.OldAnnotations => OldTable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ public virtual OperationBuilder<AddUniqueConstraintOperation> UniqueConstraint(
/// <param name="name"> The constraint name. </param>
/// <param name="sql"> The sql expression used in the CHECK constraint. </param>
/// <returns> The same builder so that multiple calls can be chained. </returns>
public virtual OperationBuilder<CreateCheckConstraintOperation> CheckConstraint(
public virtual OperationBuilder<AddCheckConstraintOperation> CheckConstraint(
[NotNull] string name,
[NotNull] string sql)
{
Check.NotEmpty(name, nameof(name));
Check.NotNull(sql, nameof(sql));

var operation = new CreateCheckConstraintOperation
var operation = new AddCheckConstraintOperation
{
Schema = Operation.Schema,
Table = Operation.Name,
Expand All @@ -178,7 +178,7 @@ public virtual OperationBuilder<CreateCheckConstraintOperation> CheckConstraint(
};
Operation.CheckConstraints.Add(operation);

return new OperationBuilder<CreateCheckConstraintOperation>(operation);
return new OperationBuilder<AddCheckConstraintOperation>(operation);
}

/// <summary>
Expand Down
18 changes: 16 additions & 2 deletions src/EFCore.Relational/Migrations/Operations/ColumnOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@

using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Migrations.Operations
{
/// <summary>
/// A <see cref="MigrationOperation" /> for operations on columns.
/// See also <see cref="AddColumnOperation" /> and <see cref="AlterColumnOperation" />.
/// </summary>
public class ColumnOperation : MigrationOperation
public abstract class ColumnOperation : MigrationOperation
{
/// <summary>
/// The name of the column.
/// </summary>
public virtual string Name { get; [param: NotNull] set; }

/// <summary>
/// The schema that contains the table, or <see langword="null" /> if the default schema should be used.
/// </summary>
public virtual string Schema { get; [param: CanBeNull] set; }

/// <summary>
/// The table which contains the column.
/// </summary>
public virtual string Table { get; [param: NotNull] set; }

/// <summary>
/// The CLR <see cref="Type" /> of the property or properties mapped to the column.
/// </summary>
Expand Down
Loading

0 comments on commit 45fe7cd

Please sign in to comment.