Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(branch): return failed precondition if deleted branch contains sub branches (#10288)
h3n4le2cecac92e2c
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.goindex f8b2cc634b..b05e8511d6 100644--- a/backend/api/v1/branch_service.go+++ b/backend/api/v1/branch_service.go@@ -615,6 +615,20 @@ func (s *BranchService) DeleteBranch(ctx context.Context, request *v1pb.DeleteBrreturn nil, status.Errorf(codes.NotFound, "branch %q not found", branchID)}+ if !request.Force {+ childBranches, err := s.store.ListBranches(ctx, &store.FindBranchMessage{+ ProjectID: &project.ResourceID,+ LoadFull: false,+ ParentBranchResourceID: &request.Name,+ })+ if err != nil {+ return nil, status.Errorf(codes.Internal, "failed to list child branches, error %v", err)+ }+ if len(childBranches) > 0 {+ return nil, status.Errorf(codes.FailedPrecondition, "branch %q has child branches, please delete them first", branchID)+ }+ }+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)}diff --git a/backend/store/branch.go b/backend/store/branch.goindex 4ac8379ebe..e91b5419c0 100644--- a/backend/store/branch.go+++ b/backend/store/branch.go@@ -39,6 +39,8 @@ type FindBranchMessage struct {ResourceID *stringUID *intLoadFull bool++ ParentBranchResourceID *string}// UpdateBranchMessage is the message to update a branch.@@ -86,6 +88,9 @@ func (s *Store) ListBranches(ctx context.Context, find *FindBranchMessage) ([]*Bif v := find.UID; v != nil {where, args = append(where, fmt.Sprintf("branch.id = $%d", len(args)+1)), append(args, *v)}+ if v := find.ParentBranchResourceID; v != nil {+ where, args = append(where, fmt.Sprintf("branch.config->>'sourceBranch' = $%d", len(args)+1)), append(args, *v)+ }tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})if err != nil {diff --git a/frontend/src/store/modules/branch.ts b/frontend/src/store/modules/branch.tsindex fc66f5a621..0523a96688 100644--- a/frontend/src/store/modules/branch.ts+++ b/frontend/src/store/modules/branch.ts@@ -105,6 +105,7 @@ export const useBranchStore = defineStore("schema_design", () => {const deleteBranch = async (name: string) => {await branchServiceClient.deleteBranch({name,+ force: true,});branchMapByName.delete(name);};