Skip to content

Commit

Permalink
Revert SnapshotModelProcessor dependency change.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Feb 20, 2020
1 parent 9cce8fc commit 5e9d751
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 46 deletions.
11 changes: 2 additions & 9 deletions src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace Microsoft.EntityFrameworkCore.Migrations.Internal
public class SnapshotModelProcessor : ISnapshotModelProcessor
{
private readonly IOperationReporter _operationReporter;
private readonly ProviderConventionSetBuilderDependencies _conventionDependencies;
private readonly HashSet<string> _relationalNames;

/// <summary>
Expand All @@ -35,11 +34,9 @@ public class SnapshotModelProcessor : ISnapshotModelProcessor
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SnapshotModelProcessor(
[NotNull] IOperationReporter operationReporter,
[NotNull] ProviderConventionSetBuilderDependencies conventionDependencies)
[NotNull] IOperationReporter operationReporter)
{
_operationReporter = operationReporter;
_conventionDependencies = conventionDependencies;
_relationalNames = new HashSet<string>(
typeof(RelationalAnnotationNames)
.GetRuntimeFields()
Expand Down Expand Up @@ -81,12 +78,8 @@ public virtual IModel Process(IModel model)
}
}

if (model is IConventionModel conventionModel
&& _conventionDependencies != null)
if (model is IConventionModel conventionModel)
{
var typeMappingConvention = new TypeMappingConvention(_conventionDependencies);
typeMappingConvention.ProcessModelFinalizing(conventionModel.Builder, null);

model = new RelationalModelConvention().ProcessModelFinalized(conventionModel);
}

Expand Down
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.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down
12 changes: 11 additions & 1 deletion src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static string GetDefaultColumnType(IProperty property)
var sharedTablePrincipalPrimaryKeyProperty = property.FindSharedTableRootPrimaryKeyProperty();
return sharedTablePrincipalPrimaryKeyProperty != null
? sharedTablePrincipalPrimaryKeyProperty.GetColumnType()
: property.FindRelationalMapping()?.StoreType;
: property.FindRelationalTypeMapping()?.StoreType;
}

/// <summary>
Expand Down Expand Up @@ -427,7 +427,17 @@ public static RelationalTypeMapping GetRelationalTypeMapping([NotNull] this IPro
/// <param name="property"> The property. </param>
/// <returns> The type mapping, or null if none was found. </returns>
[DebuggerStepThrough]
[Obsolete("Use FindRelationalTypeMapping")]
public static RelationalTypeMapping FindRelationalMapping([NotNull] this IProperty property)
=> property.FindRelationalTypeMapping();

/// <summary>
/// Returns the <see cref="RelationalTypeMapping" /> for the given property on a finalized model.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The type mapping, or null if none was found. </returns>
[DebuggerStepThrough]
public static RelationalTypeMapping FindRelationalTypeMapping([NotNull] this IProperty property)
=> (RelationalTypeMapping)property.FindTypeMapping();

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Internal/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Column : Annotatable, IColumn
/// 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 Column([NotNull] string name, [NotNull] string type, [NotNull] Table table)
public Column([NotNull] string name, [CanBeNull] string type, [NotNull] Table table)
{
Name = name;
Type = type;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Internal/ColumnMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ColumnMapping : Annotatable, IColumnMapping
public ColumnMapping(
[NotNull] IProperty property,
[NotNull] Column column,
[NotNull] RelationalTypeMapping typeMapping,
[CanBeNull] RelationalTypeMapping typeMapping,
[NotNull] TableMapping tableMapping)
{
Property = property;
Expand Down
26 changes: 13 additions & 13 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public static IModel AddRelationalModel([NotNull] IConventionModel model)
var tableMapping = new TableMapping(entityType, table, includesDerivedTypes: true);
foreach (var property in entityType.GetDeclaredProperties())
{
var typeMapping = property.GetRelationalTypeMapping();
var typeMapping = property.FindRelationalTypeMapping();
var columnName = property.GetColumnName();
var column = (Column)table.FindColumn(columnName);
if (column == null)
{
column = new Column(columnName, property.GetColumnType() ?? typeMapping.StoreType, table);
column = new Column(columnName, property.GetColumnType() ?? typeMapping?.StoreType, table);
column.IsNullable = property.IsColumnNullable();
table.Columns.Add(columnName, column);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public static IModel AddRelationalModel([NotNull] IConventionModel model)
var viewMapping = new ViewMapping(entityType, view, includesDerivedTypes: true);
foreach (var property in entityType.GetDeclaredProperties())
{
var typeMapping = property.GetRelationalTypeMapping();
var typeMapping = property.FindRelationalTypeMapping();
var columnName = property.GetColumnName();
var column = (ViewColumn)view.FindColumn(columnName);
if (column == null)
Expand Down Expand Up @@ -136,23 +136,23 @@ public static IModel AddRelationalModel([NotNull] IConventionModel model)
}
}

foreach (var table in tables.Values)
{
PopulateInternalForeignKeys(table);
}

foreach (var view in views.Values)
{
PopulateInternalForeignKeys(view);
}

if (tables.Any())
{
foreach (var table in tables.Values)
{
PopulateInternalForeignKeys(table);
}

model.SetAnnotation(RelationalAnnotationNames.Tables, tables);
}

if (views.Any())
{
foreach (var view in views.Values)
{
PopulateInternalForeignKeys(view);
}

model.SetAnnotation(RelationalAnnotationNames.Views, views);
}

Expand Down
7 changes: 4 additions & 3 deletions src/EFCore.Relational/Metadata/Internal/TableBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -53,13 +54,13 @@ public TableBase([NotNull] string name, [CanBeNull] string schema)
public virtual SortedDictionary<IEntityType, IEnumerable<IForeignKey>> ReferencingInternalForeignKeys { get; [param: NotNull] set; }

/// <inheritdoc/>
IEnumerable<ITableMappingBase> ITableBase.EntityTypeMappings => throw new System.NotImplementedException();
IEnumerable<ITableMappingBase> ITableBase.EntityTypeMappings => throw new NotImplementedException();

/// <inheritdoc/>
IEnumerable<IColumnBase> ITableBase.Columns => throw new System.NotImplementedException();
IEnumerable<IColumnBase> ITableBase.Columns => throw new NotImplementedException();

/// <inheritdoc/>
IColumnBase ITableBase.FindColumn(string name) => throw new System.NotImplementedException();
IColumnBase ITableBase.FindColumn(string name) => throw new NotImplementedException();

/// <inheritdoc/>
IEnumerable<IForeignKey> ITableBase.GetInternalForeignKeys(IEntityType entityType)
Expand Down
16 changes: 12 additions & 4 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,11 +953,19 @@ protected virtual IEnumerable<MigrationOperation> Diff(
};
}

var sourceTypeMapping = sourceMapping.TypeMapping ?? TypeMappingSource.GetMapping(source);
var targetTypeMapping = targetMapping.TypeMapping ?? TypeMappingSource.GetMapping(target);

var sourceColumnType = sourceColumn.Type
?? sourceTypeMapping.StoreType;
var targetColumnType = targetColumn.Type
?? targetTypeMapping.StoreType;

var sourceMigrationsAnnotations = MigrationsAnnotations.For(source).ToList();
var targetMigrationsAnnotations = MigrationsAnnotations.For(target).ToList();

var isNullableChanged = sourceColumn.IsNullable != targetColumn.IsNullable;
var columnTypeChanged = sourceColumn.Type != targetColumn.Type;
var columnTypeChanged = sourceColumnType != targetColumnType;

if (isNullableChanged
|| columnTypeChanged
Expand All @@ -980,11 +988,11 @@ protected virtual IEnumerable<MigrationOperation> Diff(
};

Initialize(
alterColumnOperation, target, targetMapping.TypeMapping,
alterColumnOperation, target, targetTypeMapping,
targetColumn.IsNullable, targetMigrationsAnnotations, inline: true);

Initialize(
alterColumnOperation.OldColumn, source, sourceMapping.TypeMapping,
alterColumnOperation.OldColumn, source, sourceTypeMapping,
sourceColumn.IsNullable, sourceMigrationsAnnotations, inline: true);

yield return alterColumnOperation;
Expand Down Expand Up @@ -2200,7 +2208,7 @@ private object GetDefaultValue(IProperty property)
}

private ValueConverter GetValueConverter(IProperty property)
=> property.GetValueConverter() ?? property.GetRelationalTypeMapping().Converter;
=> property.GetValueConverter() ?? property.FindRelationalTypeMapping()?.Converter;

private static IEntityType GetRootType(ITable table)
=> table.EntityTypeMappings.Select(m => m.EntityType).FirstOrDefault(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private RelationalTypeMapping FindMappingWithConversion(
/// <returns> The type mapping, or <c>null</c> if none was found. </returns>
public override CoreTypeMapping FindMapping(IProperty property)
{
var mapping = property.FindRelationalMapping();
var mapping = property.FindRelationalTypeMapping();
if (mapping != null)
{
return mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ var migrationAssembly
historyRepository,
reporter,
new MockProvider(),
new SnapshotModelProcessor(reporter, services.GetRequiredService<ProviderConventionSetBuilderDependencies>()),
new SnapshotModelProcessor(reporter),
new Migrator(
migrationAssembly,
historyRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Updates_provider_annotations_on_model()

var reporter = new TestOperationReporter();

new SnapshotModelProcessor(reporter, null).Process(model);
new SnapshotModelProcessor(reporter).Process(model);

AssertAnnotations(model);
AssertAnnotations(entityType);
Expand All @@ -78,7 +78,7 @@ public void Warns_for_conflicting_annotations()

var reporter = new TestOperationReporter();

new SnapshotModelProcessor(reporter, null).Process(model);
new SnapshotModelProcessor(reporter).Process(model);

Assert.Equal("warn: " + DesignStrings.MultipleAnnotationConflict("DefaultSchema"), reporter.Messages.Single());
Assert.Equal(2, model.GetAnnotations().Count());
Expand All @@ -99,7 +99,7 @@ public void Warns_for_conflicting_annotations_one_relational()

var reporter = new TestOperationReporter();

new SnapshotModelProcessor(reporter, null).Process(model);
new SnapshotModelProcessor(reporter).Process(model);

Assert.Equal("warn: " + DesignStrings.MultipleAnnotationConflict("DefaultSchema"), reporter.Messages.Single());
Assert.Equal(2, model.GetAnnotations().Count());
Expand All @@ -120,7 +120,7 @@ public void Does_not_warn_for_duplicate_non_conflicting_annotations()

var reporter = new TestOperationReporter();

new SnapshotModelProcessor(reporter, null).Process(model);
new SnapshotModelProcessor(reporter).Process(model);

Assert.Empty(reporter.Messages);

Expand All @@ -139,7 +139,7 @@ public void Does_not_process_non_v1_models()

var reporter = new TestOperationReporter();

new SnapshotModelProcessor(reporter, null).Process(model);
new SnapshotModelProcessor(reporter).Process(model);

Assert.Empty(reporter.Messages);

Expand All @@ -165,7 +165,7 @@ public void Sets_owned_type_keys()
});

var reporter = new TestOperationReporter();
new SnapshotModelProcessor(reporter, null).Process(model);
new SnapshotModelProcessor(reporter).Process(model);

Assert.Empty(reporter.Messages);
Assert.Equal(
Expand All @@ -185,7 +185,7 @@ public void Can_diff_against_older_ownership_model(Type snapshotType)
var differ = context.GetService<IMigrationsModelDiffer>();
var snapshot = (ModelSnapshot)Activator.CreateInstance(snapshotType);
var reporter = new TestOperationReporter();
var processor = new SnapshotModelProcessor(reporter, context.GetService<ProviderConventionSetBuilderDependencies>());
var processor = new SnapshotModelProcessor(reporter);

var differences = differ.GetDifferences(processor.Process(snapshot.Model), context.Model);

Expand All @@ -195,6 +195,7 @@ public void Can_diff_against_older_ownership_model(Type snapshotType)
private void AddAnnotations(IMutableAnnotatable element)
{
foreach (var annotationName in GetAnnotationNames()
.Where(a => a != RelationalAnnotationNames.MaxIdentifierLength)
.Select(a => "Unicorn" + a.Substring(RelationalAnnotationNames.Prefix.Length - 1)))
{
element[annotationName] = "Value";
Expand All @@ -203,7 +204,14 @@ private void AddAnnotations(IMutableAnnotatable element)

private void AssertAnnotations(IMutableAnnotatable element)
{
foreach (var annotationName in GetAnnotationNames())
foreach (var annotationName in GetAnnotationNames()
.Where(a => a != RelationalAnnotationNames.MaxIdentifierLength
&& a != RelationalAnnotationNames.Tables
&& a != RelationalAnnotationNames.TableMappings
&& a != RelationalAnnotationNames.TableColumnMappings
&& a != RelationalAnnotationNames.Views
&& a != RelationalAnnotationNames.ViewMappings
&& a != RelationalAnnotationNames.ViewColumnMappings))
{
Assert.Equal("Value", (string)element[annotationName]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,7 @@ public virtual void Property_multiple_annotations_are_stored_in_snapshot()
o =>
{
var property = o.GetEntityTypes().First().FindProperty("AlternateId");
Assert.Equal(5, property.GetAnnotations().Count());
Assert.Equal(4, property.GetAnnotations().Count());
Assert.Equal("AnnotationValue", property["AnnotationName"]);
Assert.Equal("CName", property["Relational:ColumnName"]);
Assert.Equal("int", property["Relational:ColumnType"]);
Expand Down Expand Up @@ -3752,8 +3752,7 @@ protected void Test(IModel model, string expectedCode, Action<IModel, IModel> as

var services = RelationalTestHelpers.Instance.CreateContextServices();

var processor = new SnapshotModelProcessor(new TestOperationReporter(),
services.GetRequiredService<ProviderConventionSetBuilderDependencies>().With(sqlServerTypeMappingSource));
var processor = new SnapshotModelProcessor(new TestOperationReporter());
var modelFromSnapshot = processor.Process(builder.Model);

assert(modelFromSnapshot, model);
Expand Down

0 comments on commit 5e9d751

Please sign in to comment.