helper/schema: record schema version on apply
We were previously only recording the schema version on refresh. This caused the state to be incorrectly written after a `terraform apply` causing subsequent commands to run the state through an unnecessary migration.
This commit is contained in:
parent
5b84f011dd
commit
3ba8ed536b
|
@ -151,7 +151,7 @@ func (r *Resource) Apply(
|
|||
err = r.Update(data, meta)
|
||||
}
|
||||
|
||||
return data.State(), err
|
||||
return r.recordCurrentSchemaVersion(data.State()), err
|
||||
}
|
||||
|
||||
// Diff returns a diff of this resource and is API compatible with the
|
||||
|
@ -207,14 +207,7 @@ func (r *Resource) Refresh(
|
|||
state = nil
|
||||
}
|
||||
|
||||
if state != nil && r.SchemaVersion > 0 {
|
||||
if state.Meta == nil {
|
||||
state.Meta = make(map[string]string)
|
||||
}
|
||||
state.Meta["schema_version"] = strconv.Itoa(r.SchemaVersion)
|
||||
}
|
||||
|
||||
return state, err
|
||||
return r.recordCurrentSchemaVersion(state), err
|
||||
}
|
||||
|
||||
// InternalValidate should be called to validate the structure
|
||||
|
@ -241,3 +234,14 @@ func (r *Resource) checkSchemaVersion(is *terraform.InstanceState) (bool, int) {
|
|||
stateSchemaVersion, _ := strconv.Atoi(is.Meta["schema_version"])
|
||||
return stateSchemaVersion < r.SchemaVersion, stateSchemaVersion
|
||||
}
|
||||
|
||||
func (r *Resource) recordCurrentSchemaVersion(
|
||||
state *terraform.InstanceState) *terraform.InstanceState {
|
||||
if state != nil && r.SchemaVersion > 0 {
|
||||
if state.Meta == nil {
|
||||
state.Meta = make(map[string]string)
|
||||
}
|
||||
state.Meta["schema_version"] = strconv.Itoa(r.SchemaVersion)
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
func TestResourceApply_create(t *testing.T) {
|
||||
r := &Resource{
|
||||
SchemaVersion: 2,
|
||||
Schema: map[string]*Schema{
|
||||
"foo": &Schema{
|
||||
Type: TypeInt,
|
||||
|
@ -51,6 +52,9 @@ func TestResourceApply_create(t *testing.T) {
|
|||
"id": "foo",
|
||||
"foo": "42",
|
||||
},
|
||||
Meta: map[string]string{
|
||||
"schema_version": "2",
|
||||
},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
|
@ -339,6 +343,7 @@ func TestResourceInternalValidate(t *testing.T) {
|
|||
|
||||
func TestResourceRefresh(t *testing.T) {
|
||||
r := &Resource{
|
||||
SchemaVersion: 2,
|
||||
Schema: map[string]*Schema{
|
||||
"foo": &Schema{
|
||||
Type: TypeInt,
|
||||
|
@ -368,6 +373,9 @@ func TestResourceRefresh(t *testing.T) {
|
|||
"id": "bar",
|
||||
"foo": "13",
|
||||
},
|
||||
Meta: map[string]string{
|
||||
"schema_version": "2",
|
||||
},
|
||||
}
|
||||
|
||||
actual, err := r.Refresh(s, 42)
|
||||
|
|
|
@ -843,6 +843,9 @@ func (i *InstanceState) init() {
|
|||
if i.Attributes == nil {
|
||||
i.Attributes = make(map[string]string)
|
||||
}
|
||||
if i.Meta == nil {
|
||||
i.Meta = make(map[string]string)
|
||||
}
|
||||
i.Ephemeral.init()
|
||||
}
|
||||
|
||||
|
@ -860,6 +863,12 @@ func (i *InstanceState) deepcopy() *InstanceState {
|
|||
n.Attributes[k] = v
|
||||
}
|
||||
}
|
||||
if i.Meta != nil {
|
||||
n.Meta = make(map[string]string, len(i.Meta))
|
||||
for k, v := range i.Meta {
|
||||
n.Meta[k] = v
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue