Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(tenancy): implement patch label key API (#184)
spinningbotfeecc859c711
diff --git a/api/label.go b/api/label.goindex 575fb5597b..72b39585a3 100644--- a/api/label.go+++ b/api/label.go@@ -23,11 +23,13 @@ type LabelKey struct {// LabelKeyFind is the find request for label keys.type LabelKeyFind struct {+ // RowStatus is the row status filter.+ RowStatus *RowStatus}// LabelKeyPatch is the message to patch a label key.type LabelKeyPatch struct {- ID int `jsonapi:"primary,labelKeyPatch"`+ ID int// Standard fields// Value is assigned from the jwt subject field passed by the client.diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csvindex 482cbcca31..831440a714 100644--- a/server/acl_casbin_policy_dba.csv+++ b/server/acl_casbin_policy_dba.csv@@ -78,3 +78,4 @@ p, DBA, /plan, GETp, DBA, /plan, PATCHp, DBA, /setting, GETp, DBA, /label, GET+p, DBA, /label/{id}, PATCHdiff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csvindex 06e8503047..d72c2f4442 100644--- a/server/acl_casbin_policy_owner.csv+++ b/server/acl_casbin_policy_owner.csv@@ -84,3 +84,4 @@ p, OWNER, /plan, PATCHp, OWNER, /setting, GETp, OWNER, /setting/{name}, PATCHp, OWNER, /label, GET+p, OWNER, /label/{id}, PATCHdiff --git a/server/label.go b/server/label.goindex 11c9b8079c..fdbbfa2ee1 100644--- a/server/label.go+++ b/server/label.go@@ -2,9 +2,12 @@ package serverimport ("context"+ "fmt""net/http"+ "strconv""github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common""github.com/google/jsonapi""github.com/labstack/echo/v4")@@ -14,7 +17,10 @@ var ()func (s *Server) registerLabelRoutes(g *echo.Group) {g.GET("/label", func(c echo.Context) error {ctx := context.Background()- find := &api.LabelKeyFind{}+ rowStatus := api.Normal+ find := &api.LabelKeyFind{+ RowStatus: &rowStatus,+ }list, err := s.LabelService.FindLabelKeyList(ctx, find)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch label keys").SetInternal(err)@@ -26,4 +32,34 @@ func (s *Server) registerLabelRoutes(g *echo.Group) {}return nil})++ g.PATCH("/label/:id", func(c echo.Context) error {+ ctx := context.Background()+ id, err := strconv.Atoi(c.Param("id"))+ if err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("id is not a number: %s", c.Param("id"))).SetInternal(err)+ }++ patch := &api.LabelKeyPatch{+ ID: id,+ UpdaterID: c.Get(getPrincipalIDContextKey()).(int),+ }+ if err := jsonapi.UnmarshalPayload(c.Request().Body, patch); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch label key request").SetInternal(err)+ }++ labelKey, 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))+ }+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch label ID: %v", id)).SetInternal(err)+ }++ 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)+ }+ return nil+ })}diff --git a/store/label.go b/store/label.goindex 1e5dedfcb1..5377aa5af1 100644--- a/store/label.go+++ b/store/label.go@@ -46,6 +46,11 @@ func (s *LabelService) FindLabelKeyList(ctx context.Context, find *api.LabelKeyF}func (s *LabelService) findLabelKeyList(ctx context.Context, tx *Tx, find *api.LabelKeyFind) ([]*api.LabelKey, error) {+ // Build WHERE clause.+ where, args := []string{"1 = 1"}, []interface{}{}+ if v := find.RowStatus; v != nil {+ where, args = append(where, "row_status = ?"), append(args, *v)+ }rows, err := tx.QueryContext(ctx, `SELECTid,@@ -54,7 +59,9 @@ func (s *LabelService) findLabelKeyList(ctx context.Context, tx *Tx, find *api.Lupdater_id,updated_ts,key- FROM label_key`,+ FROM label_key+ WHERE `+strings.Join(where, " AND "),+ args...,)if err != nil {return nil, FormatError(err)@@ -89,7 +96,9 @@ func (s *LabelService) findLabelKeyList(ctx context.Context, tx *Tx, find *api.LSELECTkey,value- FROM label_value`,+ FROM label_value+ WHERE `+strings.Join(where, " AND "),+ args...,)if err != nil {return nil, FormatError(err)@@ -152,6 +161,8 @@ func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPatfor _, v := range patch.ValueList {upserts = append(upserts, labelValueUpsert{rowStatus: api.Normal,+ updaterID: patch.UpdaterID,+ key: labelKey.Key,value: v,})}@@ -164,6 +175,8 @@ func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPatif _, ok := newValues[v]; !ok {upserts = append(upserts, labelValueUpsert{rowStatus: api.Archived,+ updaterID: patch.UpdaterID,+ key: labelKey.Key,value: v,})}@@ -185,7 +198,7 @@ func (s *LabelService) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPatfunc (s *LabelService) upsertLabelValue(ctx context.Context, tx *Tx, upsert labelValueUpsert) error {// Upsert row into label_value- row, err := tx.QueryContext(ctx, `+ if _, err := tx.ExecContext(ctx, `INSERT INTO label_value (row_status,creator_id,@@ -204,12 +217,9 @@ func (s *LabelService) upsertLabelValue(ctx context.Context, tx *Tx, upsert labeupsert.updaterID,upsert.key,upsert.value,- )- if err != nil {+ ); err != nil {return FormatError(err)}- defer row.Close()-return nil}