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 DataSource (#969)

Yilong Li8fba77465de4
diff --git a/api/data_source.go b/api/data_source.go
index a86957e316..1c634af1f7 100644
--- a/api/data_source.go
+++ b/api/data_source.go
@@ -1,8 +1,6 @@
package api
import (
- "context"
- "database/sql"
"encoding/json"
)
@@ -54,52 +52,6 @@ func (e DataSourceType) String() string {
return ""
}
-// DataSourceRaw is the store model for an DataSource.
-// Fields have exactly the same meanings as DataSource.
-type DataSourceRaw struct {
- ID int
-
- // Standard fields
- CreatorID int
- CreatedTs int64
- UpdaterID int
- UpdatedTs int64
-
- // Related fields
- InstanceID int
- DatabaseID int
-
- // Domain specific fields
- Name string
- Type DataSourceType
- Username string
- Password string
-}
-
-// ToDataSource creates an instance of DataSource based on the DataSourceRaw.
-// This is intended to be called when we need to compose an DataSource relationship.
-func (raw *DataSourceRaw) ToDataSource() *DataSource {
- return &DataSource{
- ID: raw.ID,
-
- // Standard fields
- CreatorID: raw.CreatorID,
- CreatedTs: raw.CreatedTs,
- UpdaterID: raw.UpdaterID,
- UpdatedTs: raw.UpdatedTs,
-
- // Related fields
- InstanceID: raw.InstanceID,
- DatabaseID: raw.DatabaseID,
-
- // Domain specific fields
- Name: raw.Name,
- Type: raw.Type,
- Username: raw.Username,
- Password: raw.Password,
- }
-}
-
// DataSource is the API message for a data source.
type DataSource struct {
ID int `jsonapi:"primary,dataSource"`
@@ -175,13 +127,3 @@ type DataSourcePatch struct {
Password *string `jsonapi:"attr,password"`
UseEmptyPassword *bool `jsonapi:"attr,useEmptyPassword"`
}
-
-// DataSourceService is the service for data source.
-type DataSourceService interface {
- CreateDataSource(ctx context.Context, create *DataSourceCreate) (*DataSourceRaw, error)
- // This is specifically used to create data source when creating the instance.
- CreateDataSourceTx(ctx context.Context, tx *sql.Tx, create *DataSourceCreate) (*DataSourceRaw, error)
- FindDataSourceList(ctx context.Context, find *DataSourceFind) ([]*DataSourceRaw, error)
- FindDataSource(ctx context.Context, find *DataSourceFind) (*DataSourceRaw, error)
- PatchDataSource(ctx context.Context, patch *DataSourcePatch) (*DataSourceRaw, error)
-}
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index a58da87002..02ec10b8a3 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -383,10 +383,9 @@ func (m *Main) Run(ctx context.Context) error {
s.ProjectService = store.NewProjectService(m.l, db, cacheService)
s.ProjectMemberService = store.NewProjectMemberService(m.l, db)
s.ProjectWebhookService = store.NewProjectWebhookService(m.l, db)
- s.DataSourceService = store.NewDataSourceService(m.l, db, cacheService)
s.BackupService = store.NewBackupService(m.l, db, s.PolicyService)
s.DatabaseService = store.NewDatabaseService(m.l, db, cacheService, s.PolicyService, s.BackupService)
- s.InstanceService = store.NewInstanceService(m.l, db, cacheService, s.DatabaseService, s.DataSourceService)
+ s.InstanceService = store.NewInstanceService(m.l, db, cacheService, s.DatabaseService, storeInstance)
s.InstanceUserService = store.NewInstanceUserService(m.l, db)
s.TableService = store.NewTableService(m.l, db)
s.ColumnService = store.NewColumnService(m.l, db)
diff --git a/server/database.go b/server/database.go
index 32e3e647d3..ded327bcb5 100644
--- a/server/database.go
+++ b/server/database.go
@@ -778,17 +778,15 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
dataSourceFind := &api.DataSourceFind{
ID: &dataSourceID,
}
- dataSourceRaw, err := s.DataSourceService.FindDataSource(ctx, dataSourceFind)
+ dataSource, err := s.store.GetDataSource(ctx, dataSourceFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch data source by ID %d", dataSourceID)).SetInternal(err)
}
- if dataSourceRaw == nil || dataSourceRaw.DatabaseID != databaseID {
+ if dataSource.DatabaseID != databaseID {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("data source not found by ID %d and database ID %d", dataSourceID, databaseID))
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- // TODO(dragonly): compose DataSource
- dataSource := dataSourceRaw.ToDataSource()
if err := jsonapi.MarshalPayload(c.Response().Writer, dataSource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal find data source response").SetInternal(err)
}
@@ -821,14 +819,12 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
dataSourceCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)
dataSourceCreate.DatabaseID = databaseID
- dataSourceRaw, err := s.DataSourceService.CreateDataSource(ctx, dataSourceCreate)
+ dataSource, err := s.store.CreateDataSource(ctx, dataSourceCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create data source").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- // TODO(dragonly): compose DataSource
- dataSource := dataSourceRaw.ToDataSource()
if err := jsonapi.MarshalPayload(c.Response().Writer, dataSource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create data source response").SetInternal(err)
}
@@ -865,7 +861,7 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
ID: &dataSourceID,
DatabaseID: &databaseID,
}
- dataSourceRaw, err := s.DataSourceService.FindDataSource(ctx, dataSourceFind)
+ dataSourceRaw, err := s.store.GetDataSource(ctx, dataSourceFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find data source").SetInternal(err)
}
@@ -886,14 +882,12 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
dataSourcePatch.Password = &password
}
- dataSourceRaw, err = s.DataSourceService.PatchDataSource(ctx, dataSourcePatch)
+ dataSource, err := s.store.PatchDataSource(ctx, dataSourcePatch)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update data source with ID %d", dataSourceID)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- // TODO(dragonly): compose DataSource
- dataSource := dataSourceRaw.ToDataSource()
if err := jsonapi.MarshalPayload(c.Response().Writer, dataSource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal patch data source response").SetInternal(err)
}
diff --git a/server/instance.go b/server/instance.go
index 8148e7692e..33760a1f06 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -493,17 +493,12 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, raw *api.Insta
}
}
- dataSourceRawList, err := s.DataSourceService.FindDataSourceList(ctx, &api.DataSourceFind{
+ dataSourceList, err := s.store.FindDataSource(ctx, &api.DataSourceFind{
InstanceID: &instance.ID,
})
if err != nil {
return nil, err
}
- // TODO(dragonly): implement composeDataSourceRelationship
- var dataSourceList []*api.DataSource
- for _, dataSourceRaw := range dataSourceRawList {
- dataSourceList = append(dataSourceList, dataSourceRaw.ToDataSource())
- }
instance.DataSourceList = dataSourceList
for _, dataSource := range instance.DataSourceList {
if dataSource.Creator, err = s.store.GetPrincipalByID(ctx, dataSource.CreatorID); err != nil {
@@ -521,7 +516,7 @@ func (s *Server) findInstanceAdminPasswordByID(ctx context.Context, instanceID i
dataSourceFind := &api.DataSourceFind{
InstanceID: &instanceID,
}
- dataSourceRawList, err := s.DataSourceService.FindDataSourceList(ctx, dataSourceFind)
+ dataSourceRawList, err := s.store.FindDataSource(ctx, dataSourceFind)
if err != nil {
return "", err
}
diff --git a/server/server.go b/server/server.go
index b0ef5c5149..4eab8eda31 100644
--- a/server/server.go
+++ b/server/server.go
@@ -48,7 +48,6 @@ type Server struct {
ColumnService api.ColumnService
ViewService api.ViewService
IndexService api.IndexService
- DataSourceService api.DataSourceService
BackupService api.BackupService
IssueService api.IssueService
IssueSubscriberService api.IssueSubscriberService
diff --git a/store/data_source.go b/store/data_source.go
index a63de04530..5d2f51e902 100644
--- a/store/data_source.go
+++ b/store/data_source.go
@@ -8,35 +8,151 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "go.uber.org/zap"
)
-var (
- _ api.DataSourceService = (*DataSourceService)(nil)
-)
+// dataSourceRaw is the store model for an DataSource.
+// Fields have exactly the same meanings as DataSource.
+type dataSourceRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ InstanceID int
+ DatabaseID int
+
+ // Domain specific fields
+ Name string
+ Type api.DataSourceType
+ Username string
+ Password string
+}
-// DataSourceService represents a service for managing dataSource.
-type DataSourceService struct {
- l *zap.Logger
- db *DB
+// toDataSource creates an instance of DataSource based on the dataSourceRaw.
+// This is intended to be called when we need to compose an DataSource relationship.
+func (raw *dataSourceRaw) toDataSource() *api.DataSource {
+ return &api.DataSource{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ InstanceID: raw.InstanceID,
+ DatabaseID: raw.DatabaseID,
+
+ // Domain specific fields
+ Name: raw.Name,
+ Type: raw.Type,
+ Username: raw.Username,
+ Password: raw.Password,
+ }
+}
- cache api.CacheService
+// CreateDataSource creates an instance of DataSource.
+func (s *Store) CreateDataSource(ctx context.Context, create *api.DataSourceCreate) (*api.DataSource, error) {
+ dataSourceRaw, err := s.createDataSourceRaw(ctx, create)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to create data source with DataSourceCreate[%+v], error[%w]", create, err)
+ }
+ dataSource, err := s.composeDataSource(ctx, dataSourceRaw)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to compose data source with dataSourceRaw[%+v], error[%w]", dataSourceRaw, err)
+ }
+ return dataSource, nil
}
-// NewDataSourceService returns a new instance of DataSourceService.
-func NewDataSourceService(logger *zap.Logger, db *DB, cache api.CacheService) *DataSourceService {
- return &DataSourceService{l: logger, db: db, cache: cache}
+// GetDataSource gets an instance of DataSource
+func (s *Store) GetDataSource(ctx context.Context, find *api.DataSourceFind) (*api.DataSource, error) {
+ dataSourceRaw, err := s.getDataSourceRaw(ctx, find)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to get data source with DataSourceFind[%+v], error[%w]", find, err)
+ }
+ dataSource, err := s.composeDataSource(ctx, dataSourceRaw)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to compose data source with dataSourceRaw[%+v], error[%w]", dataSourceRaw, err)
+ }
+ return dataSource, nil
}
-// CreateDataSource creates a new dataSource.
-func (s *DataSourceService) CreateDataSource(ctx context.Context, create *api.DataSourceCreate) (*api.DataSourceRaw, error) {
+// FindDataSource finds a list of DataSource instances
+func (s *Store) FindDataSource(ctx context.Context, find *api.DataSourceFind) ([]*api.DataSource, error) {
+ DataSourceRawList, err := s.findDataSourceRaw(ctx, find)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to find DataSource list, error[%w]", err)
+ }
+ var DataSourceList []*api.DataSource
+ for _, raw := range DataSourceRawList {
+ DataSource, err := s.composeDataSource(ctx, raw)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to compose DataSource role with dataSourceRaw[%+v], error[%w]", raw, err)
+ }
+ DataSourceList = append(DataSourceList, DataSource)
+ }
+ return DataSourceList, nil
+}
+
+// PatchDataSource patches an instance of DataSource
+func (s *Store) PatchDataSource(ctx context.Context, patch *api.DataSourcePatch) (*api.DataSource, error) {
+ dataSourceRaw, err := s.patchDataSourceRaw(ctx, patch)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to patch DataSource with DataSourcePatch[%+v], error[%w]", patch, err)
+ }
+ DataSource, err := s.composeDataSource(ctx, dataSourceRaw)
+ if err != nil {
+ return nil, fmt.Errorf("Failed to compose DataSource role with dataSourceRaw[%+v], error[%w]", dataSourceRaw, err)
+ }
+ return DataSource, nil
+}
+
+//
+// private functions
+//
+
+// createDataSourceRawTx creates an instance of DataSource.
+// This uses an existing transaction object.
+func (s *Store) createDataSourceRawTx(ctx context.Context, tx *sql.Tx, create *api.DataSourceCreate) error {
+ _, err := s.createDataSourceImpl(ctx, tx, create)
+ if err != nil {
+ return fmt.Errorf("Failed to create data source with DataSourceCreate[%+v], error[%w]", create, err)
+ }
+ return nil
+}
+
+func (s *Store) composeDataSource(ctx context.Context, raw *dataSourceRaw) (*api.DataSource, error) {
+ dataSource := raw.toDataSource()
+
+ creator, err := s.GetPrincipalByID(ctx, dataSource.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ dataSource.Creator = creator
+
+ updater, err := s.GetPrincipalByID(ctx, dataSource.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ dataSource.Updater = updater
+
+ return dataSource, nil
+}
+
+// createDataSourceRaw creates a new dataSource.
+func (s *Store) createDataSourceRaw(ctx context.Context, create *api.DataSourceCreate) (*dataSourceRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- dataSource, err := s.createDataSource(ctx, tx.PTx, create)
+ dataSource, err := s.createDataSourceImpl(ctx, tx.PTx, create)
if err != nil {
return nil, err
}
@@ -48,20 +164,15 @@ func (s *DataSourceService) CreateDataSource(ctx context.Context, create *api.Da
return dataSource, nil
}
-// CreateDataSourceTx creates a data source with a transaction.
-func (s *DataSourceService) CreateDataSourceTx(ctx context.Context, tx *sql.Tx, create *api.DataSourceCreate) (*api.DataSourceRaw, error) {
- return s.createDataSource(ctx, tx, create)
-}
-
-// FindDataSourceList retrieves a list of data sources based on find.
-func (s *DataSourceService) FindDataSourceList(ctx context.Context, find *api.DataSourceFind) ([]*api.DataSourceRaw, error) {
+// findDataSourceRaw retrieves a list of data sources based on find.
+func (s *Store) findDataSourceRaw(ctx context.Context, find *api.DataSourceFind) ([]*dataSourceRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := s.findDataSourceList(ctx, tx.PTx, find)
+ list, err := s.findDataSourceImpl(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
@@ -69,38 +180,38 @@ func (s *DataSourceService) FindDataSourceList(ctx context.Context, find *api.Da
return list, nil
}
-// FindDataSource retrieves a single dataSource based on find.
+// getDataSourceRaw retrieves a single dataSource based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *DataSourceService) FindDataSource(ctx context.Context, find *api.DataSourceFind) (*api.DataSourceRaw, error) {
+func (s *Store) getDataSourceRaw(ctx context.Context, find *api.DataSourceFind) (*dataSourceRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := s.findDataSourceList(ctx, tx.PTx, find)
+ list, err := s.findDataSourceImpl(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
- return nil, nil
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("data source not found with DataSourceFind[%+v]", find)}
} else if len(list) > 1 {
return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d data sources with filter %+v, expect 1", len(list), find)}
}
return list[0], nil
}
-// PatchDataSource updates an existing dataSource by ID.
+// patchDataSourceRaw updates an existing dataSource by ID.
// Returns ENOTFOUND if dataSource does not exist.
-func (s *DataSourceService) PatchDataSource(ctx context.Context, patch *api.DataSourcePatch) (*api.DataSourceRaw, error) {
+func (s *Store) patchDataSourceRaw(ctx context.Context, patch *api.DataSourcePatch) (*dataSourceRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- dataSource, err := s.patchDataSource(ctx, tx.PTx, patch)
+ dataSource, err := s.patchDataSourceImpl(ctx, tx.PTx, patch)
if err != nil {
return nil, FormatError(err)
}
@@ -112,8 +223,8 @@ func (s *DataSourceService) PatchDataSource(ctx context.Context, patch *api.Data
return dataSource, nil
}
-// createDataSource creates a new dataSource.
-func (s *DataSourceService) createDataSource(ctx context.Context, tx *sql.Tx, create *api.DataSourceCreate) (*api.DataSourceRaw, error) {
+// createDataSourceImpl creates a new dataSource.
+func (s *Store) createDataSourceImpl(ctx context.Context, tx *sql.Tx, create *api.DataSourceCreate) (*dataSourceRaw, error) {
// Insert row into dataSource.
row, err := tx.QueryContext(ctx, `
INSERT INTO data_source (
@@ -145,7 +256,7 @@ func (s *DataSourceService) createDataSource(ctx context.Context, tx *sql.Tx, cr
defer row.Close()
row.Next()
- var dataSourceRaw api.DataSourceRaw
+ var dataSourceRaw dataSourceRaw
if err := row.Scan(
&dataSourceRaw.ID,
&dataSourceRaw.CreatorID,
@@ -165,7 +276,7 @@ func (s *DataSourceService) createDataSource(ctx context.Context, tx *sql.Tx, cr
return &dataSourceRaw, nil
}
-func (s *DataSourceService) findDataSourceList(ctx context.Context, tx *sql.Tx, find *api.DataSourceFind) ([]*api.DataSourceRaw, error) {
+func (s *Store) findDataSourceImpl(ctx context.Context, tx *sql.Tx, find *api.DataSourceFind) ([]*dataSourceRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -204,9 +315,9 @@ func (s *DataSourceService) findDataSourceList(ctx context.Context, tx *sql.Tx,
defer rows.Close()
// Iterate over result set and deserialize rows into dataSourceRawList.
- var dataSourceRawList []*api.DataSourceRaw
+ var dataSourceRawList []*dataSourceRaw
for rows.Next() {
- var dataSourceRaw api.DataSourceRaw
+ var dataSourceRaw dataSourceRaw
if err := rows.Scan(
&dataSourceRaw.ID,
&dataSourceRaw.CreatorID,
@@ -232,8 +343,8 @@ func (s *DataSourceService) findDataSourceList(ctx context.Context, tx *sql.Tx,
return dataSourceRawList, nil
}
-// patchDataSource updates a dataSource by ID. Returns the new state of the dataSource after update.
-func (s *DataSourceService) patchDataSource(ctx context.Context, tx *sql.Tx, patch *api.DataSourcePatch) (*api.DataSourceRaw, error) {
+// patchDataSourceImpl updates a dataSource by ID. Returns the new state of the dataSource after update.
+func (s *Store) patchDataSourceImpl(ctx context.Context, tx *sql.Tx, patch *api.DataSourcePatch) (*dataSourceRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.Username; v != nil {
@@ -260,7 +371,7 @@ func (s *DataSourceService) patchDataSource(ctx context.Context, tx *sql.Tx, pat
defer row.Close()
if row.Next() {
- var dataSourceRaw api.DataSourceRaw
+ var dataSourceRaw dataSourceRaw
if err := row.Scan(
&dataSourceRaw.ID,
&dataSourceRaw.CreatorID,
diff --git a/store/environment.go b/store/environment.go
index 578067c150..9b7961e7fd 100644
--- a/store/environment.go
+++ b/store/environment.go
@@ -27,7 +27,7 @@ type environmentRaw struct {
Order int
}
-// toEnvironment creates an instance of Environment based on the EnvironmentRaw.
+// toEnvironment creates an instance of Environment based on the environmentRaw.
// This is intended to be called when we need to compose an Environment relationship.
func (raw *environmentRaw) toEnvironment() *api.Environment {
return &api.Environment{
@@ -46,20 +46,20 @@ func (raw *environmentRaw) toEnvironment() *api.Environment {
// CreateEnvironment creates an instance of Environment
func (s *Store) CreateEnvironment(ctx context.Context, create *api.EnvironmentCreate) (*api.Environment, error) {
- EnvironmentRaw, err := s.createEnvironmentRaw(ctx, create)
+ environmentRaw, err := s.createEnvironmentRaw(ctx, create)
if err != nil {
return nil, fmt.Errorf("Failed to create Environment with EnvironmentCreate[%+v], error[%w]", create, err)
}
- Environment, err := s.composeEnvironment(ctx, EnvironmentRaw)
+ Environment, err := s.composeEnvironment(ctx, environmentRaw)
if err != nil {
- return nil, fmt.Errorf("Failed to compose Environment with EnvironmentRaw[%+v], error[%w]", EnvironmentRaw, err)
+ return nil, fmt.Errorf("Failed to compose Environment with environmentRaw[%+v], error[%w]", environmentRaw, err)
}
return Environment, nil
}
// FindEnvironment finds a list of Environment instances
func (s *Store) FindEnvironment(ctx context.Context, find *api.EnvironmentFind) ([]*api.Environment, error) {
- EnvironmentRawList, err := s.findEnvironmentListRaw(ctx, find)
+ EnvironmentRawList, err := s.findEnvironmentRaw(ctx, find)
if err != nil {
return nil, fmt.Errorf("Failed to find Environment list, error[%w]", err)
}
@@ -67,7 +67,7 @@ func (s *Store) FindEnvironment(ctx context.Context, find *api.EnvironmentFind)
for _, raw := range EnvironmentRawList {
Environment, err := s.composeEnvironment(ctx, raw)
if err != nil {
- return nil, fmt.Errorf("Failed to compose Environment role with EnvironmentRaw[%+v], error[%w]", raw, err)
+ return nil, fmt.Errorf("Failed to compose Environment role with environmentRaw[%+v], error[%w]", raw, err)
}
EnvironmentList = append(EnvironmentList, Environment)
}
@@ -76,13 +76,13 @@ func (s *Store) FindEnvironment(ctx context.Context, find *api.EnvironmentFind)
// PatchEnvironment patches an instance of Environment
func (s *Store) PatchEnvironment(ctx context.Context, patch *api.EnvironmentPatch) (*api.Environment, error) {
- EnvironmentRaw, err := s.patchEnvironmentRaw(ctx, patch)
+ environmentRaw, err := s.patchEnvironmentRaw(ctx, patch)
if err != nil {
return nil, fmt.Errorf("Failed to patch Environment with EnvironmentPatch[%+v], error[%w]", patch, err)
}
- Environment, err := s.composeEnvironment(ctx, EnvironmentRaw)
+ Environment, err := s.composeEnvironment(ctx, environmentRaw)
if err != nil {
- return nil, fmt.Errorf("Failed to compose Environment role with EnvironmentRaw[%+v], error[%w]", EnvironmentRaw, err)
+ return nil, fmt.Errorf("Failed to compose Environment role with environmentRaw[%+v], error[%w]", environmentRaw, err)
}
return Environment, nil
}
@@ -91,15 +91,12 @@ func (s *Store) PatchEnvironment(ctx context.Context, patch *api.EnvironmentPatc
func (s *Store) GetEnvironmentByID(ctx context.Context, id int) (*api.Environment, error) {
envRaw, err := s.getEnvironmentByIDRaw(ctx, id)
if err != nil {
- return nil, err
- }
- if envRaw == nil {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("environment not found with ID %v", id)}
+ return nil, fmt.Errorf("Failed to get environment with ID[%d], error[%w]", id, err)
}
env, err := s.composeEnvironment(ctx, envRaw)
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("Failed to compose environment with environmentRaw[%+v], error[%w]", envRaw, err)
}
return env, nil
@@ -151,8 +148,8 @@ func (s *Store) createEnvironmentRaw(ctx context.Context, create *api.Environmen
return environment, nil
}
-// findEnvironmentListRaw retrieves a list of environments based on find.
-func (s *Store) findEnvironmentListRaw(ctx context.Context, find *api.EnvironmentFind) ([]*environmentRaw, error) {
+// findEnvironmentRaw retrieves a list of environments based on find.
+func (s *Store) findEnvironmentRaw(ctx context.Context, find *api.EnvironmentFind) ([]*environmentRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
diff --git a/store/instance.go b/store/instance.go
index 1a51ae72aa..3d9145b89c 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -22,13 +22,13 @@ type InstanceService struct {
cache api.CacheService
- databaseService api.DatabaseService
- dataSourceService api.DataSourceService
+ databaseService api.DatabaseService
+ store *Store
}
// NewInstanceService returns a new instance of InstanceService.
-func NewInstanceService(logger *zap.Logger, db *DB, cache api.CacheService, databaseService api.DatabaseService, dataSourceService api.DataSourceService) *InstanceService {
- return &InstanceService{l: logger, db: db, cache: cache, databaseService: databaseService, dataSourceService: dataSourceService}
+func NewInstanceService(logger *zap.Logger, db *DB, cache api.CacheService, databaseService api.DatabaseService, store *Store) *InstanceService {
+ return &InstanceService{l: logger, db: db, cache: cache, databaseService: databaseService, store: store}
}
// CreateInstance creates a new instance.
@@ -69,7 +69,7 @@ func (s *InstanceService) CreateInstance(ctx context.Context, create *api.Instan
Username: create.Username,
Password: create.Password,
}
- if _, err := s.dataSourceService.CreateDataSourceTx(ctx, tx.PTx, adminDataSourceCreate); err != nil {
+ if err := s.store.createDataSourceRawTx(ctx, tx.PTx, adminDataSourceCreate); err != nil {
return nil, err
}