From 480cd67d743b50e976435ecb971b8f3126f4406c Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada-Dazai Date: Mon, 20 Jan 2025 15:54:39 +0900 Subject: [PATCH 1/5] Add UpdateDeployTargets method to ApplicationStore interface Signed-off-by: Shinnosuke Sawada-Dazai --- pkg/datastore/applicationstore.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/datastore/applicationstore.go b/pkg/datastore/applicationstore.go index 6f405e49ca..82e4966deb 100644 --- a/pkg/datastore/applicationstore.go +++ b/pkg/datastore/applicationstore.go @@ -198,6 +198,7 @@ type ApplicationStore interface { UpdateBasicInfo(ctx context.Context, id, name, description string, labels map[string]string) error UpdateConfiguration(ctx context.Context, id, pipedID, platformProvider, configFilename string) error UpdatePlatformProvider(ctx context.Context, id string, provider string) error + UpdateDeployTargets(ctx context.Context, id string, targets []string) error } type applicationStore struct { @@ -377,3 +378,10 @@ func (s *applicationStore) UpdatePlatformProvider(ctx context.Context, id string return nil }) } + +func (s *applicationStore) UpdateDeployTargets(ctx context.Context, id string, targets []string) error { + return s.update(ctx, id, func(app *model.Application) error { + app.DeployTargets = targets + return nil + }) +} From 0a6ffee8ab1c3fbbff8c26ac4774e829f5e1116d Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada-Dazai Date: Mon, 20 Jan 2025 15:55:05 +0900 Subject: [PATCH 2/5] Add MigrateDatabase RPC Signed-off-by: Shinnosuke Sawada-Dazai --- pkg/app/server/grpcapi/api.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/app/server/grpcapi/api.go b/pkg/app/server/grpcapi/api.go index 5b95226818..6c986d9107 100644 --- a/pkg/app/server/grpcapi/api.go +++ b/pkg/app/server/grpcapi/api.go @@ -47,6 +47,7 @@ type apiApplicationStore interface { Disable(ctx context.Context, id string) error UpdateConfigFilename(ctx context.Context, id, filename string) error UpdateConfiguration(ctx context.Context, id, pipedID, platformProvider, configFilename string) error + UpdateDeployTargets(ctx context.Context, id string, targets []string) error } type apiDeploymentStore interface { @@ -1022,6 +1023,31 @@ func (a *API) Encrypt(ctx context.Context, req *apiservice.EncryptRequest) (*api }, nil } +func (a *API) MigrateDatabase(ctx context.Context, req *apiservice.MigrateDatabaseRequest) (*apiservice.MigrateDatabaseResponse, error) { + if _, err := requireAPIKey(ctx, model.APIKey_READ_WRITE, a.logger); err != nil { + return nil, err + } + + switch { //nolint:gocritic // we plan to add more cases + case req.GetApplication() != nil: + if err := a.migrateApplication(ctx, req.GetApplication()); err != nil { + return nil, err + } + return &apiservice.MigrateDatabaseResponse{}, nil + } + return nil, status.Error(codes.Unimplemented, "Not implemented") +} + +func (a *API) migrateApplication(ctx context.Context, app *apiservice.MigrateDatabaseRequest_Application) error { + application, err := getApplication(ctx, a.applicationStore, app.ApplicationId, a.logger) + if err != nil { + return gRPCStoreError(err, "get application") + } + if err := a.applicationStore.UpdateDeployTargets(ctx, app.ApplicationId, []string{application.PlatformProvider}); err != nil { + return gRPCStoreError(err, "update application") + } +} + // requireAPIKey checks the existence of an API key inside the given context // and ensures that it has enough permissions for the give role. func requireAPIKey(ctx context.Context, role model.APIKey_Role, logger *zap.Logger) (*model.APIKey, error) { From 3f1a551c0cd4993b0f9e8cce7777a6c0866ef574 Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada-Dazai Date: Mon, 20 Jan 2025 15:57:15 +0900 Subject: [PATCH 3/5] Log error when application migration fails in MigrateDatabase RPC Signed-off-by: Shinnosuke Sawada-Dazai --- pkg/app/server/grpcapi/api.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/app/server/grpcapi/api.go b/pkg/app/server/grpcapi/api.go index 6c986d9107..bf513f55cd 100644 --- a/pkg/app/server/grpcapi/api.go +++ b/pkg/app/server/grpcapi/api.go @@ -1031,6 +1031,7 @@ func (a *API) MigrateDatabase(ctx context.Context, req *apiservice.MigrateDataba switch { //nolint:gocritic // we plan to add more cases case req.GetApplication() != nil: if err := a.migrateApplication(ctx, req.GetApplication()); err != nil { + a.logger.Error("failed to migrate application", zap.Error(err)) return nil, err } return &apiservice.MigrateDatabaseResponse{}, nil From bedb38a334d2c6268a59501f7a5948db5130ecce Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada-Dazai Date: Mon, 20 Jan 2025 16:07:41 +0900 Subject: [PATCH 4/5] Fix build error Signed-off-by: Shinnosuke Sawada-Dazai --- pkg/app/server/grpcapi/api.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/app/server/grpcapi/api.go b/pkg/app/server/grpcapi/api.go index bf513f55cd..7c953636d3 100644 --- a/pkg/app/server/grpcapi/api.go +++ b/pkg/app/server/grpcapi/api.go @@ -1047,6 +1047,7 @@ func (a *API) migrateApplication(ctx context.Context, app *apiservice.MigrateDat if err := a.applicationStore.UpdateDeployTargets(ctx, app.ApplicationId, []string{application.PlatformProvider}); err != nil { return gRPCStoreError(err, "update application") } + return nil } // requireAPIKey checks the existence of an API key inside the given context From 4519815a382761347db8c010fbf93ae9c99df773 Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada-Dazai Date: Mon, 20 Jan 2025 16:09:36 +0900 Subject: [PATCH 5/5] Run make gen/code Signed-off-by: Shinnosuke Sawada-Dazai --- pkg/datastore/datastoretest/datastore.mock.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/datastore/datastoretest/datastore.mock.go b/pkg/datastore/datastoretest/datastore.mock.go index a2de87020a..7b73c1e89e 100644 --- a/pkg/datastore/datastoretest/datastore.mock.go +++ b/pkg/datastore/datastoretest/datastore.mock.go @@ -546,6 +546,20 @@ func (mr *MockApplicationStoreMockRecorder) UpdateConfiguration(ctx, id, pipedID return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateConfiguration", reflect.TypeOf((*MockApplicationStore)(nil).UpdateConfiguration), ctx, id, pipedID, platformProvider, configFilename) } +// UpdateDeployTargets mocks base method. +func (m *MockApplicationStore) UpdateDeployTargets(ctx context.Context, id string, targets []string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateDeployTargets", ctx, id, targets) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateDeployTargets indicates an expected call of UpdateDeployTargets. +func (mr *MockApplicationStoreMockRecorder) UpdateDeployTargets(ctx, id, targets any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateDeployTargets", reflect.TypeOf((*MockApplicationStore)(nil).UpdateDeployTargets), ctx, id, targets) +} + // UpdateDeployingStatus mocks base method. func (m *MockApplicationStore) UpdateDeployingStatus(ctx context.Context, id string, deploying bool) error { m.ctrl.T.Helper()