Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Migration Batching When Seed Data Has Non-Writable Columns (I.E. RowVersion) #18320

Merged
merged 3 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1965,10 +1965,12 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations([NotNull] Di
{
if (batchInsertOperation.Table == c.TableName
&& batchInsertOperation.Schema == c.Schema
&& batchInsertOperation.Columns.SequenceEqual(c.ColumnModifications.Select(col => col.ColumnName)))
&& batchInsertOperation.Columns.SequenceEqual(
c.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(col => col.ColumnName)))
{
batchInsertOperation.Values =
AddToMultidimensionalArray(c.ColumnModifications.Select(GetValue).ToList(), batchInsertOperation.Values);
AddToMultidimensionalArray(
c.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(GetValue).ToList(), batchInsertOperation.Values);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
Expand Down Expand Up @@ -1374,6 +1375,92 @@ public void Add_column_with_seed_data()
}));
}

[ConditionalFact]
public void Add_seed_data_with_non_writable_column_insert_operations_with_batching()
{
Execute(
_ => { },
source => source.Entity(
"Firefly",
x =>
{
x.ToTable("Firefly", "dbo");
x.Property<int>("Id");
x.Property<string>("Name").HasColumnType("nvarchar(30)");
x.Property<byte[]>("Version").IsRowVersion();
}),
target => target.Entity(
"Firefly",
x =>
{
x.ToTable("Firefly", "dbo");
x.Property<int>("Id");
x.Property<string>("Name").HasColumnType("nvarchar(30)");
x.Property<byte[]>("Version").IsRowVersion();
x.HasData(
new { Id = 42, Name = "Firefly 1" },
new { Id = 43, Name = "Firefly 2" },
new { Id = 44, Name = "Firefly 3" },
new { Id = 45, Name = "Firefly 4" });
}),
upOps => Assert.Collection(
upOps,
o =>
{
var m = Assert.IsType<InsertDataOperation>(o);
Assert.Collection(
ToJaggedArray(m.Values),
r => Assert.Collection(
r,
v => Assert.Equal(42, v),
v => Assert.Equal("Firefly 1", v)),
r => Assert.Collection(
r,
v => Assert.Equal(43, v),
v => Assert.Equal("Firefly 2", v)),
r => Assert.Collection(
r,
v => Assert.Equal(44, v),
v => Assert.Equal("Firefly 3", v)),
r => Assert.Collection(
r,
v => Assert.Equal(45, v),
v => Assert.Equal("Firefly 4", v))
);
}),
downOps => Assert.Collection(
downOps,
o =>
{
var m = Assert.IsType<DeleteDataOperation>(o);
AssertMultidimensionalArray(
m.KeyValues,
v => Assert.Equal(42, v));
},
o =>
{
var m = Assert.IsType<DeleteDataOperation>(o);
AssertMultidimensionalArray(
m.KeyValues,
v => Assert.Equal(43, v));
},
o =>
{
var m = Assert.IsType<DeleteDataOperation>(o);
AssertMultidimensionalArray(
m.KeyValues,
v => Assert.Equal(44, v));
},
o =>
{
var m = Assert.IsType<DeleteDataOperation>(o);
AssertMultidimensionalArray(
m.KeyValues,
v => Assert.Equal(45, v));
}),
builderOptions => builderOptions.UseFakeRelational(a => a.MaxBatchSize(4)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need this option? It should work without it.

Copy link
Contributor Author

@crowet crowet Oct 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default batch size in the Relational abstract class is 1. This fails if the batch size setting is not set. The test requires all 4 inserts included in the same batch, but when that option is removed, it becomes 4 operations.

image

}

private enum SomeEnum
{
Default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider;
using Microsoft.EntityFrameworkCore.Update;
using Microsoft.EntityFrameworkCore.Update.Internal;
using Xunit;
Expand Down Expand Up @@ -37,25 +38,48 @@ protected void Execute(
Action<ModelBuilder> buildTargetAction,
Action<IReadOnlyList<MigrationOperation>> assertActionUp,
Action<IReadOnlyList<MigrationOperation>> assertActionDown)
=> Execute(buildCommonAction, buildSourceAction, buildTargetAction, assertActionUp, assertActionDown, null);

protected void Execute(
Action<ModelBuilder> buildCommonAction,
Action<ModelBuilder> buildSourceAction,
Action<ModelBuilder> buildTargetAction,
Action<IReadOnlyList<MigrationOperation>> assertActionUp,
Action<IReadOnlyList<MigrationOperation>> assertActionDown,
Action<DbContextOptionsBuilder> builderOptionsAction)
{
var sourceModelBuilder = CreateModelBuilder();
buildCommonAction(sourceModelBuilder);
buildSourceAction(sourceModelBuilder);
sourceModelBuilder.FinalizeModel();
var sourceOptionsBuilder = TestHelpers
.AddProviderOptions(new DbContextOptionsBuilder())
.UseModel(sourceModelBuilder.Model)
.EnableSensitiveDataLogging();

var targetModelBuilder = CreateModelBuilder();
buildCommonAction(targetModelBuilder);
buildTargetAction(targetModelBuilder);
targetModelBuilder.FinalizeModel();
var targetOptionsBuilder = TestHelpers
.AddProviderOptions(new DbContextOptionsBuilder())
.UseModel(targetModelBuilder.Model)
.EnableSensitiveDataLogging();

if (builderOptionsAction != null)
{
builderOptionsAction(sourceOptionsBuilder);
builderOptionsAction(targetOptionsBuilder);
}

var modelDiffer = CreateModelDiffer(targetModelBuilder.Model);
var modelDiffer = CreateModelDiffer(targetOptionsBuilder.Options);

var operationsUp = modelDiffer.GetDifferences(sourceModelBuilder.Model, targetModelBuilder.Model);
assertActionUp(operationsUp);

if (assertActionDown != null)
{
modelDiffer = CreateModelDiffer(sourceModelBuilder.Model);
modelDiffer = CreateModelDiffer(sourceOptionsBuilder.Options);

var operationsDown = modelDiffer.GetDifferences(targetModelBuilder.Model, sourceModelBuilder.Model);
assertActionDown(operationsDown);
Expand Down Expand Up @@ -109,11 +133,9 @@ protected static T[][] ToJaggedArray<T>(T[,] twoDimensionalArray, bool firstDime
protected abstract TestHelpers TestHelpers { get; }
protected virtual ModelBuilder CreateModelBuilder() => TestHelpers.CreateConventionBuilder(skipValidation: true);

protected virtual MigrationsModelDiffer CreateModelDiffer(IModel model)
protected virtual MigrationsModelDiffer CreateModelDiffer(DbContextOptions options)
{
var ctx = TestHelpers.CreateContext(
TestHelpers.AddProviderOptions(new DbContextOptionsBuilder())
.UseModel(model).EnableSensitiveDataLogging().Options);
var ctx = TestHelpers.CreateContext(options);
return new MigrationsModelDiffer(
new TestRelationalTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider
{
public class FakeRelationalDbContextOptionsBuilder
: RelationalDbContextOptionsBuilder<FakeRelationalDbContextOptionsBuilder, FakeRelationalOptionsExtension>
{
public FakeRelationalDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
: base(optionsBuilder)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider
{
public static class FakeRelationalDbContextOptionsExtension
{
public static DbContextOptionsBuilder UseFakeRelational(
this DbContextOptionsBuilder optionsBuilder,
Action<FakeRelationalDbContextOptionsBuilder> fakeRelationalOptionsAction = null)
{
return optionsBuilder.UseFakeRelational("Database=Fake", fakeRelationalOptionsAction);
}

public static DbContextOptionsBuilder UseFakeRelational(
this DbContextOptionsBuilder optionsBuilder,
string connectionString,
Action<FakeRelationalDbContextOptionsBuilder> fakeRelationalOptionsAction = null)
{
return optionsBuilder.UseFakeRelational(new FakeDbConnection(connectionString), fakeRelationalOptionsAction);
}

public static DbContextOptionsBuilder UseFakeRelational(
this DbContextOptionsBuilder optionsBuilder,
DbConnection connection,
Action<FakeRelationalDbContextOptionsBuilder> fakeRelationalOptionsAction = null)
{
var extension = (FakeRelationalOptionsExtension)GetOrCreateExtension(optionsBuilder).WithConnection(connection);
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

fakeRelationalOptionsAction?.Invoke(new FakeRelationalDbContextOptionsBuilder(optionsBuilder));

return optionsBuilder;
}

private static FakeRelationalOptionsExtension GetOrCreateExtension(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.Options.FindExtension<FakeRelationalOptionsExtension>()
?? new FakeRelationalOptionsExtension();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ public override IServiceCollection AddProviderServices(IServiceCollection servic

protected override void UseProviderOptions(DbContextOptionsBuilder optionsBuilder)
{
var extension = optionsBuilder.Options.FindExtension<FakeRelationalOptionsExtension>()
?? new FakeRelationalOptionsExtension();

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(
extension.WithConnection(new FakeDbConnection("Database=Fake")));
optionsBuilder.UseFakeRelational();
}

public override LoggingDefinitions LoggingDefinitions { get; } = new TestRelationalLoggingDefinitions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.EntityFrameworkCore.Update;

namespace Microsoft.EntityFrameworkCore.TestUtilities
{
public class TestModificationCommandBatch : SingularModificationCommandBatch
{
private readonly int _maxBatchSize;

public TestModificationCommandBatch(
ModificationCommandBatchFactoryDependencies dependencies,
int? maxBatchSize)
: base(dependencies)
{
_maxBatchSize = maxBatchSize ?? 1;
}

protected override bool CanAddCommand(ModificationCommand modificationCommand)
{
return ModificationCommands.Count < _maxBatchSize;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider;
using Microsoft.EntityFrameworkCore.Update;

namespace Microsoft.EntityFrameworkCore.TestUtilities
{
public class TestModificationCommandBatchFactory : IModificationCommandBatchFactory
{
private readonly ModificationCommandBatchFactoryDependencies _dependencies;
private readonly IDbContextOptions _options;

public TestModificationCommandBatchFactory(
ModificationCommandBatchFactoryDependencies dependencies)
ModificationCommandBatchFactoryDependencies dependencies,
IDbContextOptions options)
{
_dependencies = dependencies;
_options = options;
}

public int CreateCount { get; private set; }
Expand All @@ -21,7 +27,9 @@ public virtual ModificationCommandBatch Create()
{
CreateCount++;

return new SingularModificationCommandBatch(_dependencies);
var optionsExtension = _options.Extensions.OfType<FakeRelationalOptionsExtension>().FirstOrDefault();

return new TestModificationCommandBatch(_dependencies, optionsExtension?.MaxBatchSize);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -780,11 +780,9 @@ public void Rebuild_index_when_changing_online_option()

protected override TestHelpers TestHelpers => SqlServerTestHelpers.Instance;

protected override MigrationsModelDiffer CreateModelDiffer(IModel model)
protected override MigrationsModelDiffer CreateModelDiffer(DbContextOptions options)
{
var ctx = TestHelpers.CreateContext(
TestHelpers.AddProviderOptions(new DbContextOptionsBuilder())
.UseModel(model).EnableSensitiveDataLogging().Options);
var ctx = TestHelpers.CreateContext(options);
return new MigrationsModelDiffer(
new SqlServerTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
Expand Down