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.Backup (#839)

Yilong Lid2facb74aab4
diff --git a/api/backup.go b/api/backup.go
index a6e6329b78..ce873a45c7 100644
--- a/api/backup.go
+++ b/api/backup.go
@@ -78,6 +78,56 @@ func (e BackupStorageBackend) String() string {
return "UNKNOWN"
}
+// BackupRaw is the store model for an Backup.
+// Fields have exactly the same meanings as Backup.
+type BackupRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ DatabaseID int
+
+ // Domain specific fields
+ Name string
+ Status BackupStatus
+ Type BackupType
+ StorageBackend BackupStorageBackend
+ MigrationHistoryVersion string
+ Path string
+ Comment string
+}
+
+// ToBackup creates an instance of Backup based on the BackupRaw.
+// This is intended to be called when we need to compose an Backup relationship.
+func (raw *BackupRaw) ToBackup() *Backup {
+ return &Backup{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ DatabaseID: raw.DatabaseID,
+
+ // Domain specific fields
+ Name: raw.Name,
+ Status: raw.Status,
+ Type: raw.Type,
+ StorageBackend: raw.StorageBackend,
+ MigrationHistoryVersion: raw.MigrationHistoryVersion,
+ Path: raw.Path,
+ Comment: raw.Comment,
+ }
+}
+
// Backup is the API message for a backup.
type Backup struct {
ID int `jsonapi:"primary,backup"`
@@ -218,11 +268,11 @@ type BackupSettingsMatch struct {
// BackupService is the service for backups.
type BackupService interface {
- CreateBackup(ctx context.Context, create *BackupCreate) (*Backup, error)
- FindBackup(ctx context.Context, find *BackupFind) (*Backup, error)
+ CreateBackup(ctx context.Context, create *BackupCreate) (*BackupRaw, error)
+ FindBackup(ctx context.Context, find *BackupFind) (*BackupRaw, error)
// Returns backup list in updated_ts descending order.
- FindBackupList(ctx context.Context, find *BackupFind) ([]*Backup, error)
- PatchBackup(ctx context.Context, patch *BackupPatch) (*Backup, error)
+ FindBackupList(ctx context.Context, find *BackupFind) ([]*BackupRaw, error)
+ PatchBackup(ctx context.Context, patch *BackupPatch) (*BackupRaw, error)
FindBackupSetting(ctx context.Context, find *BackupSettingFind) (*BackupSetting, error)
UpsertBackupSetting(ctx context.Context, upsert *BackupSettingUpsert) (*BackupSetting, error)
UpsertBackupSettingTx(ctx context.Context, tx *sql.Tx, upsert *BackupSettingUpsert) (*BackupSetting, error)
diff --git a/api/project_webhook.go b/api/project_webhook.go
index b936639d4f..18d55383b9 100644
--- a/api/project_webhook.go
+++ b/api/project_webhook.go
@@ -19,7 +19,7 @@ type ProjectWebhook struct {
// Related fields
// Just returns ProjectID since it always operates within the project context
- ProjectID int `jsonapi:"attr,projecId"`
+ ProjectID int `jsonapi:"attr,projectId"`
// Domain specific fields
Type string `jsonapi:"attr,type"`
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index f6a07c28d3..2bd76d5177 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -28,7 +28,7 @@ const (
Snowflake Type = "SNOWFLAKE"
// SQLite is the database type for SQLite.
SQLite Type = "SQLITE"
- // TiDB is the database type for TIDB.
+ // TiDB is the database type for TiDB.
TiDB Type = "TIDB"
)
@@ -342,7 +342,7 @@ type MigrationHistory struct {
Payload string
}
-// MigrationHistoryFind is the API message for finding migration historys.
+// MigrationHistoryFind is the API message for finding migration histories.
type MigrationHistoryFind struct {
ID *int
diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.go
index 90cbb7825d..5b66ab1fa0 100644
--- a/server/anomaly_scanner.go
+++ b/server/anomaly_scanner.go
@@ -481,7 +481,7 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.I
DatabaseID: &database.ID,
Status: &status,
}
- backupList, err := s.server.BackupService.FindBackupList(ctx, backupFind)
+ backupRawList, err := s.server.BackupService.FindBackupList(ctx, backupFind)
if err != nil {
s.l.Error("Failed to retrieve backup list",
zap.String("instance", instance.Name),
@@ -490,8 +490,8 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.I
}
hasValidBackup := false
- if len(backupList) > 0 {
- if backupList[0].UpdatedTs >= time.Now().Add(-backupMaxAge).Unix() {
+ if len(backupRawList) > 0 {
+ if backupRawList[0].UpdatedTs >= time.Now().Add(-backupMaxAge).Unix() {
hasValidBackup = true
}
}
@@ -500,8 +500,8 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.I
backupMissingAnomalyPayload = &api.AnomalyDatabaseBackupMissingPayload{
ExpectedBackupSchedule: expectedSchedule,
}
- if len(backupList) > 0 {
- backupMissingAnomalyPayload.LastBackupTs = backupList[0].UpdatedTs
+ if len(backupRawList) > 0 {
+ backupMissingAnomalyPayload.LastBackupTs = backupRawList[0].UpdatedTs
}
}
}
diff --git a/server/backup_runner.go b/server/backup_runner.go
index 8a14fd4cf8..6b79146c7e 100644
--- a/server/backup_runner.go
+++ b/server/backup_runner.go
@@ -149,12 +149,12 @@ func (s *BackupRunner) scheduleBackupTask(ctx context.Context, database *api.Dat
return fmt.Errorf("failed to get migration history for database %q: %w", database.Name, err)
}
- // Return early if the backup already exists.
- backup, err := s.server.BackupService.FindBackup(ctx, &api.BackupFind{Name: &backupName})
+ // Return early if the backupRawOld already exists.
+ backupRawOld, err := s.server.BackupService.FindBackup(ctx, &api.BackupFind{Name: &backupName})
if err != nil {
return fmt.Errorf("failed to find backup %q, error %v", backupName, err)
}
- if backup != nil {
+ if backupRawOld != nil {
return nil
}
@@ -167,7 +167,7 @@ func (s *BackupRunner) scheduleBackupTask(ctx context.Context, database *api.Dat
StorageBackend: api.BackupStorageBackendLocal,
Path: path,
}
- backup, err = s.server.BackupService.CreateBackup(ctx, backupCreate)
+ backupRawNew, err := s.server.BackupService.CreateBackup(ctx, backupCreate)
if err != nil {
if common.ErrorCode(err) == common.Conflict {
// Automatic backup already exists.
@@ -177,7 +177,7 @@ func (s *BackupRunner) scheduleBackupTask(ctx context.Context, database *api.Dat
}
payload := api.TaskDatabaseBackupPayload{
- BackupID: backup.ID,
+ BackupID: backupRawNew.ID,
}
bytes, err := json.Marshal(payload)
if err != nil {
diff --git a/server/database.go b/server/database.go
index cc3b2d5928..8f06f99969 100644
--- a/server/database.go
+++ b/server/database.go
@@ -554,14 +554,15 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
}
backupCreate.MigrationHistoryVersion = version
- backup, err := s.BackupService.CreateBackup(ctx, backupCreate)
+ backupRaw, err := s.BackupService.CreateBackup(ctx, backupCreate)
if err != nil {
if common.ErrorCode(err) == common.Conflict {
return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Backup name already exists: %s", backupCreate.Name))
}
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create backup").SetInternal(err)
}
- if err := s.composeBackupRelationship(ctx, backup); err != nil {
+ backup, err := s.composeBackupRelationship(ctx, backupRaw)
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose backup relationship").SetInternal(err)
}
@@ -634,15 +635,18 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
backupFind := &api.BackupFind{
DatabaseID: &id,
}
- backupList, err := s.BackupService.FindBackupList(ctx, backupFind)
+ backupRawList, err := s.BackupService.FindBackupList(ctx, backupFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to backup list for database id: %d", id)).SetInternal(err)
}
- for _, backup := range backupList {
- if err := s.composeBackupRelationship(ctx, backup); err != nil {
+ var backupList []*api.Backup
+ for _, backupRaw := range backupRawList {
+ backup, err := s.composeBackupRelationship(ctx, backupRaw)
+ if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose backup relationship").SetInternal(err)
}
+ backupList = append(backupList, backup)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -1066,31 +1070,38 @@ func (s *Server) composeBackupByID(ctx context.Context, id int) (*api.Backup, er
backupFind := &api.BackupFind{
ID: &id,
}
- backup, err := s.BackupService.FindBackup(ctx, backupFind)
+ backupRaw, err := s.BackupService.FindBackup(ctx, backupFind)
if err != nil {
return nil, err
}
+ if backupRaw == nil {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("backup not found with ID %d", id)}
+ }
- if backup != nil {
- if err := s.composeBackupRelationship(ctx, backup); err != nil {
- return nil, err
- }
+ backup, err := s.composeBackupRelationship(ctx, backupRaw)
+ if err != nil {
+ return nil, err
}
+
return backup, nil
}
// composeBackupRelationship will compose the relationship of a backup.
-func (s *Server) composeBackupRelationship(ctx context.Context, backup *api.Backup) error {
- var err error
- backup.Creator, err = s.composePrincipalByID(ctx, backup.CreatorID)
+func (s *Server) composeBackupRelationship(ctx context.Context, raw *api.BackupRaw) (*api.Backup, error) {
+ backup := raw.ToBackup()
+ creator, err := s.composePrincipalByID(ctx, backup.CreatorID)
if err != nil {
- return err
+ return nil, err
}
- backup.Updater, err = s.composePrincipalByID(ctx, backup.UpdaterID)
+ backup.Creator = creator
+
+ updater, err := s.composePrincipalByID(ctx, backup.UpdaterID)
if err != nil {
- return err
+ return nil, err
}
- return nil
+ backup.Updater = updater
+
+ return backup, nil
}
// Try to get database driver using the instance's admin data source.
diff --git a/server/environment.go b/server/environment.go
index cb59636830..134ee223f7 100644
--- a/server/environment.go
+++ b/server/environment.go
@@ -160,7 +160,7 @@ func (s *Server) composeEnvironmentByID(ctx context.Context, id int) (*api.Envir
return nil, err
}
if envRaw == nil {
- return nil, fmt.Errorf("environment ID not found %v", id)
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("environment not found with ID %v", id)}
}
env, err := s.composeEnvironmentRelationship(ctx, envRaw)
diff --git a/server/instance.go b/server/instance.go
index a1af33ca40..d3b1188344 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -453,7 +453,7 @@ func (s *Server) composeInstanceByID(ctx context.Context, id int) (*api.Instance
return nil, err
}
if instance == nil {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("instance ID not found %v", id)}
+ 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 {
diff --git a/server/issue.go b/server/issue.go
index 43605d0fa5..6b549b79a2 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -261,7 +261,7 @@ func (s *Server) composeIssueByID(ctx context.Context, id int) (*api.Issue, erro
return nil, err
}
if id > 0 && issue == nil {
- return nil, fmt.Errorf("issue not found for ID %v", id)
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("Issue not found with ID %v", id)}
}
if issue != nil {
@@ -547,7 +547,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
// Validate the labels. Labels are set upon task completion.
if m.Labels != "" {
- if err := s.setDatabaseLabels(ctx, m.Labels, &api.Database{Name: m.DatabaseName, Instance: instance} /* dummp database */, project, 0 /* dummp updaterID */, true /* validateOnly */); err != nil {
+ if err := s.setDatabaseLabels(ctx, m.Labels, &api.Database{Name: m.DatabaseName, Instance: instance} /* dummy database */, project, 0 /* dummy updaterID */, true /* validateOnly */); err != nil {
return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid database label %q, error %v", m.Labels, err))
}
}
@@ -594,7 +594,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
}
if m.BackupID != 0 {
- backup, err := s.BackupService.FindBackup(ctx, &api.BackupFind{ID: &m.BackupID})
+ backupRaw, err := s.BackupService.FindBackup(ctx, &api.BackupFind{ID: &m.BackupID})
if err != nil {
return nil, fmt.Errorf("failed to find backup %v", m.BackupID)
}
@@ -607,7 +607,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
}
pipelineCreate = &api.PipelineCreate{
- Name: fmt.Sprintf("Pipeline - Create database %v from backup %v", payload.DatabaseName, backup.Name),
+ Name: fmt.Sprintf("Pipeline - Create database %v from backup %v", payload.DatabaseName, backupRaw.Name),
StageList: []api.StageCreate{
{
Name: "Create database",
@@ -629,7 +629,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
TaskList: []api.TaskCreate{
{
InstanceID: m.InstanceID,
- Name: fmt.Sprintf("Restore backup %v", backup.Name),
+ Name: fmt.Sprintf("Restore backup %v", backupRaw.Name),
Status: api.TaskPending,
Type: api.TaskDatabaseRestore,
DatabaseName: payload.DatabaseName,
@@ -919,7 +919,7 @@ func getDatabaseNameAndStatement(dbType db.Type, databaseName, characterSet, col
stmt = fmt.Sprintf("%s\nUSE DATABASE %s;\n%s", stmt, databaseName, schema)
}
case db.SQLite:
- // This is a fake CREATA DATABASE statement since a single SQLite file represents a database. Engine driver will recognize it and establish a connection to create the sqlite file representing the database.
+ // This is a fake CREATE DATABASE statement since a single SQLite file represents a database. Engine driver will recognize it and establish a connection to create the sqlite file representing the database.
stmt = fmt.Sprintf("CREATE DATABASE '%s';", databaseName)
}
diff --git a/server/pipeline.go b/server/pipeline.go
index 95a81338ff..ce281ea1d2 100644
--- a/server/pipeline.go
+++ b/server/pipeline.go
@@ -2,8 +2,10 @@ package server
import (
"context"
+ "fmt"
"github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
)
func (s *Server) composePipelineByID(ctx context.Context, id int) (*api.Pipeline, error) {
@@ -14,11 +16,12 @@ func (s *Server) composePipelineByID(ctx context.Context, id int) (*api.Pipeline
if err != nil {
return nil, err
}
+ if pipeline == nil {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("Pipeline not found with ID %v", id)}
+ }
- if pipeline != nil {
- if err := s.composePipelineRelationship(ctx, pipeline); err != nil {
- return nil, err
- }
+ if err := s.composePipelineRelationship(ctx, pipeline); err != nil {
+ return nil, err
}
return pipeline, nil
}
diff --git a/server/principal.go b/server/principal.go
index 70eab4a65a..e0242f2e5f 100644
--- a/server/principal.go
+++ b/server/principal.go
@@ -151,7 +151,7 @@ func (s *Server) composePrincipalByID(ctx context.Context, id int) (*api.Princip
return nil, err
}
if id > 0 && principal == nil {
- return nil, fmt.Errorf("principal not found for ID %v", id)
+ return nil, fmt.Errorf("Principal not found with ID %v", id)
}
if err = s.composePrincipalRole(ctx, principal); err != nil {
diff --git a/server/project.go b/server/project.go
index 39bbbf9db3..0b0bff769a 100644
--- a/server/project.go
+++ b/server/project.go
@@ -597,7 +597,7 @@ func (s *Server) composeProjectByID(ctx context.Context, id int) (*api.Project,
return nil, err
}
if projectRaw == nil {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project ID not found: %d", id)}
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("Project not found with ID %d", id)}
}
project, err := s.composeProjectRelationship(ctx, projectRaw)
diff --git a/server/task_executor_database_backup.go b/server/task_executor_database_backup.go
index 186953ea9e..01c5df4e79 100644
--- a/server/task_executor_database_backup.go
+++ b/server/task_executor_database_backup.go
@@ -46,19 +46,21 @@ func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *Ser
return true, nil, err
}
- backup, err := server.BackupService.FindBackup(ctx, &api.BackupFind{ID: &payload.BackupID})
+ backupRaw, err := server.BackupService.FindBackup(ctx, &api.BackupFind{ID: &payload.BackupID})
if err != nil {
return true, nil, fmt.Errorf("failed to find backup: %w", err)
}
- if backup == nil {
+ if backupRaw == nil {
return true, nil, fmt.Errorf("backup %v not found", payload.BackupID)
}
exec.l.Debug("Start database backup...",
zap.String("instance", task.Instance.Name),
zap.String("database", task.Database.Name),
- zap.String("backup", backup.Name),
+ zap.String("backup", backupRaw.Name),
)
+ // TODO(dragonly): refactor to get composed Backup
+ backup := backupRaw.ToBackup()
backupErr := exec.backupDatabase(ctx, task.Instance, task.Database.Name, backup, server.dataDir)
// Update the status of the backup.
newBackupStatus := string(api.BackupStatusDone)
@@ -67,8 +69,8 @@ func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *Ser
newBackupStatus = string(api.BackupStatusFailed)
comment = backupErr.Error()
}
- if _, err = server.BackupService.PatchBackup(ctx, &api.BackupPatch{
- ID: backup.ID,
+ if _, err := server.BackupService.PatchBackup(ctx, &api.BackupPatch{
+ ID: backupRaw.ID,
Status: newBackupStatus,
UpdaterID: api.SystemBotID,
Comment: comment,
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index c770a9adce..46e583691c 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -49,23 +49,23 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se
return true, nil, err
}
- backup, err := server.BackupService.FindBackup(ctx, &api.BackupFind{ID: &payload.BackupID})
+ backupRaw, err := server.BackupService.FindBackup(ctx, &api.BackupFind{ID: &payload.BackupID})
if err != nil {
return true, nil, fmt.Errorf("failed to find backup: %w", err)
}
- if backup == nil {
+ if backupRaw == nil {
return true, nil, fmt.Errorf("backup %v not found", payload.BackupID)
}
sourceDatabaseFind := &api.DatabaseFind{
- ID: &backup.DatabaseID,
+ ID: &backupRaw.DatabaseID,
}
sourceDatabase, err := server.composeDatabaseByFind(ctx, sourceDatabaseFind)
if err != nil {
return true, nil, fmt.Errorf("failed to find database for the backup: %w", err)
}
if sourceDatabase == nil {
- return true, nil, fmt.Errorf("source database ID not found %v", backup.DatabaseID)
+ return true, nil, fmt.Errorf("source database ID not found %v", backupRaw.DatabaseID)
}
targetDatabaseFind := &api.DatabaseFind{
@@ -85,9 +85,12 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se
zap.String("source_database", sourceDatabase.Name),
zap.String("target_instance", targetDatabase.Instance.Name),
zap.String("target_database", targetDatabase.Name),
- zap.String("backup", backup.Name),
+ zap.String("backup", backupRaw.Name),
)
+ // TODO(dragonly): refactor to composed Backup
+ backup := backupRaw.ToBackup()
+
// Restore the database to the target database.
if err := exec.restoreDatabase(ctx, targetDatabase.Instance, targetDatabase.Name, backup, server.dataDir); err != nil {
return true, nil, err
@@ -107,7 +110,7 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se
databasePatch := &api.DatabasePatch{
ID: targetDatabase.ID,
UpdaterID: api.SystemBotID,
- SourceBackupID: &backup.ID,
+ SourceBackupID: &backupRaw.ID,
}
if _, err = server.DatabaseService.PatchDatabase(ctx, databasePatch); err != nil {
return true, nil, fmt.Errorf("failed to patch database source backup ID after restore: %w", err)
@@ -117,7 +120,7 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se
server.syncEngineVersionAndSchema(ctx, targetDatabase.Instance)
return true, &api.TaskRunResultPayload{
- Detail: fmt.Sprintf("Restored database %q from backup %q", targetDatabase.Name, backup.Name),
+ Detail: fmt.Sprintf("Restored database %q from backup %q", targetDatabase.Name, backupRaw.Name),
MigrationID: migrationID,
Version: version,
}, nil
diff --git a/server/vcs.go b/server/vcs.go
index fee69f638a..951f815d7b 100644
--- a/server/vcs.go
+++ b/server/vcs.go
@@ -185,15 +185,15 @@ func (s *Server) composeVCSByID(ctx context.Context, id int) (*api.VCS, error) {
if err != nil {
return nil, err
}
+ if vcsRaw == nil {
+ return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("VCS not found with ID %v", id)}
+ }
- if vcsRaw != nil {
- vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
- if err != nil {
- return nil, err
- }
- return vcs, nil
+ vcs, err := s.composeVCSRelationship(ctx, vcsRaw)
+ if err != nil {
+ return nil, err
}
- return nil, nil
+ return vcs, nil
}
func (s *Server) composeVCSRelationship(ctx context.Context, raw *api.VCSRaw) (*api.VCS, error) {
diff --git a/store/backup.go b/store/backup.go
index 2c15ae1117..418e49a18d 100644
--- a/store/backup.go
+++ b/store/backup.go
@@ -28,14 +28,14 @@ func NewBackupService(logger *zap.Logger, db *DB, policyService api.PolicyServic
}
// CreateBackup creates a new backup.
-func (s *BackupService) CreateBackup(ctx context.Context, create *api.BackupCreate) (*api.Backup, error) {
+func (s *BackupService) CreateBackup(ctx context.Context, create *api.BackupCreate) (*api.BackupRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- backup, err := s.createBackup(ctx, tx.PTx, create)
+ backupRaw, err := s.createBackup(ctx, tx.PTx, create)
if err != nil {
return nil, err
}
@@ -44,57 +44,57 @@ func (s *BackupService) CreateBackup(ctx context.Context, create *api.BackupCrea
return nil, FormatError(err)
}
- return backup, nil
+ return backupRaw, nil
}
// FindBackup retrieves a single backup based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *BackupService) FindBackup(ctx context.Context, find *api.BackupFind) (*api.Backup, error) {
+func (s *BackupService) FindBackup(ctx context.Context, find *api.BackupFind) (*api.BackupRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := s.findBackupList(ctx, tx.PTx, find)
+ backupRawList, err := s.findBackupList(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
- if len(list) == 0 {
+ if len(backupRawList) == 0 {
return nil, nil
- } else if len(list) > 1 {
- return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d backups with filter %+v, expect 1. ", len(list), find)}
+ } else if len(backupRawList) > 1 {
+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d backups with filter %+v, expect 1. ", len(backupRawList), find)}
}
- return list[0], nil
+ return backupRawList[0], nil
}
// FindBackupList retrieves a list of backups based on find.
-func (s *BackupService) FindBackupList(ctx context.Context, find *api.BackupFind) ([]*api.Backup, error) {
+func (s *BackupService) FindBackupList(ctx context.Context, find *api.BackupFind) ([]*api.BackupRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := s.findBackupList(ctx, tx.PTx, find)
+ backupRawList, err := s.findBackupList(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
- return list, nil
+ return backupRawList, nil
}
// PatchBackup updates an existing backup by ID.
// Returns ENOTFOUND if backup does not exist.
-func (s *BackupService) PatchBackup(ctx context.Context, patch *api.BackupPatch) (*api.Backup, error) {
+func (s *BackupService) PatchBackup(ctx context.Context, patch *api.BackupPatch) (*api.BackupRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- backup, err := s.patchBackup(ctx, tx.PTx, patch)
+ backupRaw, err := s.patchBackup(ctx, tx.PTx, patch)
if err != nil {
return nil, FormatError(err)
}
@@ -103,11 +103,11 @@ func (s *BackupService) PatchBackup(ctx context.Context, patch *api.BackupPatch)
return nil, FormatError(err)
}
- return backup, nil
+ return backupRaw, nil
}
// createBackup creates a new backup.
-func (s *BackupService) createBackup(ctx context.Context, tx *sql.Tx, create *api.BackupCreate) (*api.Backup, error) {
+func (s *BackupService) createBackup(ctx context.Context, tx *sql.Tx, create *api.BackupCreate) (*api.BackupRaw, error) {
// Insert row into backup.
row, err := tx.QueryContext(ctx, `
INSERT INTO backup (
@@ -140,29 +140,29 @@ func (s *BackupService) createBackup(ctx context.Context, tx *sql.Tx, create *ap
defer row.Close()
row.Next()
- var backup api.Backup
+ var backupRaw api.BackupRaw
if err := row.Scan(
- &backup.ID,
- &backup.CreatorID,
- &backup.CreatedTs,
- &backup.UpdaterID,
- &backup.UpdatedTs,
- &backup.DatabaseID,
- &backup.Name,
- &backup.Status,
- &backup.Type,
- &backup.StorageBackend,
- &backup.MigrationHistoryVersion,
- &backup.Path,
- &backup.Comment,
+ &backupRaw.ID,
+ &backupRaw.CreatorID,
+ &backupRaw.CreatedTs,
+ &backupRaw.UpdaterID,
+ &backupRaw.UpdatedTs,
+ &backupRaw.DatabaseID,
+ &backupRaw.Name,
+ &backupRaw.Status,
+ &backupRaw.Type,
+ &backupRaw.StorageBackend,
+ &backupRaw.MigrationHistoryVersion,
+ &backupRaw.Path,
+ &backupRaw.Comment,
); err != nil {
return nil, FormatError(err)
}
- return &backup, nil
+ return &backupRaw, nil
}
-func (s *BackupService) findBackupList(ctx context.Context, tx *sql.Tx, find *api.BackupFind) ([]*api.Backup, error) {
+func (s *BackupService) findBackupList(ctx context.Context, tx *sql.Tx, find *api.BackupFind) ([]*api.BackupRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -202,39 +202,39 @@ func (s *BackupService) findBackupList(ctx context.Context, tx *sql.Tx, find *ap
}
defer rows.Close()
- // Iterate over result set and deserialize rows into backupList.
- var backupList []*api.Backup
+ // Iterate over result set and deserialize rows into backupRawList.
+ var backupRawList []*api.BackupRaw
for rows.Next() {
- var backup api.Backup
+ var backupRaw api.BackupRaw
if err := rows.Scan(
- &backup.ID,
- &backup.CreatorID,
- &backup.CreatedTs,
- &backup.UpdaterID,
- &backup.UpdatedTs,
- &backup.DatabaseID,
- &backup.Name,
- &backup.Status,
- &backup.Type,
- &backup.StorageBackend,
- &backup.MigrationHistoryVersion,
- &backup.Path,
- &backup.Comment,
+ &backupRaw.ID,
+ &backupRaw.CreatorID,
+ &backupRaw.CreatedTs,
+ &backupRaw.UpdaterID,
+ &backupRaw.UpdatedTs,
+ &backupRaw.DatabaseID,
+ &backupRaw.Name,
+ &backupRaw.Status,
+ &backupRaw.Type,
+ &backupRaw.StorageBackend,
+ &backupRaw.MigrationHistoryVersion,
+ &backupRaw.Path,
+ &backupRaw.Comment,
); err != nil {
return nil, FormatError(err)
}
- backupList = append(backupList, &backup)
+ backupRawList = append(backupRawList, &backupRaw)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return backupList, nil
+ return backupRawList, nil
}
// patchBackup updates a backup by ID. Returns the new state of the backup after update.
-func (s *BackupService) patchBackup(ctx context.Context, tx *sql.Tx, patch *api.BackupPatch) (*api.Backup, error) {
+func (s *BackupService) patchBackup(ctx context.Context, tx *sql.Tx, patch *api.BackupPatch) (*api.BackupRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
set, args = append(set, "status = $2"), append(args, patch.Status)
@@ -257,25 +257,25 @@ func (s *BackupService) patchBackup(ctx context.Context, tx *sql.Tx, patch *api.
defer row.Close()
if row.Next() {
- var backup api.Backup
+ var backupRaw api.BackupRaw
if err := row.Scan(
- &backup.ID,
- &backup.CreatorID,
- &backup.CreatedTs,
- &backup.UpdaterID,
- &backup.UpdatedTs,
- &backup.DatabaseID,
- &backup.Name,
- &backup.Status,
- &backup.Type,
- &backup.StorageBackend,
- &backup.MigrationHistoryVersion,
- &backup.Path,
- &backup.Comment,
+ &backupRaw.ID,
+ &backupRaw.CreatorID,
+ &backupRaw.CreatedTs,
+ &backupRaw.UpdaterID,
+ &backupRaw.UpdatedTs,
+ &backupRaw.DatabaseID,
+ &backupRaw.Name,
+ &backupRaw.Status,
+ &backupRaw.Type,
+ &backupRaw.StorageBackend,
+ &backupRaw.MigrationHistoryVersion,
+ &backupRaw.Path,
+ &backupRaw.Comment,
); err != nil {
return nil, FormatError(err)
}
- return &backup, nil
+ return &backupRaw, nil
}
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("backup ID not found: %d", patch.ID)}