Skip to content

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

refactor: decouple api/service/store packages for DeploymentConfig (#1195)

Yilong Li73b817e1e5de
diff --git a/api/deploy.go b/api/deploy.go
index 33069693a2..428613c681 100644
--- a/api/deploy.go
+++ b/api/deploy.go
@@ -1,7 +1,6 @@
package api
import (
- "context"
"encoding/json"
"fmt"
@@ -101,14 +100,6 @@ type DeploymentConfigUpsert struct {
Payload string `jsonapi:"attr,payload"`
}
-// DeploymentConfigService is the service for deployment configurations.
-type DeploymentConfigService interface {
- // FindDeploymentConfig finds the deployment configuration in a project.
- FindDeploymentConfig(ctx context.Context, find *DeploymentConfigFind) (*DeploymentConfig, error)
- // UpsertDeploymentConfig upserts a deployment configuration to a project.
- UpsertDeploymentConfig(ctx context.Context, upsert *DeploymentConfigUpsert) (*DeploymentConfig, error)
-}
-
// ValidateAndGetDeploymentSchedule validates and returns the deployment schedule.
// Note: this validation only checks whether the payloads is a valid json, however, invalid field name errors are ignored.
func ValidateAndGetDeploymentSchedule(payload string) (*DeploymentSchedule, error) {
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 740efebaa1..f7705720d9 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -455,7 +455,6 @@ func (m *Main) Run(ctx context.Context) error {
s.InstanceUserService = store.NewInstanceUserService(m.l, db)
s.ColumnService = store.NewColumnService(m.l, db)
s.IndexService = store.NewIndexService(m.l, db)
- s.DeploymentConfigService = store.NewDeploymentConfigService(m.l, db)
s.SheetService = store.NewSheetService(m.l, db)
s.ActivityManager = server.NewActivityManager(s, storeInstance)
diff --git a/server/issue.go b/server/issue.go
index 806283adaf..2653bdb606 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -1124,9 +1124,7 @@ func (s *Server) postInboxIssueActivity(ctx context.Context, issue *api.Issue, a
}
func (s *Server) getTenantDatabaseMatrix(ctx context.Context, projectID int, dbNameTemplate string, dbList []*api.Database, baseDatabaseName string) ([]*api.Deployment, [][]*api.Database, error) {
- deployConfig, err := s.DeploymentConfigService.FindDeploymentConfig(ctx, &api.DeploymentConfigFind{
- ProjectID: &projectID,
- })
+ deployConfig, err := s.store.GetDeploymentConfigByProjectID(ctx, projectID)
if err != nil {
return nil, nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch deployment config for project ID: %v", projectID)).SetInternal(err)
}
diff --git a/server/project.go b/server/project.go
index 7f05e8a741..e28145c5c4 100644
--- a/server/project.go
+++ b/server/project.go
@@ -499,13 +499,10 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
deploymentConfigUpsert.ProjectID = id
- deploymentConfig, err := s.DeploymentConfigService.UpsertDeploymentConfig(ctx, deploymentConfigUpsert)
+ deploymentConfig, err := s.store.UpsertDeploymentConfig(ctx, deploymentConfigUpsert)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set deployment configuration").SetInternal(err)
}
- if err := s.composeDeploymentConfigRelationship(ctx, deploymentConfig); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose deployment configuration relationship").SetInternal(err)
- }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, deploymentConfig); err != nil {
@@ -529,10 +526,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project not found with ID[%d]", id))
}
- deploymentConfigFind := &api.DeploymentConfigFind{
- ProjectID: &id,
- }
- deploymentConfig, err := s.DeploymentConfigService.FindDeploymentConfig(ctx, deploymentConfigFind)
+ deploymentConfig, err := s.store.GetDeploymentConfigByProjectID(ctx, id)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get deployment configuration for project id: %d", id)).SetInternal(err)
}
@@ -540,10 +534,6 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
// We should return empty deployment config when it doesn't exist.
if deploymentConfig == nil {
deploymentConfig = &api.DeploymentConfig{}
- } else {
- if err := s.composeDeploymentConfigRelationship(ctx, deploymentConfig); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose deployment configuration relationship").SetInternal(err)
- }
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -554,23 +544,6 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
})
}
-func (s *Server) composeDeploymentConfigRelationship(ctx context.Context, deploymentConfig *api.DeploymentConfig) error {
- var err error
- deploymentConfig.Creator, err = s.store.GetPrincipalByID(ctx, deploymentConfig.CreatorID)
- if err != nil {
- return err
- }
- deploymentConfig.Updater, err = s.store.GetPrincipalByID(ctx, deploymentConfig.UpdaterID)
- if err != nil {
- return err
- }
- deploymentConfig.Project, err = s.store.GetProjectByID(ctx, deploymentConfig.ProjectID)
- if err != nil {
- return err
- }
- return nil
-}
-
// refreshToken is a token refresher that stores the latest access token configuration to repository.
func (s *Server) refreshToken(ctx context.Context, repositoryID int) common.TokenRefresher {
return func(token, refreshToken string, expiresTs int64) error {
diff --git a/server/server.go b/server/server.go
index d6845acd92..5e5beaf89b 100644
--- a/server/server.go
+++ b/server/server.go
@@ -37,12 +37,11 @@ type Server struct {
ActivityManager *ActivityManager
- InstanceUserService api.InstanceUserService
- ColumnService api.ColumnService
- IndexService api.IndexService
- DeploymentConfigService api.DeploymentConfigService
- LicenseService enterprise.LicenseService
- SheetService api.SheetService
+ InstanceUserService api.InstanceUserService
+ ColumnService api.ColumnService
+ IndexService api.IndexService
+ LicenseService enterprise.LicenseService
+ SheetService api.SheetService
e *echo.Echo
diff --git a/store/deploy.go b/store/deploy.go
index bec5b31ae3..88afab1bd1 100644
--- a/store/deploy.go
+++ b/store/deploy.go
@@ -8,26 +8,132 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "go.uber.org/zap"
)
-var (
- _ api.DeploymentConfigService = (*DeploymentConfigService)(nil)
-)
+// deploymentConfigRaw is the store model for an DeploymentConfig.
+// Fields have exactly the same meanings as DeploymentConfig.
+type deploymentConfigRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ ProjectID int
-// DeploymentConfigService represents a service for managing deployment configurations.
-type DeploymentConfigService struct {
- l *zap.Logger
- db *DB
+ // Domain specific fields
+ Name string
+ Payload string
}
-// NewDeploymentConfigService returns a new instance of DeploymentConfigService.
-func NewDeploymentConfigService(logger *zap.Logger, db *DB) *DeploymentConfigService {
- return &DeploymentConfigService{l: logger, db: db}
+// toDeploymentConfig creates an instance of DeploymentConfig based on the deploymentConfigRaw.
+// This is intended to be called when we need to compose an DeploymentConfig relationship.
+func (raw *deploymentConfigRaw) toDeploymentConfig() *api.DeploymentConfig {
+ return &api.DeploymentConfig{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ ProjectID: raw.ProjectID,
+
+ // Domain specific fields
+ Name: raw.Name,
+ Payload: raw.Payload,
+ }
+}
+
+// GetDeploymentConfigByProjectID gets an instance of DeploymentConfig
+func (s *Store) GetDeploymentConfigByProjectID(ctx context.Context, projectID int) (*api.DeploymentConfig, error) {
+ deploymentConfigRaw, err := s.getDeploymentConfigImpl(ctx, &api.DeploymentConfigFind{ProjectID: &projectID})
+ if err != nil {
+ return nil, fmt.Errorf("failed to get DeploymentConfig with projectID[%d], error[%w]", projectID, err)
+ }
+ if deploymentConfigRaw == nil {
+ return nil, nil
+ }
+ deploymentConfig, err := s.composeDeploymentConfig(ctx, deploymentConfigRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose DeploymentConfig with deploymentConfigRaw[%+v], error[%w]", deploymentConfigRaw, err)
+ }
+ return deploymentConfig, nil
+}
+
+// UpsertDeploymentConfig upserts an instance of DeploymentConfig
+func (s *Store) UpsertDeploymentConfig(ctx context.Context, upsert *api.DeploymentConfigUpsert) (*api.DeploymentConfig, error) {
+ deploymentConfigRaw, err := s.upsertDeploymentConfigRaw(ctx, upsert)
+ if err != nil {
+ return nil, fmt.Errorf("failed to upsert deployment config with DeploymentConfigUpsert[%+v], error[%w]", upsert, err)
+ }
+ deploymentConfig, err := s.composeDeploymentConfig(ctx, deploymentConfigRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose DeploymentConfig with deploymentConfigRaw[%+v], error[%w]", deploymentConfigRaw, err)
+ }
+ return deploymentConfig, nil
}
-// FindDeploymentConfig finds the deployment configuration in a project.
-func (s *DeploymentConfigService) FindDeploymentConfig(ctx context.Context, find *api.DeploymentConfigFind) (*api.DeploymentConfig, error) {
+//
+// private functions
+//
+
+func (s *Store) composeDeploymentConfig(ctx context.Context, raw *deploymentConfigRaw) (*api.DeploymentConfig, error) {
+ deploymentConfig := raw.toDeploymentConfig()
+
+ creator, err := s.GetPrincipalByID(ctx, deploymentConfig.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ deploymentConfig.Creator = creator
+
+ updater, err := s.GetPrincipalByID(ctx, deploymentConfig.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ deploymentConfig.Updater = updater
+
+ project, err := s.GetProjectByID(ctx, deploymentConfig.ProjectID)
+ if err != nil {
+ return nil, err
+ }
+ deploymentConfig.Project = project
+
+ return deploymentConfig, nil
+}
+
+// upsertDeploymentConfigRaw upserts a deployment configuration to a project.
+func (s *Store) upsertDeploymentConfigRaw(ctx context.Context, upsert *api.DeploymentConfigUpsert) (*deploymentConfigRaw, error) {
+ // Validate the deployment configuration.
+ if _, err := api.ValidateAndGetDeploymentSchedule(upsert.Payload); err != nil {
+ return nil, err
+ }
+
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.PTx.Rollback()
+
+ cfg, err := s.upsertDeploymentConfigImpl(ctx, tx.PTx, upsert)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+
+ if err := tx.PTx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return cfg, nil
+}
+
+// getDeploymentConfigImpl finds the deployment configuration in a project.
+func (s *Store) getDeploymentConfigImpl(ctx context.Context, find *api.DeploymentConfigFind) (*deploymentConfigRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -63,9 +169,9 @@ func (s *DeploymentConfigService) FindDeploymentConfig(ctx context.Context, find
defer rows.Close()
// Iterate over result set and deserialize rows into list.
- var ret []*api.DeploymentConfig
+ var ret []*deploymentConfigRaw
for rows.Next() {
- var cfg api.DeploymentConfig
+ var cfg deploymentConfigRaw
if err := rows.Scan(
&cfg.ID,
&cfg.CreatorID,
@@ -95,32 +201,7 @@ func (s *DeploymentConfigService) FindDeploymentConfig(ctx context.Context, find
}
}
-// UpsertDeploymentConfig upserts a deployment configuration to a project.
-func (s *DeploymentConfigService) UpsertDeploymentConfig(ctx context.Context, upsert *api.DeploymentConfigUpsert) (*api.DeploymentConfig, error) {
- // Validate the deployment configuration.
- if _, err := api.ValidateAndGetDeploymentSchedule(upsert.Payload); err != nil {
- return nil, err
- }
-
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.PTx.Rollback()
-
- cfg, err := s.upsertDeploymentConfig(ctx, tx.PTx, upsert)
- if err != nil {
- return nil, FormatError(err)
- }
-
- if err := tx.PTx.Commit(); err != nil {
- return nil, FormatError(err)
- }
-
- return cfg, nil
-}
-
-func (s *DeploymentConfigService) upsertDeploymentConfig(ctx context.Context, tx *sql.Tx, upsert *api.DeploymentConfigUpsert) (*api.DeploymentConfig, error) {
+func (s *Store) upsertDeploymentConfigImpl(ctx context.Context, tx *sql.Tx, upsert *api.DeploymentConfigUpsert) (*deploymentConfigRaw, error) {
if upsert.Payload == "" {
upsert.Payload = "{}"
}
@@ -153,7 +234,7 @@ func (s *DeploymentConfigService) upsertDeploymentConfig(ctx context.Context, tx
defer row.Close()
row.Next()
- var cfg api.DeploymentConfig
+ var cfg deploymentConfigRaw
if err := row.Scan(
&cfg.ID,
&cfg.CreatorID,