Skip to content

Upgrade process fails on nested fields when secondary Key attribute is removed without removing the field #343

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

Open
BrendenAZ opened this issue Sep 8, 2023 · 0 comments

Comments

@BrendenAZ
Copy link

Problem:
The upgrade process fails when removing a Key attribute without removing the field itself. The exception is thrown in UpgradeHintsProcessor while processing nested fields.

Error:
The inner loop for nested fields is returning an error on Single because the SecondaryId field existed in the fieldMapping, but not in the nested fields of the self referencing MergedTo field where the SecondaryId was already removed.

Proposed solution:
Before checking if we need to map recursively, I think we should perform a null check to see if the nested field still exists by changing Single to SingleOrDefault.

var newNestedField = newField.Fields
            .Single(field => field.OriginalName.Equals(newNestedFieldOrigin.Name, StringComparison.Ordinal));

Before state: Model with two key fields and a self referencing field that tracks both keys.

  [HierarchyRoot(InheritanceSchema.ConcreteTable)]
  public sealed class MultiKeyEntity: Entity
  {
    [Field, Key(0)]
    public long Id { get; private set; }

    [Field, Key(1)]
    public long SecondaryId { get; private set; }

    [Field(Nullable = true)]
    public MultiKeyEntity? MergedTo { get; set; }
  }

After state: Model with one key field (second field kept, but key attribute removed) and a self referencing field that tracks remaining key.

  [HierarchyRoot(InheritanceSchema.ConcreteTable)]
  public sealed class MultiKeyEntity: Entity
  {
    [Field, Key]
    public long Id { get; private set; }

    [Field]
    public long SecondaryId { get; private set; }

    [Field(Nullable = true)]
    public MultiKeyEntity? MergedTo { get; set; }
  }

Code block:

private void MapNestedFields(StoredFieldInfo oldField, StoredFieldInfo newField)
    {
      var oldNestedFields = oldField.Fields;
      if (oldNestedFields.Length == 0) {
        return;
      }

      var oldValueType = extractedModel.Types
        .Single(type => type.UnderlyingType.Equals(oldField.ValueType, StringComparison.Ordinal));
      foreach (var oldNestedField in oldNestedFields) {
        var oldNestedFieldOriginalName = oldNestedField.OriginalName;
        var oldNestedFieldOrigin = oldValueType.AllFields
          .Single(field => field.Name.Equals(oldNestedField.OriginalName, StringComparison.Ordinal));

        if (fieldMapping.TryGetValue(oldNestedFieldOrigin, out var newNestedFieldOrigin)) {
          var newNestedField = newField.Fields
            .Single(field => field.OriginalName.Equals(newNestedFieldOrigin.Name, StringComparison.Ordinal));
          MapFieldRecursively(oldNestedField, newNestedField);
        }
      }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant