Skip to content

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

feat(tenancy): return label values as part of label API (#179)

spinningbotd1928ff63ed8
diff --git a/api/label.go b/api/label.go
index 8c33af2c60..1a95465b07 100644
--- a/api/label.go
+++ b/api/label.go
@@ -17,7 +17,8 @@ type LabelKey struct {
UpdatedTs int64 `jsonapi:"attr,updatedTs"`
// Domain specific fields
- Key string `jsonapi:"attr,key"`
+ Key string `jsonapi:"attr,key"`
+ ValueList []string `jsonapi:"attr,valueList"`
}
// LabelKeyFind is the find request for label keys.
@@ -45,6 +46,7 @@ type DatabaseLabel struct {
Value string `json:"value"`
}
+// DatabaseLabelFind finds the labels associated with the database.
type DatabaseLabelFind struct {
// Standard fields
ID *int
@@ -54,6 +56,7 @@ type DatabaseLabelFind struct {
DatabaseID *int
}
+// DatabaseLabelUpsert upserts the label associated with the database.
type DatabaseLabelUpsert struct {
// Standard fields
// Value is assigned from the jwt subject field passed by the client.
@@ -70,10 +73,10 @@ 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)
- // FindDatabaseLabels finds all database labels matching the conditions, ascending by key.
- FindDatabaseLabels(ctx context.Context, find *DatabaseLabelFind) ([]*DatabaseLabel, error)
- // SetDatabaseLabels sets a database's labels to new labels.
- SetDatabaseLabels(ctx context.Context, labels []*DatabaseLabel, databaseID int, updaterID int) ([]*DatabaseLabel, error)
+ // FindLabelKeysList finds all available keys for labels.
+ FindLabelKeysList(ctx context.Context, find *LabelKeyFind) ([]*LabelKey, error)
+ // FindDatabaseLabelList finds all database labels matching the conditions, ascending by key.
+ FindDatabaseLabelList(ctx context.Context, find *DatabaseLabelFind) ([]*DatabaseLabel, error)
+ // SetDatabaseLabelList sets a database's labels to new labels.
+ SetDatabaseLabelList(ctx context.Context, labels []*DatabaseLabel, databaseID int, updaterID int) ([]*DatabaseLabel, error)
}
diff --git a/server/database.go b/server/database.go
index f230472099..743351322f 100644
--- a/server/database.go
+++ b/server/database.go
@@ -159,7 +159,7 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
var labels []*api.DatabaseLabel
json.Unmarshal([]byte(*databasePatch.Labels), &labels)
- _, err := s.LabelService.SetDatabaseLabels(ctx, labels, databasePatch.ID, databasePatch.UpdaterID)
+ _, err := s.LabelService.SetDatabaseLabelList(ctx, labels, databasePatch.ID, databasePatch.UpdaterID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to set database labels, database ID: %v", id)).SetInternal(err)
}
@@ -701,7 +701,7 @@ func (s *Server) composeDatabaseRelationship(ctx context.Context, database *api.
}
rowStatus = api.Normal
- labels, err := s.LabelService.FindDatabaseLabels(ctx, &api.DatabaseLabelFind{
+ labels, err := s.LabelService.FindDatabaseLabelList(ctx, &api.DatabaseLabelFind{
DatabaseID: &database.ID,
RowStatus: &rowStatus,
})
diff --git a/server/label.go b/server/label.go
index 11c9b8079c..4b89e002e1 100644
--- a/server/label.go
+++ b/server/label.go
@@ -15,7 +15,7 @@ func (s *Server) registerLabelRoutes(g *echo.Group) {
g.GET("/label", func(c echo.Context) error {
ctx := context.Background()
find := &api.LabelKeyFind{}
- list, err := s.LabelService.FindLabelKeyList(ctx, find)
+ list, err := s.LabelService.FindLabelKeysList(ctx, find)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch label keys").SetInternal(err)
}
diff --git a/store/label.go b/store/label.go
index 7597171995..a463583a88 100644
--- a/store/label.go
+++ b/store/label.go
@@ -2,9 +2,11 @@ package store
import (
"context"
+ "fmt"
"strings"
"github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
"go.uber.org/zap"
)
@@ -23,8 +25,8 @@ func NewLabelService(logger *zap.Logger, db *DB) *LabelService {
return &LabelService{l: logger, db: db}
}
-// 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) {
+// FindLabelKeysList retrieves a list of label keys for labels based on find.
+func (s *LabelService) FindLabelKeysList(ctx context.Context, find *api.LabelKeyFind) ([]*api.LabelKey, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -48,6 +50,7 @@ func (s *LabelService) FindLabelKeyList(ctx context.Context, find *api.LabelKeyF
// Iterate over result set and deserialize rows into list.
var ret []*api.LabelKey
+ keymap := make(map[string]*api.LabelKey)
for rows.Next() {
var labelKey api.LabelKey
if err := rows.Scan(
@@ -62,15 +65,47 @@ func (s *LabelService) FindLabelKeyList(ctx context.Context, find *api.LabelKeyF
}
ret = append(ret, &labelKey)
+ keymap[labelKey.Key] = &labelKey
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
+ // Find key values.
+ valueRows, err := tx.QueryContext(ctx, `
+ SELECT
+ key,
+ value
+ FROM label_value`,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer valueRows.Close()
+
+ for valueRows.Next() {
+ var key, value string
+ if err := valueRows.Scan(
+ &key,
+ &value,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+ labelKey, ok := keymap[key]
+ if !ok {
+ return nil, common.Errorf(common.Internal, fmt.Errorf("label value doesn't have a label key, key %q, value %q", key, value))
+ }
+ labelKey.ValueList = append(labelKey.ValueList, value)
+ }
+ if err := valueRows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
return ret, nil
}
-func (s *LabelService) FindDatabaseLabels(ctx context.Context, find *api.DatabaseLabelFind) ([]*api.DatabaseLabel, error) {
+// FindDatabaseLabelList finds the labels associated with the database.
+func (s *LabelService) FindDatabaseLabelList(ctx context.Context, find *api.DatabaseLabelFind) ([]*api.DatabaseLabel, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -199,8 +234,9 @@ func (s *LabelService) upsertDatabaseLabel(ctx context.Context, tx *Tx, upsert *
return &databaseLabel, nil
}
-func (s *LabelService) SetDatabaseLabels(ctx context.Context, labels []*api.DatabaseLabel, databaseID int, updaterID int) ([]*api.DatabaseLabel, error) {
- oldLabels, err := s.FindDatabaseLabels(ctx, &api.DatabaseLabelFind{
+// SetDatabaseLabelList sets the labels for a database.
+func (s *LabelService) SetDatabaseLabelList(ctx context.Context, labels []*api.DatabaseLabel, databaseID int, updaterID int) ([]*api.DatabaseLabel, error) {
+ oldLabels, err := s.FindDatabaseLabelList(ctx, &api.DatabaseLabelFind{
DatabaseID: &databaseID,
})
if err != nil {