MCPcopy Index your code
hub / github.com/bytebase/bytebase / UpdateInstance

Method UpdateInstance

backend/api/v1/instance_service.go:509–624  ·  view source on GitHub ↗

UpdateInstance updates an instance.

(ctx context.Context, req *connect.Request[v1pb.UpdateInstanceRequest])

Source from the content-addressed store, hash-verified

507
508// UpdateInstance updates an instance.
509func (s *InstanceService) UpdateInstance(ctx context.Context, req *connect.Request[v1pb.UpdateInstanceRequest]) (*connect.Response[v1pb.Instance], error) {
510 if req.Msg.Instance == nil {
511 return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("instance must be set"))
512 }
513 if req.Msg.UpdateMask == nil {
514 return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("update_mask must be set"))
515 }
516
517 instance, err := getInstanceMessage(ctx, s.store, req.Msg.Instance.Name)
518 if err != nil {
519 if strings.Contains(err.Error(), "not found") && req.Msg.AllowMissing {
520 // When allow_missing is true and instance doesn't exist, create a new one
521 instanceID, ierr := common.GetInstanceID(req.Msg.Instance.Name)
522 if ierr != nil {
523 return nil, connect.NewError(connect.CodeInvalidArgument, ierr)
524 }
525
526 return s.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
527 InstanceId: instanceID,
528 Instance: req.Msg.Instance,
529 }))
530 }
531 return nil, connect.NewError(connect.CodeInvalidArgument, err)
532 }
533 if instance.Deleted {
534 return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("instance %q has been deleted", req.Msg.Instance.Name))
535 }
536
537 metadata := proto.CloneOf(instance.Metadata)
538 patch := &store.UpdateInstanceMessage{
539 ResourceID: &instance.ResourceID,
540 Workspace: instance.Workspace,
541 Metadata: metadata,
542 }
543 updateActivation := false
544 updateSyncInterval := false
545 for _, path := range req.Msg.UpdateMask.Paths {
546 switch path {
547 case "title":
548 patch.Metadata.Title = req.Msg.Instance.Title
549 case "environment":
550 if req.Msg.Instance.Environment == nil || *req.Msg.Instance.Environment == "" {
551 // Clear the environment if null or empty string is provided
552 patch.EnvironmentID = new("")
553 } else {
554 envID, err := common.GetEnvironmentID(*req.Msg.Instance.Environment)
555 if err != nil {
556 return nil, connect.NewError(connect.CodeInvalidArgument, err)
557 }
558 environment, err := s.store.GetEnvironmentByID(ctx, common.GetWorkspaceIDFromContext(ctx), envID)
559 if err != nil {
560 return nil, connect.NewError(connect.CodeInternal, err)
561 }
562 if environment == nil {
563 return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("environment %q not found", envID))
564 }
565 patch.EnvironmentID = &envID
566 }

Callers 1

BatchUpdateInstancesMethod · 0.95

Calls 15

CreateInstanceMethod · 0.95
checkActivationLimitMethod · 0.95
convertToV1InstanceMethod · 0.95
GetInstanceIDFunction · 0.92
GetEnvironmentIDFunction · 0.92
getInstanceMessageFunction · 0.85
convertV1DataSourcesFunction · 0.85
validateLabelsFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected