Skip to content

Commit

Permalink
Implement MigrateDatabase RPC (#5497)
Browse files Browse the repository at this point in the history
* Add UpdateDeployTargets method to ApplicationStore interface

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Add MigrateDatabase RPC

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Log error when application migration fails in MigrateDatabase RPC

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Fix build error

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Run make gen/code

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Jan 21, 2025
1 parent 7f7d7ec commit 1fef133
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/app/server/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1022,6 +1023,33 @@ 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 {
a.logger.Error("failed to migrate application", zap.Error(err))
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")
}
return nil
}

// 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) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/datastore/applicationstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
})
}
14 changes: 14 additions & 0 deletions pkg/datastore/datastoretest/datastore.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1fef133

Please sign in to comment.