Skip to content

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

feat(database-group): create database group (#6264)

h3n4l04182e2ed8b6
diff --git a/backend/api/v1/common.go b/backend/api/v1/common.go
index 5957e9a8e8..427cc004c7 100644
--- a/backend/api/v1/common.go
+++ b/backend/api/v1/common.go
@@ -38,6 +38,7 @@ const (
secretNamePrefix = "secrets/"
webhookIDPrefix = "webhooks/"
sheetIDPrefix = "sheets/"
+ databaseGroupNamePrefix = "databaseGroups/"
deploymentConfigSuffix = "/deploymentConfig"
backupSettingSuffix = "/backupSetting"
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index ec6abc9c93..c674edb2eb 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -637,6 +637,60 @@ func (s *ProjectService) TestWebhook(ctx context.Context, request *v1pb.TestWebh
return &v1pb.TestWebhookResponse{Error: err.Error()}, nil
}
+// CreateDatabaseGroup creates a database group.
+func (s *ProjectService) CreateDatabaseGroup(ctx context.Context, request *v1pb.CreateDatabaseGroupRequest) (*v1pb.DatabaseGroup, error) {
+ projectResourceID, err := getProjectID(request.Parent)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectResourceID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ if project == nil {
+ return nil, status.Errorf(codes.NotFound, "project %q not found", request.Parent)
+ }
+ if project.Deleted {
+ return nil, status.Errorf(codes.InvalidArgument, "project %q has been deleted", request.Parent)
+ }
+
+ // The id value should be 4-63 characters, and valid characters are /[a-z][0-9]-/.
+ if len(request.DatabaseGroupId) < 4 || len(request.DatabaseGroupId) > 63 {
+ return nil, status.Errorf(codes.InvalidArgument, "database group id %q must be between 4 and 63 characters", request.DatabaseGroupId)
+ }
+ for _, c := range request.DatabaseGroupId {
+ if !((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-') {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid database group id %q", request.DatabaseGroupId)
+ }
+ }
+ if request.DatabaseGroup.DatabasePlaceholder == "" {
+ return nil, status.Errorf(codes.InvalidArgument, "database group database placeholder is required")
+ }
+
+ principalID := ctx.Value(common.PrincipalIDContextKey).(int)
+ storeDatabaseGroup := &store.DatabaseGroupMessage{
+ CreatorID: principalID,
+ ProjectResourceID: project.ResourceID,
+ Placeholder: request.DatabaseGroup.DatabasePlaceholder,
+ Expression: request.DatabaseGroup.DatabaseExpr,
+ }
+ databaseGroup, err := s.store.CreateDatabaseGroup(ctx, principalID, storeDatabaseGroup)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ return convertStoreToAPIDatabaseGroup(databaseGroup), nil
+}
+
+func convertStoreToAPIDatabaseGroup(databaseGroup *store.DatabaseGroupMessage) *v1pb.DatabaseGroup {
+ return &v1pb.DatabaseGroup{
+ Name: fmt.Sprintf("%s%s/%s%s", projectNamePrefix, databaseGroup.ProjectResourceID, databaseGroupNamePrefix, databaseGroup.ResourceID),
+ DatabasePlaceholder: databaseGroup.Placeholder,
+ DatabaseExpr: databaseGroup.Expression,
+ }
+}
+
func convertToStoreProjectWebhookMessage(webhook *v1pb.Webhook) (*store.ProjectWebhookMessage, error) {
tp, err := convertToAPIWebhookTypeString(webhook.Type)
if err != nil {
diff --git a/backend/store/database_group.go b/backend/store/database_group.go
new file mode 100644
index 0000000000..8baf83649e
--- /dev/null
+++ b/backend/store/database_group.go
@@ -0,0 +1,86 @@
+package store
+
+import (
+ "context"
+
+ "github.com/pkg/errors"
+ "google.golang.org/genproto/googleapis/type/expr"
+ "google.golang.org/protobuf/encoding/protojson"
+)
+
+// DatabaseGroupMessage is the message for database groups.
+type DatabaseGroupMessage struct {
+ // Output only fields.
+ //
+ ID int64
+ CreatedTs int64
+ UpdatedTs int64
+ CreatorID int
+ UpdaterID int
+
+ // Normal fields.
+ //
+ ProjectResourceID string
+ ResourceID string
+ Placeholder string
+ Expression *expr.Expr
+}
+
+// CreateDatabaseGroup creates a database group.
+func (s *Store) CreateDatabaseGroup(ctx context.Context, creatorPrincipalID int, databaseGroup *DatabaseGroupMessage) (*DatabaseGroupMessage, error) {
+ query := `
+ INSERT INTO db_group (
+ creator_id,
+ updater_id,
+ project_resource_id,
+ resource_id,
+ placeholder,
+ expression
+ ) VALUES ($1, $2, $3, $4, $5, $6)
+ RETURNING id, created_ts, updated_ts, creator_id, updater_id, project_resource_id, resource_id, placeholder, expression;
+ `
+ jsonExpr, err := protojson.Marshal(databaseGroup.Expression)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to marshal expression")
+ }
+
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to begin transaction")
+ }
+ defer tx.Rollback()
+
+ var insertedDatabaseGroup DatabaseGroupMessage
+ var stringExpr string
+ if err := tx.QueryRowContext(
+ ctx,
+ query,
+ creatorPrincipalID,
+ creatorPrincipalID,
+ databaseGroup.ProjectResourceID,
+ databaseGroup.ResourceID,
+ databaseGroup.Placeholder,
+ jsonExpr,
+ ).Scan(
+ &insertedDatabaseGroup.ID,
+ &insertedDatabaseGroup.CreatedTs,
+ &insertedDatabaseGroup.UpdatedTs,
+ &insertedDatabaseGroup.CreatorID,
+ &insertedDatabaseGroup.UpdaterID,
+ &insertedDatabaseGroup.ProjectResourceID,
+ &insertedDatabaseGroup.ResourceID,
+ &insertedDatabaseGroup.Placeholder,
+ &stringExpr,
+ ); err != nil {
+ return nil, errors.Wrapf(err, "failed to scan")
+ }
+ if err := tx.Commit(); err != nil {
+ return nil, errors.Wrapf(err, "failed to commit transaction")
+ }
+ var expression expr.Expr
+ if err := protojson.Unmarshal([]byte(stringExpr), &expression); err != nil {
+ return nil, errors.Wrapf(err, "failed to unmarshal expression")
+ }
+ insertedDatabaseGroup.Expression = &expression
+ return &insertedDatabaseGroup, nil
+}