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 SettingRaw to represent store model for Setting (#916)
Yilong Li54e14cb6563a
diff --git a/api/setting.go b/api/setting.goindex 6bf2814108..dc2e9523e3 100644--- a/api/setting.go+++ b/api/setting.go@@ -13,6 +13,42 @@ const (SettingAuthSecret SettingName = "bb.auth.secret")+// SettingRaw is the store model for an Setting.+// Fields have exactly the same meanings as Setting.+type SettingRaw struct {+ ID int++ // Standard fields+ CreatorID int+ CreatedTs int64+ UpdaterID int+ UpdatedTs int64++ // Domain specific fields+ Name SettingName+ Value string+ Description string+}++// ToSetting creates an instance of Setting based on the SettingRaw.+// This is intended to be called when we need to compose an Setting relationship.+func (raw *SettingRaw) ToSetting() *Setting {+ return &Setting{+ ID: raw.ID,++ // Standard fields+ CreatorID: raw.CreatorID,+ CreatedTs: raw.CreatedTs,+ UpdaterID: raw.UpdaterID,+ UpdatedTs: raw.UpdatedTs,++ // Domain specific fields+ Name: raw.Name,+ Value: raw.Value,+ Description: raw.Description,+ }+}+// Setting is the API message for a setting.type Setting struct {ID int `jsonapi:"primary,setting"`@@ -65,8 +101,8 @@ func (find *SettingFind) String() string {// SettingService is the service for settings.type SettingService interface {// Creates new setting and returns if not exist, returns the existing one otherwise.- CreateSettingIfNotExist(ctx context.Context, create *SettingCreate) (*Setting, error)- FindSettingList(ctx context.Context, find *SettingFind) ([]*Setting, error)- FindSetting(ctx context.Context, find *SettingFind) (*Setting, error)- PatchSetting(ctx context.Context, patch *SettingPatch) (*Setting, error)+ CreateSettingIfNotExist(ctx context.Context, create *SettingCreate) (*SettingRaw, error)+ FindSettingList(ctx context.Context, find *SettingFind) ([]*SettingRaw, error)+ FindSetting(ctx context.Context, find *SettingFind) (*SettingRaw, error)+ PatchSetting(ctx context.Context, patch *SettingPatch) (*SettingRaw, error)}diff --git a/server/setting.go b/server/setting.goindex 03eb60b448..336ac7cb81 100644--- a/server/setting.go+++ b/server/setting.go@@ -20,10 +20,18 @@ func (s *Server) registerSettingRoutes(g *echo.Group) {g.GET("/setting", func(c echo.Context) error {ctx := context.Background()find := &api.SettingFind{}- settingList, err := s.SettingService.FindSettingList(ctx, find)+ settingRawList, err := s.SettingService.FindSettingList(ctx, find)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch setting list").SetInternal(err)}+ var settingList []*api.Setting+ for _, raw := range settingRawList {+ setting, err := s.composeSettingRelationship(ctx, raw)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose setting relationship for ID %d", raw.ID)).SetInternal(err)+ }+ settingList = append(settingList, setting)+ }filteredList := []*api.Setting{}for _, setting := range settingList {@@ -35,12 +43,6 @@ func (s *Server) registerSettingRoutes(g *echo.Group) {}}- for _, setting := range filteredList {- if err := s.composeSettingRelationship(ctx, setting); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch setting relationship: %v", setting.Name)).SetInternal(err)- }- }-c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, filteredList); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal project list response").SetInternal(err)@@ -74,18 +76,20 @@ func (s *Server) registerSettingRoutes(g *echo.Group) {})}-func (s *Server) composeSettingRelationship(ctx context.Context, setting *api.Setting) error {- var err error+func (s *Server) composeSettingRelationship(ctx context.Context, raw *api.SettingRaw) (*api.Setting, error) {+ setting := raw.ToSetting()- setting.Creator, err = s.composePrincipalByID(ctx, setting.CreatorID)+ creator, err := s.composePrincipalByID(ctx, setting.CreatorID)if err != nil {- return err+ return nil, err}+ setting.Creator = creator- setting.Updater, err = s.composePrincipalByID(ctx, setting.UpdaterID)+ updater, err := s.composePrincipalByID(ctx, setting.UpdaterID)if err != nil {- return err+ return nil, err}+ setting.Updater = updater- return nil+ return setting, nil}diff --git a/store/setting.go b/store/setting.goindex 5da5acb876..09fde60c4a 100644--- a/store/setting.go+++ b/store/setting.go@@ -27,7 +27,7 @@ func NewSettingService(logger *zap.Logger, db *DB) *SettingService {}// CreateSettingIfNotExist creates a new setting only if the named setting does not exist.-func (s *SettingService) CreateSettingIfNotExist(ctx context.Context, create *api.SettingCreate) (*api.Setting, error) {+func (s *SettingService) CreateSettingIfNotExist(ctx context.Context, create *api.SettingCreate) (*api.SettingRaw, error) {// We do a find followed by a create if NOT found. Though SQLite supports UPSERT ON CONFLICT DO NOTHING syntax, it doesn't// support RETURNING in such case. So we have to use separate SELECT and INSERT anyway.find := &api.SettingFind{@@ -60,7 +60,7 @@ func (s *SettingService) CreateSettingIfNotExist(ctx context.Context, create *ap}// FindSettingList retrieves a list of settings based on find.-func (s *SettingService) FindSettingList(ctx context.Context, find *api.SettingFind) ([]*api.Setting, error) {+func (s *SettingService) FindSettingList(ctx context.Context, find *api.SettingFind) ([]*api.SettingRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -76,7 +76,7 @@ func (s *SettingService) FindSettingList(ctx context.Context, find *api.SettingF// FindSetting retrieves a single setting based on find.// Returns ECONFLICT if finding more than 1 matching records.-func (s *SettingService) FindSetting(ctx context.Context, find *api.SettingFind) (*api.Setting, error) {+func (s *SettingService) FindSetting(ctx context.Context, find *api.SettingFind) (*api.SettingRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -98,7 +98,7 @@ func (s *SettingService) FindSetting(ctx context.Context, find *api.SettingFind)// PatchSetting updates an existing setting by name.// Returns ENOTFOUND if setting does not exist.-func (s *SettingService) PatchSetting(ctx context.Context, patch *api.SettingPatch) (*api.Setting, error) {+func (s *SettingService) PatchSetting(ctx context.Context, patch *api.SettingPatch) (*api.SettingRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -118,7 +118,7 @@ func (s *SettingService) PatchSetting(ctx context.Context, patch *api.SettingPat}// createSetting creates a new setting.-func createSetting(ctx context.Context, tx *sql.Tx, create *api.SettingCreate) (*api.Setting, error) {+func createSetting(ctx context.Context, tx *sql.Tx, create *api.SettingCreate) (*api.SettingRaw, error) {// Insert row into database.row, err := tx.QueryContext(ctx, `INSERT INTO setting (@@ -144,19 +144,19 @@ func createSetting(ctx context.Context, tx *sql.Tx, create *api.SettingCreate) (defer row.Close()row.Next()- var setting api.Setting+ var settingRaw api.SettingRawif err := row.Scan(- &setting.Name,- &setting.Value,- &setting.Description,+ &settingRaw.Name,+ &settingRaw.Value,+ &settingRaw.Description,); err != nil {return nil, FormatError(err)}- return &setting, nil+ return &settingRaw, nil}-func findSettingList(ctx context.Context, tx *sql.Tx, find *api.SettingFind) ([]*api.Setting, error) {+func findSettingList(ctx context.Context, tx *sql.Tx, find *api.SettingFind) ([]*api.SettingRaw, error) {// Build WHERE clause.where, args := []string{"1 = 1"}, []interface{}{}if v := find.Name; v != nil {@@ -181,33 +181,33 @@ func findSettingList(ctx context.Context, tx *sql.Tx, find *api.SettingFind) ([]}defer rows.Close()- // Iterate over result set and deserialize rows into settingList.- var settingList []*api.Setting+ // Iterate over result set and deserialize rows into settingRawList.+ var settingRawList []*api.SettingRawfor rows.Next() {- var setting api.Setting+ var settingRaw api.SettingRawif err := rows.Scan(- &setting.CreatorID,- &setting.CreatedTs,- &setting.UpdaterID,- &setting.UpdatedTs,- &setting.Name,- &setting.Value,- &setting.Description,+ &settingRaw.CreatorID,+ &settingRaw.CreatedTs,+ &settingRaw.UpdaterID,+ &settingRaw.UpdatedTs,+ &settingRaw.Name,+ &settingRaw.Value,+ &settingRaw.Description,); err != nil {return nil, FormatError(err)}- settingList = append(settingList, &setting)+ settingRawList = append(settingRawList, &settingRaw)}if err := rows.Err(); err != nil {return nil, FormatError(err)}- return settingList, nil+ return settingRawList, nil}// patchSetting updates a setting by name. Returns the new state of the setting after update.-func patchSetting(ctx context.Context, tx *sql.Tx, patch *api.SettingPatch) (*api.Setting, error) {+func patchSetting(ctx context.Context, tx *sql.Tx, patch *api.SettingPatch) (*api.SettingRaw, error) {// Build UPDATE clause.set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}set, args = append(set, "value = $2"), append(args, patch.Value)@@ -229,20 +229,20 @@ func patchSetting(ctx context.Context, tx *sql.Tx, patch *api.SettingPatch) (*apdefer row.Close()if row.Next() {- var setting api.Setting+ var settingRaw api.SettingRawif err := row.Scan(- &setting.CreatorID,- &setting.CreatedTs,- &setting.UpdaterID,- &setting.UpdatedTs,- &setting.Name,- &setting.Value,- &setting.Description,+ &settingRaw.CreatorID,+ &settingRaw.CreatedTs,+ &settingRaw.UpdaterID,+ &settingRaw.UpdatedTs,+ &settingRaw.Name,+ &settingRaw.Value,+ &settingRaw.Description,); err != nil {return nil, FormatError(err)}- return &setting, nil+ return &settingRaw, nil}return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("setting not found: %s", patch.Name)}