Skip to content

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

refactor: split data model object for api.Instance (#851)

Yilong Li99c5b74930ae
diff --git a/api/instance.go b/api/instance.go
index b363442980..fb34a6a5ff 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -7,6 +7,56 @@ import (
"github.com/bytebase/bytebase/plugin/db"
)
+// InstanceRaw is the store model for an Instance.
+// Fields have exactly the same meanings as Instance.
+type InstanceRaw struct {
+ ID int
+
+ // Standard fields
+ RowStatus RowStatus
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ EnvironmentID int
+
+ // Domain specific fields
+ Name string
+ Engine db.Type
+ EngineVersion string
+ ExternalLink string
+ Host string
+ Port string
+}
+
+// ToInstance creates an instance of Instance based on the InstanceRaw.
+// This is intended to be called when we need to compose an Instance relationship.
+func (raw *InstanceRaw) ToInstance() *Instance {
+ return &Instance{
+ ID: raw.ID,
+
+ // Standard fields
+ RowStatus: raw.RowStatus,
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ EnvironmentID: raw.EnvironmentID,
+
+ // Domain specific fields
+ Name: raw.Name,
+ Engine: raw.Engine,
+ EngineVersion: raw.EngineVersion,
+ ExternalLink: raw.ExternalLink,
+ Host: raw.Host,
+ Port: raw.Port,
+ }
+}
+
// Instance is the API message for an instance.
type Instance struct {
ID int `jsonapi:"primary,instance"`
@@ -165,9 +215,9 @@ type MigrationHistory struct {
// InstanceService is the service for instances.
type InstanceService interface {
// CreateInstance should also create the * database and the admin data source.
- CreateInstance(ctx context.Context, create *InstanceCreate) (*Instance, error)
- FindInstanceList(ctx context.Context, find *InstanceFind) ([]*Instance, error)
- FindInstance(ctx context.Context, find *InstanceFind) (*Instance, error)
+ CreateInstance(ctx context.Context, create *InstanceCreate) (*InstanceRaw, error)
+ FindInstanceList(ctx context.Context, find *InstanceFind) ([]*InstanceRaw, error)
+ FindInstance(ctx context.Context, find *InstanceFind) (*InstanceRaw, error)
CountInstance(ctx context.Context, find *InstanceFind) (int, error)
- PatchInstance(ctx context.Context, patch *InstancePatch) (*Instance, error)
+ PatchInstance(ctx context.Context, patch *InstancePatch) (*InstanceRaw, error)
}
diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.go
index 5b66ab1fa0..601e377fba 100644
--- a/server/anomaly_scanner.go
+++ b/server/anomaly_scanner.go
@@ -94,21 +94,22 @@ func (s *AnomalyScanner) Run(ctx context.Context, wg *sync.WaitGroup) {
instanceFind := &api.InstanceFind{
RowStatus: &rowStatus,
}
- instanceList, err := s.server.InstanceService.FindInstanceList(ctx, instanceFind)
+ instanceRawList, err := s.server.InstanceService.FindInstanceList(ctx, instanceFind)
if err != nil {
s.l.Error("Failed to retrieve instance list", zap.Error(err))
return
}
- for _, instance := range instanceList {
- if err := s.server.composeInstanceRelationship(ctx, instance); err != nil {
- s.l.Error(fmt.Sprintf("Failed to compose instance relationship, ID %v, name %q.", instance.ID, instance.Name), zap.Error(err))
+ for _, instanceRaw := range instanceRawList {
+ instance, err := s.server.composeInstanceRelationship(ctx, instanceRaw)
+ if err != nil {
+ s.l.Error(fmt.Sprintf("Failed to compose instance relationship, ID %v, name %q.", instanceRaw.ID, instanceRaw.Name), zap.Error(err))
continue
}
foundEnv := false
for _, env := range envList {
- if env.ID == instance.EnvironmentID {
+ if env.ID == instanceRaw.EnvironmentID {
if env.RowStatus == api.Normal {
instance.Environment = env
}
@@ -122,11 +123,11 @@ func (s *AnomalyScanner) Run(ctx context.Context, wg *sync.WaitGroup) {
}
mu.Lock()
- if _, ok := runningTasks[instance.ID]; ok {
+ if _, ok := runningTasks[instanceRaw.ID]; ok {
mu.Unlock()
continue
}
- runningTasks[instance.ID] = true
+ runningTasks[instanceRaw.ID] = true
mu.Unlock()
// Do NOT use go-routine otherwise would cause "database locked" in underlying SQLite
diff --git a/server/database.go b/server/database.go
index a7d2f1b1cd..261a4bbbb2 100644
--- a/server/database.go
+++ b/server/database.go
@@ -25,15 +25,15 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
}
databaseCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)
- instance, err := s.InstanceService.FindInstance(ctx, &api.InstanceFind{ID: &databaseCreate.InstanceID})
+ instanceRaw, err := s.InstanceService.FindInstance(ctx, &api.InstanceFind{ID: &databaseCreate.InstanceID})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find instance").SetInternal(err)
}
- if instance == nil {
+ if instanceRaw == nil {
err := fmt.Errorf("Instance ID not found %v", databaseCreate.InstanceID)
return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
}
- databaseCreate.EnvironmentID = instance.EnvironmentID
+ databaseCreate.EnvironmentID = instanceRaw.EnvironmentID
project, err := s.composeProjectByID(ctx, databaseCreate.ProjectID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find project").SetInternal(err)
@@ -47,7 +47,8 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
}
// Pre-validate database labels.
if databaseCreate.Labels != nil && *databaseCreate.Labels != "" {
- if err := s.setDatabaseLabels(ctx, *databaseCreate.Labels, &api.Database{Name: databaseCreate.Name, Instance: instance} /* dummy database */, project, databaseCreate.CreatorID, true /* validateOnly */); err != nil {
+ // TODO(dragonly): compose Instance
+ if err := s.setDatabaseLabels(ctx, *databaseCreate.Labels, &api.Database{Name: databaseCreate.Name, Instance: instanceRaw.ToInstance()} /* dummy database */, project, databaseCreate.CreatorID, true /* validateOnly */); err != nil {
return err
}
}
diff --git a/server/instance.go b/server/instance.go
index 7857a6b948..0212f84e7b 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -32,7 +32,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
}
- instance, err := s.InstanceService.CreateInstance(ctx, instanceCreate)
+ instanceRaw, err := s.InstanceService.CreateInstance(ctx, instanceCreate)
if err != nil {
if common.ErrorCode(err) == common.Conflict {
return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Instance name already exists: %s", instanceCreate.Name))
@@ -40,7 +40,8 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create instance").SetInternal(err)
}
- if err := s.composeInstanceRelationship(ctx, instance); err != nil {
+ instance, err := s.composeInstanceRelationship(ctx, instanceRaw)
+ if err != nil {
return err
}
@@ -74,15 +75,18 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
rowStatus := api.RowStatus(rowStatusStr)
instanceFind.RowStatus = &rowStatus
}
- instanceList, err := s.InstanceService.FindInstanceList(ctx, instanceFind)
+ instanceRawList, err := s.InstanceService.FindInstanceList(ctx, instanceFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch instance list").SetInternal(err)
}
- for _, instance := range instanceList {
- if err := s.composeInstanceRelationship(ctx, instance); err != nil {
+ var instanceList []*api.Instance
+ for _, instanceRaw := range instanceRawList {
+ instance, err := s.composeInstanceRelationship(ctx, instanceRaw)
+ if err != nil {
return err
}
+ instanceList = append(instanceList, instance)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -146,6 +150,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
}
+ var instancePatchedRaw *api.InstanceRaw
if instancePatch.RowStatus != nil || instancePatch.Name != nil || instancePatch.ExternalLink != nil || instancePatch.Host != nil || instancePatch.Port != nil {
// Users can switch instance status from ARCHIVED to NORMAL.
// So we need to check the current instance count with NORMAL status for quota limitation.
@@ -154,7 +159,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
return err
}
}
- instance, err = s.InstanceService.PatchInstance(ctx, instancePatch)
+ instancePatchedRaw, err = s.InstanceService.PatchInstance(ctx, instancePatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
@@ -168,25 +173,25 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
instanceFind := &api.InstanceFind{
ID: &id,
}
- instance, err = s.InstanceService.FindInstance(ctx, instanceFind)
+ instancePatchedRaw, err = s.InstanceService.FindInstance(ctx, instanceFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
- if instance == nil {
+ if instancePatchedRaw == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
}
dataSourceType := api.Admin
dataSourceFind := &api.DataSourceFind{
- InstanceID: &instance.ID,
+ InstanceID: &instancePatchedRaw.ID,
Type: &dataSourceType,
}
adminDataSourceRaw, err := s.DataSourceService.FindDataSource(ctx, dataSourceFind)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch data source for instance: %v", instance.Name)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch data source for instance: %v", instancePatchedRaw.Name)).SetInternal(err)
}
if adminDataSourceRaw == nil {
- err := fmt.Errorf("data source not found for instance ID %v, name %q and type %q", instance.ID, instance.Name, dataSourceType)
+ err := fmt.Errorf("data source not found for instance ID %v, name %q and type %q", instancePatchedRaw.ID, instancePatchedRaw.Name, dataSourceType)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
}
@@ -202,31 +207,32 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
dataSourcePatch.Password = &password
}
if _, err := s.DataSourceService.PatchDataSource(ctx, dataSourcePatch); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch data source for instance: %v", instance.Name)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch data source for instance: %v", instancePatchedRaw.Name)).SetInternal(err)
}
}
- if err := s.composeInstanceRelationship(ctx, instance); err != nil {
+ instancePatched, err := s.composeInstanceRelationship(ctx, instancePatchedRaw)
+ if err != nil {
return err
}
// Try immediately setup the migration schema, sync the engine version and schema after updating any connection related info.
if instancePatch.Host != nil || instancePatch.Port != nil {
- db, err := getAdminDatabaseDriver(ctx, instance, "", s.l)
+ db, err := getAdminDatabaseDriver(ctx, instancePatched, "", s.l)
if err == nil {
defer db.Close(ctx)
if err := db.SetupMigrationIfNeeded(ctx); err != nil {
s.l.Warn("Failed to setup migration schema on instance update",
- zap.String("instance_name", instance.Name),
- zap.String("engine", string(instance.Engine)),
+ zap.String("instance_name", instancePatchedRaw.Name),
+ zap.String("engine", string(instancePatchedRaw.Engine)),
zap.Error(err))
}
- s.syncEngineVersionAndSchema(ctx, instance)
+ s.syncEngineVersionAndSchema(ctx, instancePatched)
}
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := jsonapi.MarshalPayload(c.Response().Writer, instance); err != nil {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, instancePatched); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal instance ID response: %v", id)).SetInternal(err)
}
return nil
@@ -466,38 +472,42 @@ func (s *Server) composeInstanceByID(ctx context.Context, id int) (*api.Instance
instanceFind := &api.InstanceFind{
ID: &id,
}
- instance, err := s.InstanceService.FindInstance(ctx, instanceFind)
+ instanceRaw, err := s.InstanceService.FindInstance(ctx, instanceFind)
if err != nil {
return nil, err
}
- if instance == nil {
+ if instanceRaw == nil {
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("instance not found with ID %v", id)}
}
- if err := s.composeInstanceRelationship(ctx, instance); err != nil {
+ instance, err := s.composeInstanceRelationship(ctx, instanceRaw)
+ if err != nil {
return nil, err
}
return instance, nil
}
-func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.Instance) error {
- var err error
+func (s *Server) composeInstanceRelationship(ctx context.Context, raw *api.InstanceRaw) (*api.Instance, error) {
+ instance := raw.ToInstance()
- instance.Creator, err = s.composePrincipalByID(ctx, instance.CreatorID)
+ creator, err := s.composePrincipalByID(ctx, instance.CreatorID)
if err != nil {
- return err
+ return nil, err
}
+ instance.Creator = creator
- instance.Updater, err = s.composePrincipalByID(ctx, instance.UpdaterID)
+ updater, err := s.composePrincipalByID(ctx, instance.UpdaterID)
if err != nil {
- return err
+ return nil, err
}
+ instance.Updater = updater
- instance.Environment, err = s.composeEnvironmentByID(ctx, instance.EnvironmentID)
+ env, err := s.composeEnvironmentByID(ctx, instance.EnvironmentID)
if err != nil {
- return err
+ return nil, err
}
+ instance.Environment = env
rowStatus := api.Normal
anomalyListRaw, err := s.AnomalyService.FindAnomalyList(ctx, &api.AnomalyFind{
@@ -506,7 +516,7 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
InstanceOnly: true,
})
if err != nil {
- return err
+ return nil, err
}
var anomalyList []*api.Anomaly
for _, anomalyRaw := range anomalyListRaw {
@@ -516,10 +526,10 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
instance.AnomalyList = anomalyList
for _, anomaly := range instance.AnomalyList {
if anomaly.Creator, err = s.composePrincipalByID(ctx, anomaly.CreatorID); err != nil {
- return err
+ return nil, err
}
if anomaly.Updater, err = s.composePrincipalByID(ctx, anomaly.UpdaterID); err != nil {
- return err
+ return nil, err
}
}
@@ -527,9 +537,9 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
InstanceID: &instance.ID,
})
if err != nil {
- return err
+ return nil, err
}
- // TODO(dragonly): compose DataSource
+ // TODO(dragonly): implement composeDataSourceRelationship
var dataSourceList []*api.DataSource
for _, dataSourceRaw := range dataSourceRawList {
dataSourceList = append(dataSourceList, dataSourceRaw.ToDataSource())
@@ -537,10 +547,10 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
instance.DataSourceList = dataSourceList
for _, dataSource := range instance.DataSourceList {
if dataSource.Creator, err = s.composePrincipalByID(ctx, dataSource.CreatorID); err != nil {
- return err
+ return nil, err
}
if dataSource.Updater, err = s.composePrincipalByID(ctx, dataSource.UpdaterID); err != nil {
- return err
+ return nil, err
}
// TODO(d): remove this when UI is fully switched to data source API.
@@ -550,7 +560,7 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
}
}
- return nil
+ return instance, nil
}
func (s *Server) findInstanceAdminPasswordByID(ctx context.Context, instanceID int) (string, error) {
diff --git a/server/schema_syncer.go b/server/schema_syncer.go
index 61da72bb83..1d6a0ce3f0 100644
--- a/server/schema_syncer.go
+++ b/server/schema_syncer.go
@@ -57,22 +57,23 @@ func (s *SchemaSyncer) Run(ctx context.Context, wg *sync.WaitGroup) {
instanceFind := &api.InstanceFind{
RowStatus: &rowStatus,
}
- instanceList, err := s.server.InstanceService.FindInstanceList(ctx, instanceFind)
+ instanceRawList, err := s.server.InstanceService.FindInstanceList(ctx, instanceFind)
if err != nil {
s.l.Error("Failed to retrieve instances", zap.Error(err))
return
}
- for _, instance := range instanceList {
+ for _, instanceRaw := range instanceRawList {
mu.Lock()
- if _, ok := runningTasks[instance.ID]; ok {
+ if _, ok := runningTasks[instanceRaw.ID]; ok {
mu.Unlock()
continue
}
- runningTasks[instance.ID] = true
+ runningTasks[instanceRaw.ID] = true
mu.Unlock()
- if err := s.server.composeInstanceRelationship(ctx, instance); err != nil {
+ instance, err := s.server.composeInstanceRelationship(ctx, instanceRaw)
+ if err != nil {
s.l.Error("Failed to sync instance",
zap.Int("id", instance.ID),
zap.String("name", instance.Name),
diff --git a/store/instance.go b/store/instance.go
index 7ffd39f5cf..1a51ae72aa 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -32,7 +32,7 @@ func NewInstanceService(logger *zap.Logger, db *DB, cache api.CacheService, data
}
// CreateInstance creates a new instance.
-func (s *InstanceService) CreateInstance(ctx context.Context, create *api.InstanceCreate) (*api.Instance, error) {
+func (s *InstanceService) CreateInstance(ctx context.Context, create *api.InstanceCreate) (*api.InstanceRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -85,7 +85,7 @@ func (s *InstanceService) CreateInstance(ctx context.Context, create *api.Instan
}
// FindInstanceList retrieves a list of instances based on find.
-func (s *InstanceService) FindInstanceList(ctx context.Context, find *api.InstanceFind) ([]*api.Instance, error) {
+func (s *InstanceService) FindInstanceList(ctx context.Context, find *api.InstanceFind) ([]*api.InstanceRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -133,15 +133,15 @@ func (s *InstanceService) CountInstance(ctx context.Context, find *api.InstanceF
// FindInstance retrieves a single instance based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *InstanceService) FindInstance(ctx context.Context, find *api.InstanceFind) (*api.Instance, error) {
+func (s *InstanceService) FindInstance(ctx context.Context, find *api.InstanceFind) (*api.InstanceRaw, error) {
if find.ID != nil {
- instance := &api.Instance{}
- has, err := s.cache.FindCache(api.InstanceCache, *find.ID, instance)
+ instanceRaw := &api.InstanceRaw{}
+ has, err := s.cache.FindCache(api.InstanceCache, *find.ID, instanceRaw)
if err != nil {
return nil, err
}
if has {
- return instance, nil
+ return instanceRaw, nil
}
}
@@ -171,7 +171,7 @@ func (s *InstanceService) FindInstance(ctx context.Context, find *api.InstanceFi
// PatchInstance updates an existing instance by ID.
// Returns ENOTFOUND if instance does not exist.
-func (s *InstanceService) PatchInstance(ctx context.Context, patch *api.InstancePatch) (*api.Instance, error) {
+func (s *InstanceService) PatchInstance(ctx context.Context, patch *api.InstancePatch) (*api.InstanceRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -195,7 +195,7 @@ func (s *InstanceService) PatchInstance(ctx context.Context, patch *api.Instance
}
// createInstance creates a new instance.
-func createInstance(ctx context.Context, tx *sql.Tx, create *api.InstanceCreate) (*api.Instance, error) {
+func createInstance(ctx context.Context, tx *sql.Tx, create *api.InstanceCreate) (*api.InstanceRaw, error) {
// Insert row into database.
row, err := tx.QueryContext(ctx, `
INSERT INTO instance (
@@ -227,29 +227,29 @@ func createInstance(ctx context.Context, tx *sql.Tx, create *api.InstanceCreate)
defer row.Close()
row.Next()
- var instance api.Instance
+ var instanceRaw api.InstanceRaw
if err := row.Scan(
- &instance.ID,
- &instance.RowStatus,
- &instance.CreatorID,
- &instance.CreatedTs,
- &instance.UpdaterID,
- &instance.UpdatedTs,
- &instance.EnvironmentID,
- &instance.Name,
- &instance.Engine,
- &instance.EngineVersion,
- &instance.ExternalLink,
- &instance.Host,
- &instance.Port,
+ &instanceRaw.ID,
+ &instanceRaw.RowStatus,
+ &instanceRaw.CreatorID,
+ &instanceRaw.CreatedTs,
+ &instanceRaw.UpdaterID,
+ &instanceRaw.UpdatedTs,
+ &instanceRaw.EnvironmentID,
+ &instanceRaw.Name,
+ &instanceRaw.Engine,
+ &instanceRaw.EngineVersion,
+ &instanceRaw.ExternalLink,
+ &instanceRaw.Host,
+ &instanceRaw.Port,
); err != nil {
return nil, FormatError(err)
}
- return &instance, nil
+ return &instanceRaw, nil
}
-func findInstanceList(ctx context.Context, tx *sql.Tx, find *api.InstanceFind) ([]*api.Instance, error) {
+func findInstanceList(ctx context.Context, tx *sql.Tx, find *api.InstanceFind) ([]*api.InstanceRaw, error) {
where, args := findInstanceQuery(find)
rows, err := tx.QueryContext(ctx, `
@@ -276,39 +276,39 @@ func findInstanceList(ctx context.Context, tx *sql.Tx, find *api.InstanceFind) (
}
defer rows.Close()
- // Iterate over result set and deserialize rows into instanceList.
- var instanceList []*api.Instance
+ // Iterate over result set and deserialize rows into instanceRawList.
+ var instanceRawList []*api.InstanceRaw
for rows.Next() {
- var instance api.Instance
+ var instanceRaw api.InstanceRaw
if err := rows.Scan(
- &instance.ID,
- &instance.RowStatus,
- &instance.CreatorID,
- &instance.CreatedTs,
- &instance.UpdaterID,
- &instance.UpdatedTs,
- &instance.EnvironmentID,
- &instance.Name,
- &instance.Engine,
- &instance.EngineVersion,
- &instance.ExternalLink,
- &instance.Host,
- &instance.Port,
+ &instanceRaw.ID,
+ &instanceRaw.RowStatus,
+ &instanceRaw.CreatorID,
+ &instanceRaw.CreatedTs,
+ &instanceRaw.UpdaterID,
+ &instanceRaw.UpdatedTs,
+ &instanceRaw.EnvironmentID,
+ &instanceRaw.Name,
+ &instanceRaw.Engine,
+ &instanceRaw.EngineVersion,
+ &instanceRaw.ExternalLink,
+ &instanceRaw.Host,
+ &instanceRaw.Port,
); err != nil {
return nil, FormatError(err)
}
- instanceList = append(instanceList, &instance)
+ instanceRawList = append(instanceRawList, &instanceRaw)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return instanceList, nil
+ return instanceRawList, nil
}
// patchInstance updates a instance by ID. Returns the new state of the instance after update.
-func patchInstance(ctx context.Context, tx *sql.Tx, patch *api.InstancePatch) (*api.Instance, error) {
+func patchInstance(ctx context.Context, tx *sql.Tx, patch *api.InstancePatch) (*api.InstanceRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.RowStatus; v != nil {
@@ -347,26 +347,26 @@ func patchInstance(ctx context.Context, tx *sql.Tx, patch *api.InstancePatch) (*
defer row.Close()
if row.Next() {
- var instance api.Instance
+ var instanceRaw api.InstanceRaw
if err := row.Scan(
- &instance.ID,
- &instance.RowStatus,
- &instance.CreatorID,
- &instance.CreatedTs,
- &instance.UpdaterID,
- &instance.UpdatedTs,
- &instance.EnvironmentID,
- &instance.Name,
- &instance.Engine,
- &instance.EngineVersion,
- &instance.ExternalLink,
- &instance.Host,
- &instance.Port,
+ &instanceRaw.ID,
+ &instanceRaw.RowStatus,
+ &instanceRaw.CreatorID,
+ &instanceRaw.CreatedTs,
+ &instanceRaw.UpdaterID,
+ &instanceRaw.UpdatedTs,
+ &instanceRaw.EnvironmentID,
+ &instanceRaw.Name,
+ &instanceRaw.Engine,
+ &instanceRaw.EngineVersion,
+ &instanceRaw.ExternalLink,
+ &instanceRaw.Host,
+ &instanceRaw.Port,
); err != nil {
return nil, FormatError(err)
}
- return &instance, nil
+ return &instanceRaw, nil
}
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("instance ID not found: %d", patch.ID)}