Prevent module field name & type changes

This commit is contained in:
Denis Arh
2019-05-16 11:00:21 +02:00
parent 83e95a870c
commit 55a0e613a6
2 changed files with 8 additions and 4 deletions
+6 -2
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/titpetric/factory"
@@ -159,6 +158,12 @@ func (r module) updateFields(moduleID uint64, ff types.ModuleFieldSet) error {
if e := existing.FindByID(f.ID); e != nil {
f.CreatedAt = e.CreatedAt
f.UpdatedAt = &now
// We do not have any other code in place that would handle changes of field name and kind, so we need
// to reset any changes made to the field.
// @todo remove when we are able to handle field rename & type change
f.Name = e.Name
f.Kind = e.Kind
} else {
f.ID = 0
}
@@ -172,7 +177,6 @@ func (r module) updateFields(moduleID uint64, ff types.ModuleFieldSet) error {
f.ModuleID = moduleID
f.Place = idx
f.DeletedAt = nil
spew.Dump(f)
if err := r.db().Replace(r.tableFields(), f); err != nil {
return errors.Wrap(err, "Error updating module fields")
+2 -2
View File
@@ -39,13 +39,13 @@ func TestModule_updateFields(t *testing.T) {
test.NoError(t, err, "unexpected error on module creation")
test.Assert(t, len(m.Fields) == 2, "expecting to find two fields in the new module")
m.Fields[0].Name = "one-v2"
m.Fields[0].Name = "one-should-not-be-renamed"
m.Fields[1] = &types.ModuleField{Name: "three"}
m, err = repo.Update(m)
test.NoError(t, err, "unexpected error on module update")
test.Assert(t, len(m.Fields) == 2, "expecting to find two fields in the new module")
test.Assert(t, m.Fields[0].Name == "one-v2", "expecting to find field 'one'")
test.Assert(t, m.Fields[0].Name == "one", "expecting to find field 'one', got %q", m.Fields[0].Name)
test.Assert(t, m.Fields[0].Place == 0, "expecting Place=0")
test.Assert(t, m.Fields[1].Name == "three", "expecting to find field 'three'")
test.Assert(t, m.Fields[1].Place == 1, "expecting Place=1")