Skip to content

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

refactor: split data model object for api.LabelKey (#900)

Yilong Li1a0db353ab43
diff --git a/api/label.go b/api/label.go
index 974b2e6442..c118f39840 100644
--- a/api/label.go
+++ b/api/label.go
@@ -9,7 +9,7 @@ const (
// EnvironmentKeyName is the reserved key for environment.
EnvironmentKeyName string = "bb.environment"
- // DatabaseLabelSizeMax is the maximium size of database labels.
+ // DatabaseLabelSizeMax is the maximum size of database labels.
DatabaseLabelSizeMax = 4
labelLengthMax = 63
@@ -19,6 +19,43 @@ const (
TenantLabelKey = "bb.tenant"
)
+// LabelKeyRaw is the store model for an LabelKey.
+// Fields have exactly the same meanings as LabelKey.
+type LabelKeyRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Domain specific fields
+ // bb.environment is a reserved key and identically mapped from environments. It has zero ID and its values are immutable.
+ Key string
+ ValueList []string
+}
+
+// ToLabelKey creates an instance of LabelKey based on the LabelKeyRaw.
+// This is intended to be called when we need to compose an LabelKey relationship.
+func (raw *LabelKeyRaw) ToLabelKey() *LabelKey {
+ labelKey := LabelKey{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Domain specific fields
+ // bb.environment is a reserved key and identically mapped from environments. It has zero ID and its values are immutable.
+ Key: raw.Key,
+ }
+ labelKey.ValueList = append(labelKey.ValueList, raw.ValueList...)
+ return &labelKey
+}
+
// LabelKey is the available key for labels.
type LabelKey struct {
ID int `jsonapi:"primary,labelKey"`
@@ -159,9 +196,9 @@ type DatabaseLabelUpsert struct {
// LabelService is the service for labels.
type LabelService interface {
// FindLabelKeyList finds all available keys for labels.
- FindLabelKeyList(ctx context.Context, find *LabelKeyFind) ([]*LabelKey, error)
+ FindLabelKeyList(ctx context.Context, find *LabelKeyFind) ([]*LabelKeyRaw, error)
// PatchLabelKey patches a label key.
- PatchLabelKey(ctx context.Context, patch *LabelKeyPatch) (*LabelKey, error)
+ PatchLabelKey(ctx context.Context, patch *LabelKeyPatch) (*LabelKeyRaw, error)
// FindDatabaseLabelList finds all database labels matching the conditions, ascending by key.
FindDatabaseLabelList(ctx context.Context, find *DatabaseLabelFind) ([]*DatabaseLabelRaw, error)
// SetDatabaseLabelList sets a database's labels to new labels.
diff --git a/server/database.go b/server/database.go
index de51aa0509..5394bf2167 100644
--- a/server/database.go
+++ b/server/database.go
@@ -908,10 +908,15 @@ func (s *Server) setDatabaseLabels(ctx context.Context, labelsJSON string, datab
}
rowStatus := api.Normal
- labelKeyList, err := s.LabelService.FindLabelKeyList(ctx, &api.LabelKeyFind{RowStatus: &rowStatus})
+ labelKeyRawList, err := s.LabelService.FindLabelKeyList(ctx, &api.LabelKeyFind{RowStatus: &rowStatus})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find label key list").SetInternal(err)
}
+ // TODO(dragonly): implement composeLabelKeyRelationship
+ var labelKeyList []*api.LabelKey
+ for _, raw := range labelKeyRawList {
+ labelKeyList = append(labelKeyList, raw.ToLabelKey())
+ }
if err = validateDatabaseLabelList(labels, labelKeyList, database.Instance.Environment.Name); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate database labels").SetInternal(err)
diff --git a/server/issue.go b/server/issue.go
index ad600f6c9c..4c38bbabdb 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -331,6 +331,7 @@ func (s *Server) composeIssueRelationship(ctx context.Context, issue *api.Issue)
return nil
}
+// TODO(dragonly): refactor validate only code path
func (s *Server) composeIssueRelationshipValidateOnly(ctx context.Context, issue *api.Issue) error {
var err error
diff --git a/server/label.go b/server/label.go
index df1aa1d7ac..3fd3319b04 100644
--- a/server/label.go
+++ b/server/label.go
@@ -21,10 +21,15 @@ func (s *Server) registerLabelRoutes(g *echo.Group) {
find := &api.LabelKeyFind{
RowStatus: &rowStatus,
}
- labelKeyList, err := s.LabelService.FindLabelKeyList(ctx, find)
+ labelKeyRawList, err := s.LabelService.FindLabelKeyList(ctx, find)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch label keys").SetInternal(err)
}
+ // TODO(dragonly): compose this
+ var labelKeyList []*api.LabelKey
+ for _, raw := range labelKeyRawList {
+ labelKeyList = append(labelKeyList, raw.ToLabelKey())
+ }
// Add reserved environment key.
envRawList, err := s.EnvironmentService.FindEnvironmentList(ctx, &api.EnvironmentFind{RowStatus: &rowStatus})
@@ -64,7 +69,7 @@ func (s *Server) registerLabelRoutes(g *echo.Group) {
}
// We don't allow updating reserved environment label keys. Since its ID is zero, it cannot be updated by default.
- labelKey, err := s.LabelService.PatchLabelKey(ctx, patch)
+ labelKeyRaw, err := s.LabelService.PatchLabelKey(ctx, patch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Label ID not found: %d", id))
@@ -72,6 +77,8 @@ func (s *Server) registerLabelRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch label ID: %v", id)).SetInternal(err)
}
+ // TODO(dragonly): implement composeLabelKey
+ labelKey := labelKeyRaw.ToLabelKey()
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, labelKey); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal label key response: %v", id)).SetInternal(err)
diff --git a/store/label.go b/store/label.go
index d3cac86489..e8f189adc4 100644
--- a/store/label.go
+++ b/store/label.go
@@ -27,7 +27,7 @@ func NewLabelService(logger *zap.Logger, db *DB) *LabelService {
}
// FindLabelKeyList retrieves a list of label keys for labels based on find.
-func (s *LabelService) FindLabelKeyList(ctx context.Context, find *api.LabelKeyFind) ([]*api.LabelKey, error) {
+func (s *LabelService) FindLabelKeyList(ctx context.Context, find *api.LabelKeyFind) ([]*api.LabelKeyRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -46,7 +46,7 @@ func (s *LabelService) FindLabelKeyList(ctx context.Context, find *api.LabelKeyF
return ret, nil
}
-func (s *LabelService) findLabelKeyList(ctx context.Context, tx *sql.Tx, find *api.LabelKeyFind) ([]*api.LabelKey, error) {
+func (s *LabelService) findLabelKeyList(ctx context.Context, tx *sql.Tx, find *api.LabelKeyFind) ([]*api.LabelKeyRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.RowStatus; v != nil {
@@ -68,23 +68,23 @@ func (s *LabelService) findLabelKeyList(ctx context.Context, tx *sql.Tx, find *a
return nil, FormatError(err)
}
// Iterate over result set and deserialize rows into list.
- var ret []*api.LabelKey
- keymap := make(map[string]*api.LabelKey)
+ var labelKeyRawList []*api.LabelKeyRaw
+ keymap := make(map[string]*api.LabelKeyRaw)
for rows.Next() {
- var labelKey api.LabelKey
+ var labelKeyRaw api.LabelKeyRaw
if err := rows.Scan(
- &labelKey.ID,
- &labelKey.CreatorID,
- &labelKey.CreatedTs,
- &labelKey.UpdaterID,
- &labelKey.UpdatedTs,
- &labelKey.Key,
+ &labelKeyRaw.ID,
+ &labelKeyRaw.CreatorID,
+ &labelKeyRaw.CreatedTs,
+ &labelKeyRaw.UpdaterID,
+ &labelKeyRaw.UpdatedTs,
+ &labelKeyRaw.Key,
); err != nil {
return nil, FormatError(err)
}
- ret = append(ret, &labelKey)
- keymap[labelKey.Key] = &labelKey
+ labelKeyRawList = append(labelKeyRawList, &labelKeyRaw)
+ keymap[labelKeyRaw.Key] = &labelKeyRaw
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
@@ -125,7 +125,7 @@ func (s *LabelService) findLabelKeyList(ctx context.Context, tx *sql.Tx, find *a
return nil, FormatError(err)
}
- return ret, nil
+ return labelKeyRawList, nil
}
type labelValueUpsert struct {
@@ -136,25 +136,25 @@ type labelValueUpsert struct {
}
// PatchLabelKey patches a label key.
-func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPatch) (*api.LabelKey, error) {
+func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPatch) (*api.LabelKeyRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- ret, err := s.findLabelKeyList(ctx, tx.PTx, &api.LabelKeyFind{})
+ labelKeyRawList, err := s.findLabelKeyList(ctx, tx.PTx, &api.LabelKeyFind{})
if err != nil {
return nil, err
}
- var labelKey *api.LabelKey
- for _, k := range ret {
- if k.ID == patch.ID {
- labelKey = k
+ var labelKeyRaw *api.LabelKeyRaw
+ for _, raw := range labelKeyRawList {
+ if raw.ID == patch.ID {
+ labelKeyRaw = raw
}
}
- if labelKey == nil {
- return nil, common.Errorf(common.NotFound, fmt.Errorf("label key ID not found: %v", patch.ID))
+ if labelKeyRaw == nil {
+ return nil, common.Errorf(common.NotFound, fmt.Errorf("label key not found with ID %v", patch.ID))
}
// Generate label value upserts.
@@ -164,7 +164,7 @@ func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPat
upserts = append(upserts, labelValueUpsert{
rowStatus: api.Normal,
updaterID: patch.UpdaterID,
- key: labelKey.Key,
+ key: labelKeyRaw.Key,
value: v,
})
}
@@ -173,12 +173,12 @@ func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPat
for _, v := range patch.ValueList {
newValues[v] = true
}
- for _, v := range labelKey.ValueList {
+ for _, v := range labelKeyRaw.ValueList {
if _, ok := newValues[v]; !ok {
upserts = append(upserts, labelValueUpsert{
rowStatus: api.Archived,
updaterID: patch.UpdaterID,
- key: labelKey.Key,
+ key: labelKeyRaw.Key,
value: v,
})
}
@@ -194,8 +194,8 @@ func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPat
return nil, FormatError(err)
}
- labelKey.ValueList = patch.ValueList
- return labelKey, nil
+ labelKeyRaw.ValueList = patch.ValueList
+ return labelKeyRaw, nil
}
func (s *LabelService) upsertLabelValue(ctx context.Context, tx *sql.Tx, upsert labelValueUpsert) error {
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index 753fdc0155..88d1190a62 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -122,7 +122,7 @@ func TestTenant(t *testing.T) {
api.DeploymentConfigUpsert{
ProjectID: project.ID,
},
- deploymentSchdule,
+ deploymentSchedule,
); err != nil {
return err
}
@@ -401,7 +401,7 @@ func TestTenantVCS(t *testing.T) {
api.DeploymentConfigUpsert{
ProjectID: project.ID,
},
- deploymentSchdule,
+ deploymentSchedule,
); err != nil {
return err
}
@@ -632,7 +632,7 @@ func TestTenantDatabaseNameTemplate(t *testing.T) {
api.DeploymentConfigUpsert{
ProjectID: project.ID,
},
- deploymentSchdule,
+ deploymentSchedule,
); err != nil {
return err
}
diff --git a/tests/tests.go b/tests/tests.go
index e28f1dfb24..0a8acb37e2 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -51,7 +51,7 @@ var (
`
backupDump = "CREATE TABLE book (\n\t\tid INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\tname TEXT NULL\n\t);\nINSERT INTO 'book' VALUES ('1', 'byte');\nINSERT INTO 'book' VALUES ('2', NULL);\n\n"
- deploymentSchdule = api.DeploymentSchedule{
+ deploymentSchedule = api.DeploymentSchedule{
Deployments: []*api.Deployment{
{
Name: "Staging stage",