Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MigrateDatabase RPC #5497

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
}, 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
}

Check warning on line 1029 in pkg/app/server/grpcapi/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/server/grpcapi/api.go#L1026-L1029

Added lines #L1026 - L1029 were not covered by tests

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

Check warning on line 1037 in pkg/app/server/grpcapi/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/server/grpcapi/api.go#L1031-L1037

Added lines #L1031 - L1037 were not covered by tests
}
return nil, status.Error(codes.Unimplemented, "Not implemented")

Check warning on line 1039 in pkg/app/server/grpcapi/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/server/grpcapi/api.go#L1039

Added line #L1039 was not covered by tests
}

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

Check warning on line 1050 in pkg/app/server/grpcapi/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/server/grpcapi/api.go#L1042-L1050

Added lines #L1042 - L1050 were not covered by tests
}

// 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 @@
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 @@
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
})

Check warning on line 386 in pkg/datastore/applicationstore.go

View check run for this annotation

Codecov / codecov/patch

pkg/datastore/applicationstore.go#L382-L386

Added lines #L382 - L386 were not covered by tests
}
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.