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

Generate ProductVersion in model snapshot again #21360

Merged
merged 1 commit into from
Jun 22, 2020
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
12 changes: 10 additions & 2 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public virtual void Generate(string builderName, IModel model, IndentedStringBui
.FilterIgnoredAnnotations(model.GetAnnotations())
.ToDictionary(a => a.Name, a => a);

if (annotations.Count > 0)
var productVersion = model.GetProductVersion();

if (annotations.Count > 0 || productVersion != null)
{
stringBuilder.Append(builderName);

Expand All @@ -69,7 +71,13 @@ public virtual void Generate(string builderName, IModel model, IndentedStringBui
.Append(Code.Fragment(methodCallCodeFragment));
}

GenerateAnnotations(annotations.Values, stringBuilder);
IEnumerable<IAnnotation> remainingAnnotations = annotations.Values;
if (productVersion != null)
{
remainingAnnotations = remainingAnnotations.Append(
new Annotation(CoreAnnotationNames.ProductVersion, productVersion));
}
GenerateAnnotations(remainingAnnotations, stringBuilder);
}

stringBuilder.AppendLine(";");
Expand Down
82 changes: 52 additions & 30 deletions test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,18 @@ public virtual void CheckConstraint_is_only_stored_in_snapshot_once_for_TPH()
});
}

[ConditionalFact]
public virtual void ProductVersion_is_stored_in_snapshot()
{
var modelBuilder = CreateConventionalModelBuilder();
var generator = CreateMigrationsGenerator();
var code = generator.GenerateSnapshot("RootNamespace", typeof(DbContext), "Snapshot", modelBuilder.Model);
Assert.Contains(@".HasAnnotation(""ProductVersion"",", code);

var modelFromSnapshot = BuildModelFromSnapshotSource(code);
Assert.Equal(ProductInfo.GetVersion(), modelFromSnapshot.GetProductVersion());
}

#endregion

#region EntityType
Expand Down Expand Up @@ -3970,36 +3982,16 @@ protected void Test(IModel model, string expectedCode, Action<IModel> assert)

protected void Test(IModel model, string expectedCode, Action<IModel, IModel> assert)
{
var sqlServerTypeMappingSource = new SqlServerTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
new RelationalTypeMappingSourceDependencies(
new IRelationalTypeMappingSourcePlugin[]
{
new SqlServerNetTopologySuiteTypeMappingSourcePlugin(NtsGeometryServices.Instance)
}));

var codeHelper = new CSharpHelper(
sqlServerTypeMappingSource);

var sqlServerAnnotationCodeGenerator = new SqlServerAnnotationCodeGenerator(
new AnnotationCodeGeneratorDependencies(sqlServerTypeMappingSource));

var generator = new CSharpMigrationsGenerator(
new MigrationsCodeGeneratorDependencies(
sqlServerTypeMappingSource,
sqlServerAnnotationCodeGenerator),
new CSharpMigrationsGeneratorDependencies(
codeHelper,
new CSharpMigrationOperationGenerator(
new CSharpMigrationOperationGeneratorDependencies(
codeHelper)),
new CSharpSnapshotGenerator(
new CSharpSnapshotGeneratorDependencies(
codeHelper, sqlServerTypeMappingSource, sqlServerAnnotationCodeGenerator))));

var generator = CreateMigrationsGenerator();
var code = generator.GenerateSnapshot("RootNamespace", typeof(DbContext), "Snapshot", model);
Assert.Equal(expectedCode, code, ignoreLineEndingDifferences: true);

var modelFromSnapshot = BuildModelFromSnapshotSource(code);
assert(modelFromSnapshot, model);
}

protected IModel BuildModelFromSnapshotSource(string code)
{
var build = new BuildSource { Sources = { code } };

foreach (var buildReference in GetReferences())
Expand Down Expand Up @@ -4027,9 +4019,7 @@ protected void Test(IModel model, string expectedCode, Action<IModel, IModel> as
var services = RelationalTestHelpers.Instance.CreateContextServices();

var processor = new SnapshotModelProcessor(new TestOperationReporter(), services.GetService<IConventionSetBuilder>());
var modelFromSnapshot = processor.Process(builder.Model);

assert(modelFromSnapshot, model);
return processor.Process(builder.Model);
}

protected ModelBuilder CreateConventionalModelBuilder()
Expand All @@ -4047,5 +4037,37 @@ protected ModelBuilder CreateConventionalModelBuilder()
using var context = serviceScope.ServiceProvider.GetService<DbContext>();
return new ModelBuilder(ConventionSet.CreateConventionSet(context));
}

protected CSharpMigrationsGenerator CreateMigrationsGenerator()
{
var sqlServerTypeMappingSource = new SqlServerTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
new RelationalTypeMappingSourceDependencies(
new IRelationalTypeMappingSourcePlugin[]
{
new SqlServerNetTopologySuiteTypeMappingSourcePlugin(NtsGeometryServices.Instance)
}));

var codeHelper = new CSharpHelper(
sqlServerTypeMappingSource);

var sqlServerAnnotationCodeGenerator = new SqlServerAnnotationCodeGenerator(
new AnnotationCodeGeneratorDependencies(sqlServerTypeMappingSource));

var generator = new CSharpMigrationsGenerator(
new MigrationsCodeGeneratorDependencies(
sqlServerTypeMappingSource,
sqlServerAnnotationCodeGenerator),
new CSharpMigrationsGeneratorDependencies(
codeHelper,
new CSharpMigrationOperationGenerator(
new CSharpMigrationOperationGeneratorDependencies(
codeHelper)),
new CSharpSnapshotGenerator(
new CSharpSnapshotGeneratorDependencies(
codeHelper, sqlServerTypeMappingSource, sqlServerAnnotationCodeGenerator))));

return generator;
}
}
}