Skip to content

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

feat(tenancy): implement patchLabelKey storage for setting values for a label key (#181)

spinningbotd8ed5f0f5a07
diff --git a/api/label.go b/api/label.go
index 1a95465b07..575fb5597b 100644
--- a/api/label.go
+++ b/api/label.go
@@ -25,6 +25,21 @@ type LabelKey struct {
type LabelKeyFind struct {
}
+// LabelKeyPatch is the message to patch a label key.
+type LabelKeyPatch struct {
+ ID int `jsonapi:"primary,labelKeyPatch"`
+
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ // CreatorID is the ID of the creator.
+ UpdaterID int
+
+ // Related fields
+
+ // Domain specific fields
+ ValueList []string `jsonapi:"attr,valueList"`
+}
+
// DatabaseLabel is the label associated with a database.
type DatabaseLabel struct {
ID int `json:"-"`
@@ -73,8 +88,10 @@ type DatabaseLabelUpsert struct {
// LabelService is the service for labels.
type LabelService interface {
- // FindLabelKeysList finds all available keys for labels.
- FindLabelKeysList(ctx context.Context, find *LabelKeyFind) ([]*LabelKey, error)
+ // FindLabelKeyList finds all available keys for labels.
+ FindLabelKeyList(ctx context.Context, find *LabelKeyFind) ([]*LabelKey, error)
+ // PatchLabelKey patches a label key.
+ PatchLabelKey(ctx context.Context, patch *LabelKeyPatch) (*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.
diff --git a/server/label.go b/server/label.go
index 4b89e002e1..11c9b8079c 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.FindLabelKeysList(ctx, find)
+ list, err := s.LabelService.FindLabelKeyList(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 a463583a88..1e5dedfcb1 100644
--- a/store/label.go
+++ b/store/label.go
@@ -25,14 +25,27 @@ func NewLabelService(logger *zap.Logger, db *DB) *LabelService {
return &LabelService{l: logger, db: db}
}
-// 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) {
+// 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) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.Rollback()
+ ret, err := s.findLabelKeyList(ctx, tx, find)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return ret, nil
+}
+
+func (s *LabelService) findLabelKeyList(ctx context.Context, tx *Tx, find *api.LabelKeyFind) ([]*api.LabelKey, error) {
rows, err := tx.QueryContext(ctx, `
SELECT
id,
@@ -104,6 +117,102 @@ func (s *LabelService) FindLabelKeysList(ctx context.Context, find *api.LabelKey
return ret, nil
}
+type labelValueUpsert struct {
+ rowStatus api.RowStatus
+ updaterID int
+ key string
+ value string
+}
+
+// PatchLabelKey patches a label key.
+func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPatch) (*api.LabelKey, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ ret, err := s.findLabelKeyList(ctx, tx, &api.LabelKeyFind{})
+ if err != nil {
+ return nil, err
+ }
+ var labelKey *api.LabelKey
+ for _, k := range ret {
+ if k.ID == patch.ID {
+ labelKey = k
+ }
+ }
+ if labelKey == nil {
+ return nil, common.Errorf(common.NotFound, fmt.Errorf("label key ID not found: %v", patch.ID))
+ }
+
+ // Generate label value upserts.
+ var upserts []labelValueUpsert
+ // Add all new values.
+ for _, v := range patch.ValueList {
+ upserts = append(upserts, labelValueUpsert{
+ rowStatus: api.Normal,
+ value: v,
+ })
+ }
+ // Archive old values that are not in new values.
+ newValues := make(map[string]bool)
+ for _, v := range patch.ValueList {
+ newValues[v] = true
+ }
+ for _, v := range labelKey.ValueList {
+ if _, ok := newValues[v]; !ok {
+ upserts = append(upserts, labelValueUpsert{
+ rowStatus: api.Archived,
+ value: v,
+ })
+ }
+ }
+
+ for _, upsert := range upserts {
+ if err := s.upsertLabelValue(ctx, tx, upsert); err != nil {
+ return nil, err
+ }
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ labelKey.ValueList = patch.ValueList
+ return labelKey, nil
+}
+
+func (s *LabelService) upsertLabelValue(ctx context.Context, tx *Tx, upsert labelValueUpsert) error {
+ // Upsert row into label_value
+ row, err := tx.QueryContext(ctx, `
+ INSERT INTO label_value (
+ row_status,
+ creator_id,
+ updater_id,
+ key,
+ value
+ )
+ VALUES (?, ?, ?, ?, ?)
+ ON CONFLICT(key, value) DO UPDATE SET
+ row_status = excluded.row_status,
+ creator_id = excluded.creator_id,
+ updater_id = excluded.updater_id
+ `,
+ upsert.rowStatus,
+ upsert.updaterID,
+ upsert.updaterID,
+ upsert.key,
+ upsert.value,
+ )
+ if err != nil {
+ return FormatError(err)
+ }
+ defer row.Close()
+
+ return nil
+}
+
// 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)