Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
Fixup for bbd8f88 #2334 (#2336)
Browse files Browse the repository at this point in the history
Previously the [deployment failed](#2334 (comment)):

```
failed to overwrite default of old field type with None (string):
failed to set default value of enum type to None (string):
value: None (string) is not part of allowed enum values:
[Done Duplicate Incomplete Description Can not Reproduce Deferred Won't Fix Out of Date Verified]
file: spacetemplate/importer/repository.go
line: 91
```

We've updated the resolution enum to
have a new value and that is also the new default. That new value didn't
exist in the old enum type but we tried to make it the new default for
the old type anyways. That didn't work because the `FieldType.SetDefault()`
implementation for enums checks if the given value is part of the
allowed enum values.

The overall intention is to check if too enums are the same but ignore
the default value. That is why we temporarily make both defaults the
same before we call `FieldType.Equal()`.

With this change we simply reverse the assignment of the new default to
the old type. Instead we temporarily assign the old default to the new
type. The result is that a call to `FieldType.Equal()` will return true.
  • Loading branch information
kwk authored Oct 30, 2018
1 parent bbd8f88 commit d070588
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
15 changes: 8 additions & 7 deletions spacetemplate/importer/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,20 @@ func (r *GormRepository) createOrUpdateWITs(ctx context.Context, s *ImportHelper

// When comparing the new and old field types we don't want
// to compare the default value. That is why we always
// overwrite the default value of the old type with the
// default value of the new type.
// overwrite the default value of the new type with the
// default value of the old type.

defVal := fd.Type.GetDefaultValue()
oldFieldType, err = oldFieldType.SetDefaultValue(defVal)
// remember new default value
oldDefVal := oldFieldType.GetDefaultValue()
newFieldType, err := fd.Type.SetDefaultValue(oldDefVal)
if err != nil {
return errs.Wrapf(err, "failed to overwrite default of old field type with %+v (%[1]T)", defVal)
return errs.Wrapf(err, "failed to temporarily overwrite default of new field type with %+v (%[1]T)", oldDefVal)
}

if equal := fd.Type.Equal(oldFieldType); !equal {
if equal := newFieldType.Equal(oldFieldType); !equal {
// Special treatment for EnumType
origEnum, ok1 := oldFieldType.(workitem.EnumType)
newEnum, ok2 := fd.Type.(workitem.EnumType)
newEnum, ok2 := newFieldType.(workitem.EnumType)
if ok1 && ok2 {
equal = newEnum.EqualEnclosing(origEnum)
}
Expand Down
39 changes: 39 additions & 0 deletions spacetemplate/importer/repository_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,45 @@ func (s *repoSuite) TestImport() {
require.Error(t, err)
require.Contains(t, err.Error(), "you must not remove these fields from the new work item type definition of")
})
t.Run("import existing template with new state enum value in first position (default)", func(t *testing.T) {
// Create fresh template
spaceTemplateID := uuid.NewV4()
witID := uuid.NewV4()
wiltID := uuid.NewV4()
witgID := uuid.NewV4()
wibID := uuid.NewV4()
oldTempl := getValidTestTemplateParsed(t, spaceTemplateID, witID, wiltID, witgID, wibID)
oldTempl.Template.Name = "old name for space template " + spaceTemplateID.String()
_, err := s.importerRepo.Import(s.Ctx, oldTempl)
require.NoError(t, err)

// Import it once more but this time have a new default value that
// doesn't exist in before's state.
templ := getValidTestTemplateParsed(t, spaceTemplateID, witID, wiltID, witgID, wibID)
templ.Template.Name = oldTempl.Template.Name

stateField, ok := templ.WITs[0].Fields["state"]
require.True(t, ok, "failed to find 'state' field in %+v", templ.WITs[0].Fields)
enumType, ok := stateField.Type.(workitem.EnumType)
require.True(t, ok, "failed to convert 'state' field to enum type: %+v", stateField.Type)
newDefault := uuid.NewV4().String()
newAllowedValues := append(enumType.Values, newDefault)
enumType.Values = newAllowedValues
fieldType, err := enumType.SetDefaultValue(newDefault)
require.NoError(t, err)
stateField.Type = fieldType
templ.WITs[0].Fields["state"] = stateField

// when
newTempl, err := s.importerRepo.Import(s.Ctx, templ)
// then
require.NoError(t, err)
newStateField := newTempl.WITs[0].Fields["state"]
newEnumType, ok := stateField.Type.(workitem.EnumType)
require.True(t, ok, "failed to convert 'state' field to enum type: %+v",
newStateField)
require.Equal(t, newAllowedValues, newEnumType.Values)
})
})
}

Expand Down

0 comments on commit d070588

Please sign in to comment.