diff --git a/command/meta_backend_migrate.go b/command/meta_backend_migrate.go index 91c24a9c64..eceb3032d6 100644 --- a/command/meta_backend_migrate.go +++ b/command/meta_backend_migrate.go @@ -382,8 +382,10 @@ func (m *Meta) backendMigrateState_s_s(opts *backendMigrateOpts) error { } } - // Confirmed! Write. - if err := stateTwo.WriteState(one); err != nil { + // Confirmed! We'll have the statemgr package handle the migration, which + // includes preserving any lineage/serial information where possible, if + // both managers support such metadata. + if err := statemgr.Migrate(stateTwo, stateOne); err != nil { return fmt.Errorf(strings.TrimSpace(errBackendStateCopy), opts.OneType, opts.TwoType, err) } diff --git a/command/meta_backend_test.go b/command/meta_backend_test.go index fc6ba51fd6..f0c9510948 100644 --- a/command/meta_backend_test.go +++ b/command/meta_backend_test.go @@ -326,24 +326,23 @@ func TestMetaBackend_configureNewWithState(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %s", err) } - if err := s.RefreshState(); err != nil { + state, err := statemgr.RefreshAndRead(s) + if err != nil { t.Fatalf("unexpected error: %s", err) } - state := s.State() if state == nil { t.Fatal("state is nil") } - if testStateMgrCurrentLineage(s) != "backend-new-migrate" { - t.Fatalf("bad: %#v", state) + if got, want := testStateMgrCurrentLineage(s), "backend-new-migrate"; got != want { + t.Fatalf("lineage changed during migration\nnow: %s\nwas: %s", got, want) } // Write some state state = states.NewState() mark := markStateForMatching(state, "changing") - s.WriteState(state) - if err := s.PersistState(); err != nil { + if err := statemgr.WriteAndPersist(s, state); err != nil { t.Fatalf("unexpected error: %s", err) } diff --git a/command/test-fixtures/backend-new-migrate/terraform.tfstate b/command/test-fixtures/backend-new-migrate/terraform.tfstate index b1b1415d01..f1d8b968b3 100644 --- a/command/test-fixtures/backend-new-migrate/terraform.tfstate +++ b/command/test-fixtures/backend-new-migrate/terraform.tfstate @@ -8,7 +8,12 @@ "path": [ "root" ], - "outputs": {}, + "outputs": { + "foo": { + "type": "string", + "value": "bar" + } + }, "resources": {}, "depends_on": [] }