Skip to content

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

feat(tenancy): add database labels to create database issue create context (#263)

spinningbote0a918bd4d0e
diff --git a/api/issue.go b/api/issue.go
index 84675dd74d..b9d0d93402 100644
--- a/api/issue.go
+++ b/api/issue.go
@@ -130,6 +130,9 @@ type CreateDatabaseContext struct {
BackupID int `json:"backupId"`
// BackupName is the name of the backup.
BackupName string `json:"backupName"`
+ // Labels is a json-encoded string from a list of DatabaseLabel.
+ // See definition in api.Database.
+ Labels string `jsonapi:"attr,labels,omitempty"`
}
// UpdateSchemaDetail is the detail of updating database schema.
diff --git a/api/task.go b/api/task.go
index 3281a36302..2d710adfca 100644
--- a/api/task.go
+++ b/api/task.go
@@ -80,6 +80,7 @@ type TaskDatabaseCreatePayload struct {
Statement string `json:"statement,omitempty"`
CharacterSet string `json:"character,omitempty"`
Collation string `json:"collation,omitempty"`
+ Labels string `json:"labels,omitempty"`
}
// TaskDatabaseSchemaUpdatePayload is the task payload for database schema update.
@@ -128,10 +129,10 @@ type Task struct {
TaskCheckRunList []*TaskCheckRun `jsonapi:"relation,taskCheckRun"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- Status TaskStatus `jsonapi:"attr,status"`
- Type TaskType `jsonapi:"attr,type"`
- Payload string `jsonapi:"attr,payload"`
+ Name string `jsonapi:"attr,name"`
+ Status TaskStatus `jsonapi:"attr,status"`
+ Type TaskType `jsonapi:"attr,type"`
+ Payload string `jsonapi:"attr,payload"`
EarliestAllowedTs int64 `jsonapi:"attr,earliestAllowedTs"`
}
@@ -154,12 +155,13 @@ type TaskCreate struct {
Type TaskType `jsonapi:"attr,type"`
// Payload is derived from fields below it
Payload string
- EarliestAllowedTs int64 `jsonapi:"attr,earliestAllowedTs"`
+ EarliestAllowedTs int64 `jsonapi:"attr,earliestAllowedTs"`
Statement string `jsonapi:"attr,statement"`
RollbackStatement string `jsonapi:"attr,rollbackStatement"`
DatabaseName string `jsonapi:"attr,databaseName"`
CharacterSet string `jsonapi:"attr,characterSet"`
Collation string `jsonapi:"attr,collation"`
+ Labels string `jsonapi:"attr,labels"`
BackupID *int `jsonapi:"attr,backupId"`
VCSPushEvent *common.VCSPushEvent
MigrationType db.MigrationType `jsonapi:"attr,migrationType"`
@@ -194,8 +196,8 @@ type TaskPatch struct {
UpdaterID int
// Domain specific fields
- Statement *string `jsonapi:"attr,statement"`
- Payload *string
+ Statement *string `jsonapi:"attr,statement"`
+ Payload *string
EarliestAllowedTs *int64 `jsonapi:"attr,earliestAllowedTs"`
}
diff --git a/server/database.go b/server/database.go
index 3c1f3c5d8a..2bcff7c9ba 100644
--- a/server/database.go
+++ b/server/database.go
@@ -43,7 +43,7 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
// Set database labels, except bb.environment is immutable and must match instance environment.
// This needs to be after we compose database relationship.
- if databaseCreate.Labels != nil {
+ if databaseCreate.Labels != nil && *databaseCreate.Labels != "" {
if err := s.setDatabaseLabels(ctx, *databaseCreate.Labels, database, databaseCreate.CreatorID); err != nil {
return err
}
diff --git a/server/issue.go b/server/issue.go
index 95c6b043f0..60e3d1d716 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -618,6 +618,7 @@ func (s *Server) getPipelineFromIssue(ctx context.Context, issueCreate *api.Issu
payload.ProjectID = issueCreate.ProjectID
payload.CharacterSet = m.CharacterSet
payload.Collation = m.Collation
+ payload.Labels = m.Labels
payload.DatabaseName, payload.Statement = getDatabaseNameAndStatement(instance.Engine, m.DatabaseName, m.CharacterSet, m.Collation)
bytes, err := json.Marshal(payload)
if err != nil {
diff --git a/server/task.go b/server/task.go
index 550ff72e20..b1d32cb3d3 100644
--- a/server/task.go
+++ b/server/task.go
@@ -534,8 +534,9 @@ func (s *Server) changeTaskStatusWithPatch(ctx context.Context, task *api.Task,
Name: payload.DatabaseName,
CharacterSet: payload.CharacterSet,
Collation: payload.Collation,
+ Labels: &payload.Labels,
}
- _, err = s.DatabaseService.CreateDatabase(ctx, databaseCreate)
+ database, err := s.DatabaseService.CreateDatabase(ctx, databaseCreate)
if err != nil {
// Just emits an error instead of failing, since we have another periodic job to sync db info.
// Though the db will be assigned to the default project instead of the desired project in that case.
@@ -546,6 +547,27 @@ func (s *Server) changeTaskStatusWithPatch(ctx context.Context, task *api.Task,
zap.Error(err),
)
}
+ err = s.composeDatabaseRelationship(ctx, database)
+ if err != nil {
+ s.l.Error("failed to compose database relationship after creating database",
+ zap.String("database_name", payload.DatabaseName),
+ zap.Int("project_id", payload.ProjectID),
+ zap.Int("instance_id", task.InstanceID),
+ zap.Error(err),
+ )
+ }
+ // Set database labels, except bb.environment is immutable and must match instance environment.
+ // This needs to be after we compose database relationship.
+ if err != nil && databaseCreate.Labels != nil && *databaseCreate.Labels != "" {
+ if err := s.setDatabaseLabels(ctx, *databaseCreate.Labels, database, databaseCreate.CreatorID); err != nil {
+ s.l.Error("failed to record database labels after creating database",
+ zap.String("database_name", payload.DatabaseName),
+ zap.Int("project_id", payload.ProjectID),
+ zap.Int("instance_id", task.InstanceID),
+ zap.Error(err),
+ )
+ }
+ }
}
// If create database or schema update task completes, we sync the corresponding instance schema immediately.