From 1696ade924a140df7313e8d27f3ebb5c70c71c6b Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Tue, 28 Aug 2018 14:08:47 +0200 Subject: [PATCH] backend/migrations: check all workspaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug that (in the case of the `local` backend) would only check if the selected workspace had a state when deciding to preform a migration. When the selected workspace didn’t have a state (but other existing workspace(s) did), the migration would not be preformed and the other workspaces would be ignored. --- command/meta_backend.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/command/meta_backend.go b/command/meta_backend.go index 185e8b6da..7d46260e1 100644 --- a/command/meta_backend.go +++ b/command/meta_backend.go @@ -907,20 +907,28 @@ func (m *Meta) backend_C_r_s( return nil, fmt.Errorf(errBackendLocalRead, err) } - env := m.Workspace() - - localState, err := localB.State(env) + workspaces, err := localB.States() if err != nil { return nil, fmt.Errorf(errBackendLocalRead, err) } - if err := localState.RefreshState(); err != nil { - return nil, fmt.Errorf(errBackendLocalRead, err) + + var localStates []state.State + for _, workspace := range workspaces { + localState, err := localB.State(workspace) + if err != nil { + return nil, fmt.Errorf(errBackendLocalRead, err) + } + if err := localState.RefreshState(); err != nil { + return nil, fmt.Errorf(errBackendLocalRead, err) + } + + // We only care about non-empty states. + if localS := localState.State(); !localS.Empty() { + localStates = append(localStates, localState) + } } - // If the local state is not empty, we need to potentially do a - // state migration to the new backend (with user permission), unless the - // destination is also "local" - if localS := localState.State(); !localS.Empty() { + if len(localStates) > 0 { // Perform the migration err = m.backendMigrateState(&backendMigrateOpts{ OneType: "local", @@ -946,12 +954,14 @@ func (m *Meta) backend_C_r_s( } if erase { - // We always delete the local state, unless that was our new state too. - if err := localState.WriteState(nil); err != nil { - return nil, fmt.Errorf(errBackendMigrateLocalDelete, err) - } - if err := localState.PersistState(); err != nil { - return nil, fmt.Errorf(errBackendMigrateLocalDelete, err) + for _, localState := range localStates { + // We always delete the local state, unless that was our new state too. + if err := localState.WriteState(nil); err != nil { + return nil, fmt.Errorf(errBackendMigrateLocalDelete, err) + } + if err := localState.PersistState(); err != nil { + return nil, fmt.Errorf(errBackendMigrateLocalDelete, err) + } } } }