From 3ba8ed536b373fd403c7f64103765b013164b1a8 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 18 Mar 2015 19:07:29 -0500 Subject: [PATCH] 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. --- helper/schema/resource.go | 22 +++++++++++++--------- helper/schema/resource_test.go | 8 ++++++++ terraform/state.go | 9 +++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/helper/schema/resource.go b/helper/schema/resource.go index a19912eed..797d021ab 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -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 +} diff --git a/helper/schema/resource_test.go b/helper/schema/resource_test.go index b1c42721f..e406e55b9 100644 --- a/helper/schema/resource_test.go +++ b/helper/schema/resource_test.go @@ -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) diff --git a/terraform/state.go b/terraform/state.go index 3dbad7ae6..42e9023ba 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -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 }