Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: add DatabaseLabelRaw to represent store model for DatabaseLabel (#906)
Yilong Li47392cd1fe98
diff --git a/api/label.go b/api/label.goindex cf4b7d5946..974b2e6442 100644--- a/api/label.go+++ b/api/label.go@@ -68,6 +68,48 @@ func (patch *LabelKeyPatch) Validate() error {return nil}+// DatabaseLabelRaw is the store model for an DatabaseLabel.+// Fields have exactly the same meanings as DatabaseLabel.+type DatabaseLabelRaw struct {+ ID int++ // Standard fields+ RowStatus RowStatus+ CreatorID int+ CreatedTs int64+ UpdaterID int+ UpdatedTs int64++ // Related fields+ DatabaseID int+ Key string++ // Domain specific fields+ Value string+}++// ToDatabaseLabel creates an instance of DatabaseLabel based on the DatabaseLabelRaw.+// This is intended to be called when we need to compose an DatabaseLabel relationship.+func (raw *DatabaseLabelRaw) ToDatabaseLabel() *DatabaseLabel {+ return &DatabaseLabel{+ ID: raw.ID,++ // Standard fields+ RowStatus: raw.RowStatus,+ CreatorID: raw.CreatorID,+ CreatedTs: raw.CreatedTs,+ UpdaterID: raw.UpdaterID,+ UpdatedTs: raw.UpdatedTs,++ // Related fields+ DatabaseID: raw.DatabaseID,+ Key: raw.Key,++ // Domain specific fields+ Value: raw.Value,+ }+}+// DatabaseLabel is the label associated with a database.type DatabaseLabel struct {ID int `json:"-"`@@ -121,7 +163,7 @@ type LabelService interface {// 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)+ FindDatabaseLabelList(ctx context.Context, find *DatabaseLabelFind) ([]*DatabaseLabelRaw, 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.goindex f9c8877e24..de51aa0509 100644--- a/server/database.go+++ b/server/database.go@@ -894,6 +894,8 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}func (s *Server) setDatabaseLabels(ctx context.Context, labelsJSON string, database *api.Database, project *api.Project, updaterID int, validateOnly bool) error {+ // NOTE: this is a partially filled DatabaseLabel+ // TODO(dragonly): should we make it cleaner?var labels []*api.DatabaseLabelif err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {return err@@ -1039,13 +1041,19 @@ func (s *Server) composeDatabaseRelationship(ctx context.Context, raw *api.Datab}rowStatus = api.Normal- labelList, err := s.LabelService.FindDatabaseLabelList(ctx, &api.DatabaseLabelFind{+ labelRawList, err := s.LabelService.FindDatabaseLabelList(ctx, &api.DatabaseLabelFind{DatabaseID: &db.ID,RowStatus: &rowStatus,})if err != nil {return nil, err}+ // TODO(dragonly): seems like we do not need to composed this.+ // need redesign, e.g., extract the kv part which is only in memory, and the relations which are in the database.+ var labelList []*api.DatabaseLabel+ for _, raw := range labelRawList {+ labelList = append(labelList, raw.ToDatabaseLabel())+ }// Since tenants are identified by labels in deployment config, we need an environment// label to identify tenants from different environment in a schema update deployment.diff --git a/store/label.go b/store/label.goindex 0aeae5ad6c..d3cac86489 100644--- a/store/label.go+++ b/store/label.go@@ -226,7 +226,7 @@ func (s *LabelService) upsertLabelValue(ctx context.Context, tx *sql.Tx, upsert}// FindDatabaseLabelList finds the labels associated with the database.-func (s *LabelService) FindDatabaseLabelList(ctx context.Context, find *api.DatabaseLabelFind) ([]*api.DatabaseLabel, error) {+func (s *LabelService) FindDatabaseLabelList(ctx context.Context, find *api.DatabaseLabelFind) ([]*api.DatabaseLabelRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -245,7 +245,7 @@ func (s *LabelService) FindDatabaseLabelList(ctx context.Context, find *api.Datareturn databaseLabelList, nil}-func (s *LabelService) findDatabaseLabels(ctx context.Context, tx *sql.Tx, find *api.DatabaseLabelFind) ([]*api.DatabaseLabel, error) {+func (s *LabelService) findDatabaseLabels(ctx context.Context, tx *sql.Tx, find *api.DatabaseLabelFind) ([]*api.DatabaseLabelRaw, error) {// Build WHERE clause.where, args := []string{"1 = 1"}, []interface{}{}if v := find.ID; v != nil {@@ -278,24 +278,24 @@ func (s *LabelService) findDatabaseLabels(ctx context.Context, tx *sql.Tx, finddefer rows.Close()// Iterate over result set and deserialize rows into list.- var ret []*api.DatabaseLabel+ var ret []*api.DatabaseLabelRawfor rows.Next() {- var databaseLabel api.DatabaseLabel+ var dbLabelRaw api.DatabaseLabelRawif err := rows.Scan(- &databaseLabel.ID,- &databaseLabel.RowStatus,- &databaseLabel.CreatorID,- &databaseLabel.CreatedTs,- &databaseLabel.UpdaterID,- &databaseLabel.UpdatedTs,- &databaseLabel.DatabaseID,- &databaseLabel.Key,- &databaseLabel.Value,+ &dbLabelRaw.ID,+ &dbLabelRaw.RowStatus,+ &dbLabelRaw.CreatorID,+ &dbLabelRaw.CreatedTs,+ &dbLabelRaw.UpdaterID,+ &dbLabelRaw.UpdatedTs,+ &dbLabelRaw.DatabaseID,+ &dbLabelRaw.Key,+ &dbLabelRaw.Value,); err != nil {return nil, FormatError(err)}- ret = append(ret, &databaseLabel)+ ret = append(ret, &dbLabelRaw)}if err := rows.Err(); err != nil {return nil, FormatError(err)@@ -304,7 +304,7 @@ func (s *LabelService) findDatabaseLabels(ctx context.Context, tx *sql.Tx, findreturn ret, nil}-func (s *LabelService) upsertDatabaseLabel(ctx context.Context, tx *sql.Tx, upsert *api.DatabaseLabelUpsert) (*api.DatabaseLabel, error) {+func (s *LabelService) upsertDatabaseLabel(ctx context.Context, tx *sql.Tx, upsert *api.DatabaseLabelUpsert) (*api.DatabaseLabelRaw, error) {// Upsert row into db_labelrow, err := tx.QueryContext(ctx, `INSERT INTO db_label (@@ -337,27 +337,27 @@ func (s *LabelService) upsertDatabaseLabel(ctx context.Context, tx *sql.Tx, upsedefer row.Close()row.Next()- var databaseLabel api.DatabaseLabel+ var dbLabelRaw api.DatabaseLabelRawif err := row.Scan(- &databaseLabel.ID,- &databaseLabel.RowStatus,- &databaseLabel.CreatorID,- &databaseLabel.CreatedTs,- &databaseLabel.UpdaterID,- &databaseLabel.UpdatedTs,- &databaseLabel.DatabaseID,- &databaseLabel.Key,- &databaseLabel.Value,+ &dbLabelRaw.ID,+ &dbLabelRaw.RowStatus,+ &dbLabelRaw.CreatorID,+ &dbLabelRaw.CreatedTs,+ &dbLabelRaw.UpdaterID,+ &dbLabelRaw.UpdatedTs,+ &dbLabelRaw.DatabaseID,+ &dbLabelRaw.Key,+ &dbLabelRaw.Value,); err != nil {return nil, FormatError(err)}- return &databaseLabel, nil+ return &dbLabelRaw, nil}// SetDatabaseLabelList sets the labels for a database.func (s *LabelService) SetDatabaseLabelList(ctx context.Context, labelList []*api.DatabaseLabel, databaseID int, updaterID int) ([]*api.DatabaseLabel, error) {- oldLabelList, err := s.FindDatabaseLabelList(ctx, &api.DatabaseLabelFind{+ oldLabelRawList, err := s.FindDatabaseLabelList(ctx, &api.DatabaseLabelFind{DatabaseID: &databaseID,})if err != nil {@@ -372,18 +372,18 @@ func (s *LabelService) SetDatabaseLabelList(ctx context.Context, labelList []*apvar ret []*api.DatabaseLabel- for _, oldLabel := range oldLabelList {+ for _, oldLabelRaw := range oldLabelRawList {// Archive all old labels// Skip environment label key because we don't store it.- if oldLabel.Key == api.EnvironmentKeyName {+ if oldLabelRaw.Key == api.EnvironmentKeyName {continue}upsert := &api.DatabaseLabelUpsert{UpdaterID: updaterID,RowStatus: api.Archived,DatabaseID: databaseID,- Key: oldLabel.Key,- Value: oldLabel.Value,+ Key: oldLabelRaw.Key,+ Value: oldLabelRaw.Value,}if _, err := s.upsertDatabaseLabel(ctx, tx.PTx, upsert); err != nil {return nil, err@@ -403,11 +403,12 @@ func (s *LabelService) SetDatabaseLabelList(ctx context.Context, labelList []*apKey: label.Key,Value: label.Value,}- label, err := s.upsertDatabaseLabel(ctx, tx.PTx, upsert)+ labelRaw, err := s.upsertDatabaseLabel(ctx, tx.PTx, upsert)if err != nil {return nil, err}- ret = append(ret, label)+ // TODO(dragonly): implement composeDatabaseLabelRelation+ ret = append(ret, labelRaw.ToDatabaseLabel())}if err := tx.PTx.Commit(); err != nil {