Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: get branch (#9659)

Danny Xudaa0ae93564a
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index 4593d8a5ef..2d112bb832 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -2,10 +2,16 @@ package v1
import (
"context"
+ "fmt"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
+ "google.golang.org/protobuf/types/known/timestamppb"
+ "github.com/bytebase/bytebase/backend/common"
enterprise "github.com/bytebase/bytebase/backend/enterprise/api"
+ api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
@@ -26,13 +32,64 @@ func NewBranchService(store *store.Store, licenseService enterprise.LicenseServi
}
// GetSchemaDesign gets the schema design.
-func (*BranchService) GetSchemaDesign(_ context.Context, _ *v1pb.GetSchemaDesignRequest) (*v1pb.SchemaDesign, error) {
- return nil, nil
+func (s *BranchService) GetSchemaDesign(ctx context.Context, request *v1pb.GetSchemaDesignRequest) (*v1pb.SchemaDesign, error) {
+ projectID, branchID, err := common.GetProjectAndBranchID(request.Name)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ if err := s.checkBranchPermission(ctx, projectID); err != nil {
+ return nil, err
+ }
+
+ project, branch, err := s.getBranch(ctx, projectID, branchID, true /* loadFull */)
+ if err != nil {
+ return nil, err
+ }
+
+ schemaDesign, err := s.convertBranchToSchemaDesign(ctx, project, branch, v1pb.SchemaDesignView_SCHEMA_DESIGN_VIEW_FULL)
+ if err != nil {
+ return nil, err
+ }
+ return schemaDesign, nil
}
// ListSchemaDesigns lists schema designs.
-func (*BranchService) ListSchemaDesigns(_ context.Context, _ *v1pb.ListSchemaDesignsRequest) (*v1pb.ListSchemaDesignsResponse, error) {
- return nil, nil
+func (s *BranchService) ListSchemaDesigns(ctx context.Context, request *v1pb.ListSchemaDesignsRequest) (*v1pb.ListSchemaDesignsResponse, error) {
+ projectID, err := common.GetProjectID(request.Parent)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ project, err := s.getProject(ctx, projectID)
+ if err != nil {
+ return nil, err
+ }
+ if err := s.checkBranchPermission(ctx, projectID); err != nil {
+ return nil, err
+ }
+
+ branchFind := &store.FindBranchMessage{
+ ProjectID: &project.ResourceID,
+ }
+ if request.View == v1pb.SchemaDesignView_SCHEMA_DESIGN_VIEW_FULL {
+ branchFind.LoadFull = true
+ }
+ branches, err := s.store.ListBranches(ctx, branchFind)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to list branches, error %v", err)
+ }
+
+ var schemaDesigns []*v1pb.SchemaDesign
+ for _, branch := range branches {
+ schemaDesign, err := s.convertBranchToSchemaDesign(ctx, project, branch, request.View)
+ if err != nil {
+ return nil, err
+ }
+ schemaDesigns = append(schemaDesigns, schemaDesign)
+ }
+ response := &v1pb.ListSchemaDesignsResponse{
+ SchemaDesigns: schemaDesigns,
+ }
+ return response, nil
}
// CreateSchemaDesign creates a new schema design.
@@ -51,6 +108,132 @@ func (*BranchService) MergeSchemaDesign(_ context.Context, _ *v1pb.MergeSchemaDe
}
// DeleteSchemaDesign deletes an existing schema design.
-func (*BranchService) DeleteSchemaDesign(_ context.Context, _ *v1pb.DeleteSchemaDesignRequest) (*emptypb.Empty, error) {
- return nil, nil
+func (s *BranchService) DeleteSchemaDesign(ctx context.Context, request *v1pb.DeleteSchemaDesignRequest) (*emptypb.Empty, error) {
+ projectID, branchID, err := common.GetProjectAndBranchID(request.Name)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ project, branch, err := s.getBranch(ctx, projectID, branchID, false /* loadFull */)
+ if err != nil {
+ return nil, err
+ }
+ if err := s.checkBranchPermission(ctx, project.ResourceID); err != nil {
+ return nil, err
+ }
+
+ if err := s.store.DeleteBranch(ctx, project.ResourceID, branch.ResourceID); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to delete branch, error %v", err)
+ }
+ return &emptypb.Empty{}, nil
+}
+
+func (s *BranchService) getProject(ctx context.Context, projectID string) (*store.ProjectMessage, error) {
+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ if project == nil {
+ return nil, status.Errorf(codes.NotFound, "project %q not found", projectID)
+ }
+ if project.Deleted {
+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", projectID)
+ }
+ return project, nil
+}
+
+func (s *BranchService) getBranch(ctx context.Context, projectID, branchID string, loadFull bool) (*store.ProjectMessage, *store.BranchMessage, error) {
+ project, err := s.getProject(ctx, projectID)
+ if err != nil {
+ return nil, nil, status.Errorf(codes.Internal, err.Error())
+ }
+ branch, err := s.store.GetBranch(ctx, &store.FindBranchMessage{ProjectID: &projectID, ResourceID: &branchID, LoadFull: loadFull})
+ if err != nil {
+ return nil, nil, err
+ }
+ if branch == nil {
+ return nil, nil, status.Errorf(codes.NotFound, "branch %q not found", branchID)
+ }
+ return project, branch, nil
+}
+
+func (s *BranchService) checkBranchPermission(ctx context.Context, projectID string) error {
+ role, ok := ctx.Value(common.RoleContextKey).(api.Role)
+ if !ok {
+ return status.Errorf(codes.Internal, "role not found")
+ }
+ if isOwnerOrDBA(role) {
+ return nil
+ }
+
+ principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
+ if !ok {
+ return status.Errorf(codes.Internal, "principal ID not found")
+ }
+ policy, err := s.store.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{ProjectID: &projectID})
+ if err != nil {
+ return status.Errorf(codes.Internal, err.Error())
+ }
+ for _, binding := range policy.Bindings {
+ if binding.Role == api.Developer || binding.Role == api.Owner {
+ for _, member := range binding.Members {
+ if member.ID == principalID || member.Email == api.AllUsers {
+ return nil
+ }
+ }
+ }
+ }
+ return status.Errorf(codes.PermissionDenied, "permission denied")
+}
+
+func (s *BranchService) convertBranchToSchemaDesign(ctx context.Context, project *store.ProjectMessage, branch *store.BranchMessage, view v1pb.SchemaDesignView) (*v1pb.SchemaDesign, error) {
+ creator, err := s.store.GetUserByID(ctx, branch.CreatorID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to get creator, error %v", err)
+ }
+ if creator == nil {
+ return nil, status.Errorf(codes.NotFound, "creator %d not found", branch.CreatorID)
+ }
+ updater, err := s.store.GetUserByID(ctx, branch.UpdaterID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to get updater, error %v", err)
+ }
+ if updater == nil {
+ return nil, status.Errorf(codes.NotFound, "updater %d not found", branch.UpdaterID)
+ }
+
+ var baselineDatabase, baselineBranch string
+ schemaDesignType := v1pb.SchemaDesign_MAIN_BRANCH
+ if branch.Config != nil {
+ baselineDatabase = branch.Config.SourceDatabase
+ if branch.Config.SourceBranch != "" {
+ schemaDesignType = v1pb.SchemaDesign_PERSONAL_DRAFT
+ baselineBranch = branch.Config.SourceBranch
+ }
+ }
+
+ schemaDesign := &v1pb.SchemaDesign{
+ Name: fmt.Sprintf("%s%s/%s%v", common.ProjectNamePrefix, project.ResourceID, common.BranchPrefix, branch.ResourceID),
+ Title: branch.ResourceID,
+ Etag: fmt.Sprintf("%d", branch.CreatedTime.UnixMilli()),
+ BaselineSheetName: baselineBranch,
+ Engine: v1pb.Engine(branch.Engine),
+ BaselineDatabase: baselineDatabase,
+ Type: schemaDesignType,
+ Creator: common.FormatUserEmail(creator.Email),
+ Updater: common.FormatUserEmail(updater.Email),
+ CreateTime: timestamppb.New(branch.CreatedTime),
+ UpdateTime: timestamppb.New(branch.UpdatedTime),
+ }
+
+ if view != v1pb.SchemaDesignView_SCHEMA_DESIGN_VIEW_FULL {
+ return schemaDesign, nil
+ }
+
+ schemaDesign.Schema = branch.Head.Schema
+ schemaDesign.SchemaMetadata = convertDatabaseMetadata(nil /* database */, branch.Head.Metadata, branch.Head.DatabaseConfig, v1pb.DatabaseMetadataView_DATABASE_METADATA_VIEW_FULL, nil /* filter */)
+ schemaDesign.BaselineSchema = branch.Base.Schema
+ schemaDesign.BaselineSchemaMetadata = convertDatabaseMetadata(nil /* database */, branch.Base.Metadata, branch.Base.DatabaseConfig, v1pb.DatabaseMetadataView_DATABASE_METADATA_VIEW_FULL, nil /* filter */)
+ return schemaDesign, nil
}
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index ff36e48e2a..63df2aae16 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -1982,10 +1982,12 @@ type metadataFilter struct {
func convertDatabaseMetadata(database *store.DatabaseMessage, metadata *storepb.DatabaseSchemaMetadata, config *storepb.DatabaseConfig, requestView v1pb.DatabaseMetadataView, filter *metadataFilter) *v1pb.DatabaseMetadata {
m := &v1pb.DatabaseMetadata{
- Name: fmt.Sprintf("%s%s/%s%s%s", common.InstanceNamePrefix, database.InstanceID, common.DatabaseIDPrefix, database.DatabaseName, common.MetadataSuffix),
CharacterSet: metadata.CharacterSet,
Collation: metadata.Collation,
}
+ if database != nil {
+ m.Name = fmt.Sprintf("%s%s/%s%s%s", common.InstanceNamePrefix, database.InstanceID, common.DatabaseIDPrefix, database.DatabaseName, common.MetadataSuffix)
+ }
for _, schema := range metadata.Schemas {
if filter != nil && (schema.Name != "" && filter.schema != schema.Name) {
continue
diff --git a/backend/common/resource_name.go b/backend/common/resource_name.go
index ecef1bb4b2..a7087992f9 100644
--- a/backend/common/resource_name.go
+++ b/backend/common/resource_name.go
@@ -43,6 +43,7 @@ const (
LogNamePrefix = "logs/"
InboxNamePrefix = "inbox/"
SchemaDesignPrefix = "schemaDesigns/"
+ BranchPrefix = "branches/"
DeploymentConfigPrefix = "deploymentConfigs/"
ChangelistsPrefix = "changelists/"
@@ -481,6 +482,15 @@ func GetProjectResourceIDAndSchemaDesignSheetID(name string) (string, string, er
return tokens[0], tokens[1], nil
}
+// GetProjectAndBranchID returns the project and branch ID from a resource name.
+func GetProjectAndBranchID(name string) (string, string, error) {
+ tokens, err := GetNameParentTokens(name, ProjectNamePrefix, BranchPrefix)
+ if err != nil {
+ return "", "", err
+ }
+ return tokens[0], tokens[1], nil
+}
+
// TrimSuffix trims the suffix from the name and returns the trimmed name.
func TrimSuffix(name, suffix string) (string, error) {
if !strings.HasSuffix(name, suffix) {
diff --git a/backend/store/branch.go b/backend/store/branch.go
index 8835d9c7a8..184d645f34 100644
--- a/backend/store/branch.go
+++ b/backend/store/branch.go
@@ -35,6 +35,8 @@ type BranchMessage struct {
type FindBranchMessage struct {
ProjectID *string
ResourceID *string
+ // TODO(d): handle LoadFull.
+ LoadFull bool
}
// UpdateBranchMessage is the message to update a branch.