Skip to content

Commit

Permalink
Add the column type to the migration when deleting a data using a rem…
Browse files Browse the repository at this point in the history
…oved column

Add fluent overloads to specify column types for Insert/Update/Delete operations

Fixes #21886
Fixes #22302
  • Loading branch information
AndriySvyryd committed Sep 1, 2020
1 parent e0f9cee commit 79d0942
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,24 @@ protected virtual void Generate(

builder.AppendLine(",");

if (operation.KeyColumnTypes != null)
{
if (operation.KeyColumnTypes.Length == 1)
{
builder
.Append("keyColumnType: ")
.Append(Code.Literal(operation.KeyColumnTypes[0]));
}
else
{
builder
.Append("keyColumnTypes: ")
.Append(Code.Literal(operation.KeyColumnTypes));
}

builder.AppendLine(",");
}

if (operation.KeyValues.GetLength(0) == 1
&& operation.KeyValues.GetLength(1) == 1)
{
Expand Down
36 changes: 36 additions & 0 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,8 @@ protected virtual IEnumerable<MigrationOperation> Remove([NotNull] IColumn sourc
};
operation.AddAnnotations(MigrationsAnnotations.ForRemove(source));

diffContext.AddDrop(source, operation);

yield return operation;
}

Expand Down Expand Up @@ -2260,11 +2262,20 @@ private IEnumerable<MigrationOperation> GetDataOperations(
break;
}

var table = command.Entries.First().EntityType.GetTableMappings().Select(m => m.Table)
.First(t => t.Name == command.TableName && t.Schema == command.Schema);
var keyColumns = command.ColumnModifications.Where(col => col.IsKey)
.Select(c => table.FindColumn(c.ColumnName));
var anyKeyColumnDropped = keyColumns.Any(c => diffContext.FindDrop(c) != null);

yield return new DeleteDataOperation
{
Schema = command.Schema,
Table = command.TableName,
KeyColumns = command.ColumnModifications.Where(col => col.IsKey).Select(col => col.ColumnName).ToArray(),
KeyColumnTypes = anyKeyColumnDropped
? keyColumns.Select(col => col.StoreType).ToArray()
: null,
KeyValues = ToMultidimensionalArray(
command.ColumnModifications.Where(col => col.IsKey).Select(GetValue).ToArray()),
IsDestructiveChange = true
Expand Down Expand Up @@ -2565,6 +2576,9 @@ private readonly IDictionary<ITable, CreateTableOperation> _createTableOperation
private readonly IDictionary<ITable, DropTableOperation> _dropTableOperations
= new Dictionary<ITable, DropTableOperation>();

private readonly IDictionary<IColumn, DropColumnOperation> _dropColumnOperations
= new Dictionary<IColumn, DropColumnOperation>();

private readonly IDictionary<DropTableOperation, ITable> _removedTables
= new Dictionary<DropTableOperation, ITable>();

Expand Down Expand Up @@ -2601,6 +2615,17 @@ public virtual void AddDrop([NotNull] ITable source, [NotNull] DropTableOperatio
_removedTables.Add(operation, source);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void AddDrop([NotNull] IColumn source, [NotNull] DropColumnOperation operation)
{
_dropColumnOperations.Add(source, operation);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down Expand Up @@ -2660,6 +2685,17 @@ public virtual DropTableOperation FindDrop([NotNull] ITable source)
? operation
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual DropColumnOperation FindDrop([NotNull] IColumn source)
=> _dropColumnOperations.TryGetValue(source, out var operation)
? operation
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Loading

0 comments on commit 79d0942

Please sign in to comment.