Skip to content

Commit 31808ef

Browse files
committed
command/meta: Enable migration from PSS to a backend
1 parent 64015ca commit 31808ef

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

internal/command/meta_backend.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,14 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di
781781
return nil, diags
782782
}
783783

784-
return m.stateStore_c_S(sMgr, opts.ViewType)
784+
// Grab a purely local backend to get the local state if it exists
785+
localB, moreDiags := m.Backend(&BackendOpts{ForceLocal: true, Init: true})
786+
diags = diags.Append(moreDiags)
787+
if moreDiags.HasErrors() {
788+
return nil, diags
789+
}
790+
791+
return m.stateStore_c_S(sMgr, "local", localB, opts.ViewType)
785792

786793
// Configuring a backend for the first time or -reconfigure flag was used
787794
case backendConfig != nil && s.Backend.Empty() &&
@@ -829,11 +836,20 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di
829836
s.StateStore.Provider.Source,
830837
backendConfig.Type,
831838
)
832-
return nil, diags.Append(&hcl.Diagnostic{
833-
Severity: hcl.DiagError,
834-
Summary: "Not implemented yet",
835-
Detail: "Migration from state store to backend is not implemented yet",
839+
840+
// Grab a purely local backend to get the local state if it exists
841+
localB, moreDiags := m.Backend(&BackendOpts{
842+
ForceLocal: true,
843+
Init: true,
844+
BackendConfig: backendConfig,
845+
// TODO: confirm no more fields need to be set here
836846
})
847+
diags = diags.Append(moreDiags)
848+
if moreDiags.HasErrors() {
849+
return nil, diags
850+
}
851+
852+
return m.stateStore_c_S(sMgr, backendConfig.Type, localB, opts.ViewType)
837853

838854
// Migration from backend to state store
839855
case backendConfig == nil && !s.Backend.Empty() &&
@@ -1854,8 +1870,8 @@ func (m *Meta) stateStore_C_s(c *configs.StateStore, stateStoreHash int, backend
18541870
return b, diags
18551871
}
18561872

1857-
// Unconfiguring a state store (moving from state store => local).
1858-
func (m *Meta) stateStore_c_S(ssSMgr *clistate.LocalState, viewType arguments.ViewType) (backend.Backend, tfdiags.Diagnostics) {
1873+
// Unconfiguring a state store (moving from state store => backend).
1874+
func (m *Meta) stateStore_c_S(ssSMgr *clistate.LocalState, dstBackendType string, dstBackend backendrun.OperationsBackend, viewType arguments.ViewType) (backend.Backend, tfdiags.Diagnostics) {
18591875
var diags tfdiags.Diagnostics
18601876

18611877
s := ssSMgr.State()
@@ -1864,13 +1880,6 @@ func (m *Meta) stateStore_c_S(ssSMgr *clistate.LocalState, viewType arguments.Vi
18641880
view := views.NewInit(viewType, m.View)
18651881
view.Output(views.StateMigrateLocalMessage, stateStoreType)
18661882

1867-
// Grab a purely local backend to get the local state if it exists
1868-
localB, moreDiags := m.Backend(&BackendOpts{ForceLocal: true, Init: true})
1869-
diags = diags.Append(moreDiags)
1870-
if moreDiags.HasErrors() {
1871-
return nil, diags
1872-
}
1873-
18741883
// Initialize the configured state store
18751884
ss, moreDiags := m.savedStateStore(ssSMgr)
18761885
diags = diags.Append(moreDiags)
@@ -1881,9 +1890,9 @@ func (m *Meta) stateStore_c_S(ssSMgr *clistate.LocalState, viewType arguments.Vi
18811890
// Perform the migration
18821891
err := m.backendMigrateState(&backendMigrateOpts{
18831892
SourceType: stateStoreType,
1884-
DestinationType: "local",
1893+
DestinationType: dstBackendType,
18851894
Source: ss,
1886-
Destination: localB,
1895+
Destination: dstBackend,
18871896
ViewType: viewType,
18881897
})
18891898
if err != nil {

0 commit comments

Comments
 (0)