Skip to content

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

feat: support create issue by using groups (#6373)

h3n4l0b0d894ae7e2
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index a9882222dc..ec98b51079 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -1715,7 +1715,7 @@ func (s *ProjectService) GetSchemaGroup(ctx context.Context, request *v1pb.GetSc
}
func (s *ProjectService) convertStoreToAPIDatabaseGroupFull(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) (*v1pb.DatabaseGroup, error) {
- matches, unmatches, err := s.getMatchesAndUnmatchesDatabases(ctx, databaseGroup, projectResourceID)
+ matches, unmatches, err := s.getMatchedAndUnmatchedDatabases(ctx, databaseGroup, projectResourceID)
if err != nil {
return nil, err
}
@@ -1737,7 +1737,7 @@ func (s *ProjectService) convertStoreToAPIDatabaseGroupFull(ctx context.Context,
return ret, nil
}
-func (s *ProjectService) getMatchesAndUnmatchesDatabases(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) ([]*store.DatabaseMessage, []*store.DatabaseMessage, error) {
+func (s *ProjectService) getMatchedAndUnmatchedDatabases(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) ([]*store.DatabaseMessage, []*store.DatabaseMessage, error) {
databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{
ProjectID: &projectResourceID,
})
@@ -1795,7 +1795,7 @@ func convertStoreToAPIDatabaseGroupBasic(databaseGroup *store.DatabaseGroupMessa
}
func (s *ProjectService) convertStoreToAPISchemaGroupFull(ctx context.Context, schemaGroup *store.SchemaGroupMessage, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) (*v1pb.SchemaGroup, error) {
- matches, unmatches, err := s.getMatchesAndUnmatchesTables(ctx, schemaGroup, databaseGroup, projectResourceID)
+ matches, unmatches, err := s.getMatchesAndUnmatchedTables(ctx, schemaGroup, databaseGroup, projectResourceID)
if err != nil {
return nil, err
}
@@ -1809,8 +1809,8 @@ func (s *ProjectService) convertStoreToAPISchemaGroupFull(ctx context.Context, s
return ret, nil
}
-func (s *ProjectService) getMatchesAndUnmatchesTables(ctx context.Context, schemaGroup *store.SchemaGroupMessage, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) ([]*v1pb.SchemaGroup_Table, []*v1pb.SchemaGroup_Table, error) {
- matchesDatabases, _, err := s.getMatchesAndUnmatchesDatabases(ctx, databaseGroup, projectResourceID)
+func (s *ProjectService) getMatchesAndUnmatchedTables(ctx context.Context, schemaGroup *store.SchemaGroupMessage, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) ([]*v1pb.SchemaGroup_Table, []*v1pb.SchemaGroup_Table, error) {
+ matchesDatabases, _, err := s.getMatchedAndUnmatchedDatabases(ctx, databaseGroup, projectResourceID)
if err != nil {
return nil, nil, err
}
diff --git a/backend/legacyapi/issue.go b/backend/legacyapi/issue.go
index ab393db8de..d0b2e405d5 100644
--- a/backend/legacyapi/issue.go
+++ b/backend/legacyapi/issue.go
@@ -159,8 +159,12 @@ type MigrationDetail struct {
DatabaseID int `json:"databaseId"`
// SheetID is the ID of a sheet. Statement and sheet ID is mutually exclusive.
SheetID int `json:"sheetId"`
+ // DatabaseGroupName is the name of a database group.
+ DatabaseGroupName string `json:"databaseGroupName"`
+ // SchemaGroupNames is the list of schema group names.
+ SchemaGroupNames []string `json:"schemaGroupNames"`
// EarliestAllowedTs the earliest execution time of the change at system local Unix timestamp in seconds.
- EarliestAllowedTs int64 `jsonapi:"attr,earliestAllowedTs"`
+ EarliestAllowedTs int64 `json:"earliestAllowedTs"`
// SchemaVersion is parsed from VCS file name.
// It is automatically generated in the UI workflow.
SchemaVersion string `json:"schemaVersion"`
diff --git a/backend/server/issue.go b/backend/server/issue.go
index 5ec262b865..65a4433232 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -7,14 +7,18 @@ import (
"fmt"
"math"
"net/http"
+ "reflect"
"sort"
"strconv"
"strings"
+ "github.com/google/cel-go/cel"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"go.uber.org/zap"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"github.com/bytebase/bytebase/backend/common"
@@ -899,6 +903,9 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Con
emptyDatabaseIDCount++
}
}
+ // Now, we only allow 2 cases:
+ // 1. All migration details have database ID, which means the users manually select the databases to change.
+ // 2. Only have one migration detail, and the database ID is empty, which means the users want to deploy to all tenant databases or specifying the database group.
if emptyDatabaseIDCount > 0 && databaseIDCount > 0 {
return nil, echo.NewHTTPError(http.StatusBadRequest, "Migration detail should set either database name or database ID.")
}
@@ -918,23 +925,52 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Con
aggregatedMatrix := make([][]*store.DatabaseMessage, len(deploySchedule.Deployments))
databaseToMigrationList := make(map[int][]*api.MigrationDetail)
- databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{ProjectID: &project.ResourceID})
+ allDatabases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{ProjectID: &project.ResourceID})
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch databases in project ID: %v", issueCreate.ProjectID)).SetInternal(err)
}
- databaseMap := make(map[int]*store.DatabaseMessage)
- for _, database := range databases {
- databaseMap[database.UID] = database
+ allDatabasesMap := make(map[int]*store.DatabaseMessage)
+ for _, database := range allDatabases {
+ allDatabasesMap[database.UID] = database
}
if databaseIDCount == 0 {
- // Deploy to all tenant databases.
migrationDetail := c.DetailList[0]
- matrix, err := utils.GetDatabaseMatrixFromDeploymentSchedule(deploySchedule, databases)
- if err != nil {
- return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to build deployment pipeline").SetInternal(err)
+ var matrix [][]*store.DatabaseMessage
+ if migrationDetail.DatabaseGroupName != "" {
+ // Deploy to given database group.
+ parts := strings.Split(migrationDetail.DatabaseGroupName, "/")
+ if len(parts) != 4 {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "Invalid database group name")
+ }
+ projectResourceID, databaseGroupResourceID := parts[1], parts[3]
+ if project.ResourceID != projectResourceID {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "Invalid database group name")
+ }
+ databaseGroup, err := s.store.GetDatabaseGroup(ctx, &store.FindDatabaseGroupMessage{ProjectUID: &project.UID, ResourceID: &databaseGroupResourceID})
+ if err != nil {
+ return nil, err
+ }
+ if databaseGroup == nil {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "Invalid database group name")
+ }
+ // TODO(zp): get matching databases.
+ matches, _, err := getMatchedAndUnmatchedDatabases(ctx, databaseGroup, allDatabases)
+ if err != nil {
+ return nil, err
+ }
+ matrix = [][]*store.DatabaseMessage{
+ matches,
+ }
+ aggregatedMatrix = matrix
+ } else {
+ // Deploy to all tenant databases.
+ matrix, err = utils.GetDatabaseMatrixFromDeploymentSchedule(deploySchedule, allDatabases)
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to build deployment pipeline").SetInternal(err)
+ }
+ aggregatedMatrix = matrix
}
- aggregatedMatrix = matrix
for _, databaseList := range matrix {
for _, database := range databaseList {
// There should be only one migration per database for tenant mode deployment.
@@ -943,7 +979,7 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Con
}
} else {
for _, d := range c.DetailList {
- database, ok := databaseMap[d.DatabaseID]
+ database, ok := allDatabasesMap[d.DatabaseID]
if !ok {
return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID %d not found in project %d", d.DatabaseID, issueCreate.ProjectID))
}
@@ -1578,3 +1614,47 @@ func convertDatabaseLabels(labelsJSON string) ([]*api.DatabaseLabel, error) {
}
return labels, nil
}
+
+// TODO(zp): keep this function as same as the one in the project_service.go.
+func getMatchedAndUnmatchedDatabases(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, allDatabases []*store.DatabaseMessage) ([]*store.DatabaseMessage, []*store.DatabaseMessage, error) {
+ e, err := cel.NewEnv(
+ cel.Variable("resource", cel.MapType(cel.StringType, cel.AnyType)),
+ )
+ if err != nil {
+ return nil, nil, status.Errorf(codes.Internal, err.Error())
+ }
+ ast, issues := e.Parse(databaseGroup.Expression.Expression)
+ if issues != nil && issues.Err() != nil {
+ return nil, nil, status.Errorf(codes.InvalidArgument, issues.Err().Error())
+ }
+ prog, err := e.Program(ast)
+ if err != nil {
+ return nil, nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+
+ var matches []*store.DatabaseMessage
+ var unmatches []*store.DatabaseMessage
+
+ for _, database := range allDatabases {
+ res, _, err := prog.ContextEval(ctx, map[string]any{
+ "resource": map[string]any{
+ "database_name": database.DatabaseName,
+ "environment_name": fmt.Sprintf("%s%s", "environments/", database.EnvironmentID),
+ },
+ })
+ if err != nil {
+ return nil, nil, status.Errorf(codes.Internal, err.Error())
+ }
+
+ val, err := res.ConvertToNative(reflect.TypeOf(false))
+ if err != nil {
+ return nil, nil, status.Errorf(codes.Internal, "expect bool result")
+ }
+ if boolVal, ok := val.(bool); ok && boolVal {
+ matches = append(matches, database)
+ } else {
+ unmatches = append(unmatches, database)
+ }
+ }
+ return matches, unmatches, nil
+}