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: decouple api/service/store packages for Backup & BackupSetting (#1032)
Yilong Li4316b078f84c
diff --git a/api/backup.go b/api/backup.goindex 78baf6cffb..e8d2980028 100644--- a/api/backup.go+++ b/api/backup.go@@ -1,8 +1,6 @@package apiimport (- "context"- "database/sql""encoding/json")@@ -78,56 +76,6 @@ 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"`@@ -205,52 +153,6 @@ type BackupPatch struct {Comment string}-// BackupSettingRaw is the store model for an BackupSetting.-// Fields have exactly the same meanings as BackupSetting.-type BackupSettingRaw struct {- ID int-- // Standard fields- CreatorID int- CreatedTs int64- UpdaterID int- UpdatedTs int64-- // Related fields- DatabaseID int-- // Domain specific fields- Enabled bool- Hour int- DayOfWeek int- // HookURL is the callback url to be requested (using HTTP GET) after a successful backup.- HookURL string-}--// ToBackupSetting creates an instance of BackupSetting based on the BackupSettingRaw.-// This is intended to be called when we need to compose an BackupSetting relationship.-func (raw *BackupSettingRaw) ToBackupSetting() *BackupSetting {- return &BackupSetting{- 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- Enabled: raw.Enabled,- Hour: raw.Hour,- DayOfWeek: raw.DayOfWeek,- // HookURL is the callback url to be requested (using HTTP GET) after a successful backup.- HookURL: raw.HookURL,- }-}-// BackupSetting is the backup setting for a database.type BackupSetting struct {ID int `jsonapi:"primary,backupSetting"`@@ -312,16 +214,3 @@ type BackupSettingsMatch struct {Hour intDayOfWeek int}--// BackupService is the service for backups.-type BackupService interface {- 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) ([]*BackupRaw, error)- PatchBackup(ctx context.Context, patch *BackupPatch) (*BackupRaw, error)- FindBackupSetting(ctx context.Context, find *BackupSettingFind) (*BackupSettingRaw, error)- UpsertBackupSetting(ctx context.Context, upsert *BackupSettingUpsert) (*BackupSettingRaw, error)- UpsertBackupSettingTx(ctx context.Context, tx *sql.Tx, upsert *BackupSettingUpsert) (*BackupSettingRaw, error)- FindBackupSettingsMatch(ctx context.Context, match *BackupSettingsMatch) ([]*BackupSettingRaw, error)-}diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.goindex b72fc87409..76e11dffdf 100644--- a/bin/server/cmd/root.go+++ b/bin/server/cmd/root.go@@ -370,8 +370,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.BackupService = store.NewBackupService(m.l, db, storeInstance)- s.DatabaseService = store.NewDatabaseService(m.l, db, cacheService, storeInstance, s.BackupService)+ s.DatabaseService = store.NewDatabaseService(m.l, db, cacheService, storeInstance)+ // TODO(dragonly): remove this hack+ storeInstance.DatabaseService = s.DatabaseServices.InstanceService = store.NewInstanceService(m.l, db, cacheService, s.DatabaseService, storeInstance)s.InstanceUserService = store.NewInstanceUserService(m.l, db)s.TableService = store.NewTableService(m.l, db)diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.goindex 145272781a..d3b9a0d461 100644--- a/server/anomaly_scanner.go+++ b/server/anomaly_scanner.go@@ -372,10 +372,7 @@ SchemaDriftEnd:func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.Instance, database *api.Database, policyMap map[int]*api.BackupPlanPolicy) {schedule := api.BackupPlanPolicyScheduleUnset- backupSettingFind := &api.BackupSettingFind{- DatabaseID: &database.ID,- }- backupSetting, err := s.server.BackupService.FindBackupSetting(ctx, backupSettingFind)+ backupSetting, err := s.server.store.GetBackupSettingByDatabaseID(ctx, database.ID)if err != nil {s.l.Error("Failed to retrieve backup setting",zap.String("instance", instance.Name),@@ -470,7 +467,7 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.IDatabaseID: &database.ID,Status: &status,}- backupRawList, err := s.server.BackupService.FindBackupList(ctx, backupFind)+ backupList, err := s.server.store.FindBackup(ctx, backupFind)if err != nil {s.l.Error("Failed to retrieve backup list",zap.String("instance", instance.Name),@@ -479,8 +476,8 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.I}hasValidBackup := false- if len(backupRawList) > 0 {- if backupRawList[0].UpdatedTs >= time.Now().Add(-backupMaxAge).Unix() {+ if len(backupList) > 0 {+ if backupList[0].UpdatedTs >= time.Now().Add(-backupMaxAge).Unix() {hasValidBackup = true}}@@ -489,8 +486,8 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.IbackupMissingAnomalyPayload = &api.AnomalyDatabaseBackupMissingPayload{ExpectedBackupSchedule: expectedSchedule,}- if len(backupRawList) > 0 {- backupMissingAnomalyPayload.LastBackupTs = backupRawList[0].UpdatedTs+ if len(backupList) > 0 {+ backupMissingAnomalyPayload.LastBackupTs = backupList[0].UpdatedTs}}}diff --git a/server/backup_runner.go b/server/backup_runner.goindex 06780d0a8c..ba20a0bb01 100644--- a/server/backup_runner.go+++ b/server/backup_runner.go@@ -58,17 +58,11 @@ func (s *BackupRunner) Run(ctx context.Context, wg *sync.WaitGroup) {Hour: t.Hour(),DayOfWeek: int(t.Weekday()),}- backupSettingRawList, err := s.server.BackupService.FindBackupSettingsMatch(ctx, match)+ backupSettingList, err := s.server.store.FindBackupSettingsMatch(ctx, match)if err != nil {s.l.Error("Failed to retrieve backup settings match", zap.Error(err))return}- // TODO(dragonly): implement composeBackupSettingRelation- var backupSettingList []*api.BackupSetting- for _, backupSettingRaw := range backupSettingRawList {- backupSetting := backupSettingRaw.ToBackupSetting()- backupSettingList = append(backupSettingList, backupSetting)- }for _, backupSetting := range backupSettingList {mu.Lock()@@ -156,12 +150,12 @@ func (s *BackupRunner) scheduleBackupTask(ctx context.Context, database *api.Datreturn fmt.Errorf("failed to get migration history for database %q: %w", database.Name, err)}- // Return early if the backupRawOld already exists.- backupRawOld, err := s.server.BackupService.FindBackup(ctx, &api.BackupFind{Name: &backupName})+ // Return early if the backupOld already exists.+ backupOld, err := s.server.store.FindBackup(ctx, &api.BackupFind{Name: &backupName})if err != nil {return fmt.Errorf("failed to find backup %q, error %v", backupName, err)}- if backupRawOld != nil {+ if backupOld != nil {return nil}@@ -174,7 +168,7 @@ func (s *BackupRunner) scheduleBackupTask(ctx context.Context, database *api.DatStorageBackend: api.BackupStorageBackendLocal,Path: path,}- backupRawNew, err := s.server.BackupService.CreateBackup(ctx, backupCreate)+ backupNew, err := s.server.store.CreateBackup(ctx, backupCreate)if err != nil {if common.ErrorCode(err) == common.Conflict {// Automatic backup already exists.@@ -184,7 +178,7 @@ func (s *BackupRunner) scheduleBackupTask(ctx context.Context, database *api.Dat}payload := api.TaskDatabaseBackupPayload{- BackupID: backupRawNew.ID,+ BackupID: backupNew.ID,}bytes, err := json.Marshal(payload)if err != nil {diff --git a/server/database.go b/server/database.goindex 90b3e6ec1c..03b29c5077 100644--- a/server/database.go+++ b/server/database.go@@ -572,17 +572,13 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}backupCreate.MigrationHistoryVersion = version- backupRaw, err := s.BackupService.CreateBackup(ctx, backupCreate)+ backup, err := s.store.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)}- backup, err := s.composeBackupRelationship(ctx, backupRaw)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose backup relationship").SetInternal(err)- }payload := api.TaskDatabaseBackupPayload{BackupID: backup.ID,@@ -653,20 +649,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {backupFind := &api.BackupFind{DatabaseID: &id,}- backupRawList, err := s.BackupService.FindBackupList(ctx, backupFind)+ backupList, err := s.store.FindBackup(ctx, backupFind)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to backup list for database id: %d", id)).SetInternal(err)}- 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)if err := jsonapi.MarshalPayload(c.Response().Writer, backupList); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal fetch backup list response: %v", id)).SetInternal(err)@@ -699,7 +686,7 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}backupSettingUpsert.EnvironmentID = db.Instance.Environment.ID- backupSetting, err := s.BackupService.UpsertBackupSetting(ctx, backupSettingUpsert)+ backupSetting, err := s.store.UpsertBackupSetting(ctx, backupSettingUpsert)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to set backup setting").SetInternal(err)}@@ -729,21 +716,16 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database not found with ID %d", id))}- backupSettingFind := &api.BackupSettingFind{- DatabaseID: &id,- }- backupSettingRaw, err := s.BackupService.FindBackupSetting(ctx, backupSettingFind)+ backupSetting, err := s.store.GetBackupSettingByDatabaseID(ctx, id)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get backup setting for database id: %d", id)).SetInternal(err)}- if backupSettingRaw == nil {+ if backupSetting == nil {// Returns the backup setting with UNKNOWN_ID to indicate the database has no backup- backupSettingRaw = &api.BackupSettingRaw{+ backupSetting = &api.BackupSetting{ID: api.UnknownID,}}- // TODO(dragonly): compose this- backupSetting := backupSettingRaw.ToBackupSetting()c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, backupSetting); err != nil {@@ -1014,7 +996,7 @@ func (s *Server) composeDatabaseRelationship(ctx context.Context, raw *api.Databdb.Instance = instanceif db.SourceBackupID != 0 {- sourceBackup, err := s.composeBackupByID(ctx, db.SourceBackupID)+ sourceBackup, err := s.store.GetBackupByID(ctx, db.SourceBackupID)if err != nil {return nil, err}@@ -1106,45 +1088,6 @@ func (s *Server) composeViewRelationship(ctx context.Context, raw *api.ViewRaw)return view, nil}-// composeBackupByID will compose the backup by backup ID.-func (s *Server) composeBackupByID(ctx context.Context, id int) (*api.Backup, error) {- backupFind := &api.BackupFind{- ID: &id,- }- 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)}- }-- 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, raw *api.BackupRaw) (*api.Backup, error) {- backup := raw.ToBackup()- creator, err := s.store.GetPrincipalByID(ctx, backup.CreatorID)- if err != nil {- return nil, err- }- backup.Creator = creator-- updater, err := s.store.GetPrincipalByID(ctx, backup.UpdaterID)- if err != nil {- return nil, err- }- backup.Updater = updater-- return backup, nil-}-// Try to get database driver using the instance's admin data source.// Upon successful return, caller MUST call driver.Close, otherwise, it will leak the database connection.func getAdminDatabaseDriver(ctx context.Context, instance *api.Instance, databaseName string, logger *zap.Logger) (db.Driver, error) {diff --git a/server/issue.go b/server/issue.goindex 3c4e53790e..976cc56755 100644--- a/server/issue.go+++ b/server/issue.go@@ -673,10 +673,13 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I}if m.BackupID != 0 {- backupRaw, err := s.BackupService.FindBackup(ctx, &api.BackupFind{ID: &m.BackupID})+ backup, err := s.store.GetBackupByID(ctx, m.BackupID)if err != nil {return nil, fmt.Errorf("failed to find backup %v", m.BackupID)}+ if backup == nil {+ return nil, fmt.Errorf("backup not found with ID[%d]", m.BackupID)+ }restorePayload := api.TaskDatabaseRestorePayload{}restorePayload.DatabaseName = m.DatabaseNamerestorePayload.BackupID = m.BackupID@@ -686,7 +689,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, backupRaw.Name),+ Name: fmt.Sprintf("Pipeline - Create database %v from backup %v", payload.DatabaseName, backup.Name),StageList: []api.StageCreate{{Name: "Create database",@@ -708,7 +711,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.ITaskList: []api.TaskCreate{{InstanceID: m.InstanceID,- Name: fmt.Sprintf("Restore backup %v", backupRaw.Name),+ Name: fmt.Sprintf("Restore backup %v", backup.Name),Status: api.TaskPending,Type: api.TaskDatabaseRestore,DatabaseName: payload.DatabaseName,diff --git a/server/server.go b/server/server.goindex ace84f923a..0490609ae9 100644--- a/server/server.go+++ b/server/server.go@@ -47,7 +47,6 @@ type Server struct {ColumnService api.ColumnServiceViewService api.ViewServiceIndexService api.IndexService- BackupService api.BackupServiceIssueService api.IssueServiceIssueSubscriberService api.IssueSubscriberServicePipelineService api.PipelineServicediff --git a/server/task_executor_database_backup.go b/server/task_executor_database_backup.goindex 3db5327a96..23b1add2ff 100644--- a/server/task_executor_database_backup.go+++ b/server/task_executor_database_backup.go@@ -42,21 +42,19 @@ func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *Serreturn true, nil, fmt.Errorf("invalid database backup payload: %w", err)}- backupRaw, err := server.BackupService.FindBackup(ctx, &api.BackupFind{ID: &payload.BackupID})+ backup, err := server.store.GetBackupByID(ctx, payload.BackupID)if err != nil {- return true, nil, fmt.Errorf("failed to find backup: %w", err)+ return true, nil, fmt.Errorf("Failed to find backup with ID[%d], error[%w]", payload.BackupID, err)}- if backupRaw == nil {+ if backup == 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", backupRaw.Name),+ zap.String("backup", backup.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)@@ -65,8 +63,8 @@ func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *SernewBackupStatus = string(api.BackupStatusFailed)comment = backupErr.Error()}- if _, err := server.BackupService.PatchBackup(ctx, &api.BackupPatch{- ID: backupRaw.ID,+ if _, err := server.store.PatchBackup(ctx, &api.BackupPatch{+ ID: backup.ID,Status: newBackupStatus,UpdaterID: api.SystemBotID,Comment: comment,diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.goindex 860f91d28a..23aaf76531 100644--- a/server/task_executor_database_restore.go+++ b/server/task_executor_database_restore.go@@ -45,23 +45,23 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Sereturn true, nil, fmt.Errorf("invalid database backup payload: %w", err)}- backupRaw, err := server.BackupService.FindBackup(ctx, &api.BackupFind{ID: &payload.BackupID})+ backup, err := server.store.GetBackupByID(ctx, payload.BackupID)if err != nil {- return true, nil, fmt.Errorf("failed to find backup: %w", err)+ return true, nil, fmt.Errorf("Failed to find backup with ID[%d], error[%w]", payload.BackupID, err)}- if backupRaw == nil {- return true, nil, fmt.Errorf("backup %v not found", payload.BackupID)+ if backup == nil {+ return true, nil, fmt.Errorf("backup with ID[%d] not found", payload.BackupID)}sourceDatabaseFind := &api.DatabaseFind{- ID: &backupRaw.DatabaseID,+ ID: &backup.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", backupRaw.DatabaseID)+ return true, nil, fmt.Errorf("source database ID not found %v", backup.DatabaseID)}targetDatabaseFind := &api.DatabaseFind{@@ -81,12 +81,9 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Sezap.String("source_database", sourceDatabase.Name),zap.String("target_instance", targetDatabase.Instance.Name),zap.String("target_database", targetDatabase.Name),- zap.String("backup", backupRaw.Name),+ zap.String("backup", backup.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@@ -106,7 +103,7 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *SedatabasePatch := &api.DatabasePatch{ID: targetDatabase.ID,UpdaterID: api.SystemBotID,- SourceBackupID: &backupRaw.ID,+ SourceBackupID: &backup.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)@@ -116,7 +113,7 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Seserver.syncEngineVersionAndSchema(ctx, targetDatabase.Instance)return true, &api.TaskRunResultPayload{- Detail: fmt.Sprintf("Restored database %q from backup %q", targetDatabase.Name, backupRaw.Name),+ Detail: fmt.Sprintf("Restored database %q from backup %q", targetDatabase.Name, backup.Name),MigrationID: migrationID,Version: version,}, nildiff --git a/store/backup.go b/store/backup.goindex 8246b01068..2776fb237b 100644--- a/store/backup.go+++ b/store/backup.go@@ -8,34 +8,266 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common"- "go.uber.org/zap")-var (- _ api.BackupService = (*BackupService)(nil)-)+// 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 api.BackupStatus+ Type api.BackupType+ StorageBackend api.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() *api.Backup {+ return &api.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,+ }+}++// backupSettingRaw is the store model for an BackupSetting.+// Fields have exactly the same meanings as BackupSetting.+type backupSettingRaw struct {+ ID int++ // Standard fields+ CreatorID int+ CreatedTs int64+ UpdaterID int+ UpdatedTs int64++ // Related fields+ DatabaseID int++ // Domain specific fields+ Enabled bool+ Hour int+ DayOfWeek int+ // HookURL is the callback url to be requested (using HTTP GET) after a successful backup.+ HookURL string+}++// toBackupSetting creates an instance of BackupSetting based on the backupSettingRaw.+// This is intended to be called when we need to compose an BackupSetting relationship.+func (raw *backupSettingRaw) toBackupSetting() *api.BackupSetting {+ return &api.BackupSetting{+ 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+ Enabled: raw.Enabled,+ Hour: raw.Hour,+ DayOfWeek: raw.DayOfWeek,+ // HookURL is the callback url to be requested (using HTTP GET) after a successful backup.+ HookURL: raw.HookURL,+ }+}++// CreateBackup creates an instance of Backup+func (s *Store) CreateBackup(ctx context.Context, create *api.BackupCreate) (*api.Backup, error) {+ backupRaw, err := s.createBackupRaw(ctx, create)+ if err != nil {+ return nil, fmt.Errorf("Failed to create Backup with BackupCreate[%+v], error[%w]", create, err)+ }+ backup, err := s.composeBackup(ctx, backupRaw)+ if err != nil {+ return nil, fmt.Errorf("Failed to compose Backup with backupRaw[%+v], error[%w]", backupRaw, err)+ }+ return backup, nil+}++// GetBackupByID gets an instance of Backup by ID+func (s *Store) GetBackupByID(ctx context.Context, id int) (*api.Backup, error) {+ backupRaw, err := s.getBackupRawByID(ctx, id)+ if err != nil {+ return nil, fmt.Errorf("Failed to get backup setting by ID[%d], error[%w]", id, err)+ }+ if backupRaw == nil {+ return nil, nil+ }+ backupSetting, err := s.composeBackup(ctx, backupRaw)+ if err != nil {+ return nil, fmt.Errorf("Failed to compose Backup with backupRaw[%+v], error[%w]", backupRaw, err)+ }+ return backupSetting, nil+}++// FindBackup finds a list of Backup instances+func (s *Store) FindBackup(ctx context.Context, find *api.BackupFind) ([]*api.Backup, error) {+ backupRawList, err := s.findBackupRaw(ctx, find)+ if err != nil {+ return nil, fmt.Errorf("Failed to find Backup list with BackupFind[%+v], error[%w]", find, err)+ }+ var backupList []*api.Backup+ for _, raw := range backupRawList {+ backup, err := s.composeBackup(ctx, raw)+ if err != nil {+ return nil, fmt.Errorf("Failed to compose Backup with backupRaw[%+v], error[%w]", raw, err)+ }+ backupList = append(backupList, backup)+ }+ return backupList, nil+}++// PatchBackup patches an instance of Backup+func (s *Store) PatchBackup(ctx context.Context, patch *api.BackupPatch) (*api.Backup, error) {+ backupRaw, err := s.patchBackupRaw(ctx, patch)+ if err != nil {+ return nil, fmt.Errorf("Failed to patch Backup with BackupPatch[%+v], error[%w]", patch, err)+ }+ backup, err := s.composeBackup(ctx, backupRaw)+ if err != nil {+ return nil, fmt.Errorf("Failed to compose Backup with backupRaw[%+v], error[%w]", backupRaw, err)+ }+ return backup, nil+}-// BackupService represents a service for managing backup.-type BackupService struct {- l *zap.Logger- db *DB- store *Store+// GetBackupSettingByDatabaseID gets an instance of BackupSetting by ID+func (s *Store) GetBackupSettingByDatabaseID(ctx context.Context, id int) (*api.BackupSetting, error) {+ backupSettingRaw, err := s.getBackupSettingRaw(ctx, &api.BackupSettingFind{DatabaseID: &id})+ if err != nil {+ return nil, fmt.Errorf("Failed to get backup setting by ID[%d], error[%w]", id, err)+ }+ if backupSettingRaw == nil {+ return nil, nil+ }+ backupSetting, err := s.composeBackupSetting(ctx, backupSettingRaw)+ if err != nil {+ return nil, fmt.Errorf("Failed to compose BackupSetting with backupSettingRaw[%+v], error[%w]", backupSettingRaw, err)+ }+ return backupSetting, nil+}++// UpsertBackupSetting upserts an instance of backup setting+func (s *Store) UpsertBackupSetting(ctx context.Context, upsert *api.BackupSettingUpsert) (*api.BackupSetting, error) {+ backupSettingRaw, err := s.upsertBackupSettingRaw(ctx, upsert)+ if err != nil {+ return nil, fmt.Errorf("Failed to upsert backup setting with BackupSettingUpsert[%+v], error[%w]", upsert, err)+ }+ backup, err := s.composeBackupSetting(ctx, backupSettingRaw)+ if err != nil {+ return nil, fmt.Errorf("Failed to compose Backup with backupRaw[%+v], error[%w]", backupSettingRaw, err)+ }+ return backup, nil+}++// FindBackupSettingsMatch finds a list of backup setting instances with match conditions+func (s *Store) FindBackupSettingsMatch(ctx context.Context, match *api.BackupSettingsMatch) ([]*api.BackupSetting, error) {+ backupSettingRawList, err := s.findBackupSettingsMatchImpl(ctx, match)+ if err != nil {+ return nil, fmt.Errorf("Failed to find matching backup setting list with BackupSettingsMatch[%+v], error[%w]", match, err)+ }+ var backupSettingList []*api.BackupSetting+ for _, raw := range backupSettingRawList {+ backupSetting, err := s.composeBackupSetting(ctx, raw)+ if err != nil {+ return nil, fmt.Errorf("Failed to compose Backup with backupRaw[%+v], error[%w]", raw, err)+ }+ backupSettingList = append(backupSettingList, backupSetting)+ }+ return backupSettingList, nil}-// NewBackupService returns a new instance of BackupService.-func NewBackupService(logger *zap.Logger, db *DB, store *Store) *BackupService {- return &BackupService{l: logger, db: db, store: store}+//+// private functions+//++// composeBackup composes an instance of Backup by backupRaw+func (s *Store) composeBackup(ctx context.Context, raw *backupRaw) (*api.Backup, error) {+ backup := raw.toBackup()++ creator, err := s.GetPrincipalByID(ctx, backup.CreatorID)+ if err != nil {+ return nil, err+ }+ backup.Creator = creator++ updater, err := s.GetPrincipalByID(ctx, backup.UpdaterID)+ if err != nil {+ return nil, err+ }+ backup.Updater = updater++ return backup, nil+}++func (s *Store) composeBackupSetting(ctx context.Context, raw *backupSettingRaw) (*api.BackupSetting, error) {+ backupSetting := raw.toBackupSetting()++ creator, err := s.GetPrincipalByID(ctx, backupSetting.CreatorID)+ if err != nil {+ return nil, err+ }+ backupSetting.Creator = creator++ updater, err := s.GetPrincipalByID(ctx, backupSetting.UpdaterID)+ if err != nil {+ return nil, err+ }+ backupSetting.Updater = updater++ databaseRaw, err := s.DatabaseService.FindDatabase(ctx, &api.DatabaseFind{ID: &backupSetting.DatabaseID})+ if err != nil {+ return nil, err+ }+ // TODO(dragonly): compose Database+ backupSetting.Database = databaseRaw.ToDatabase()++ return backupSetting, nil}-// CreateBackup creates a new backup.-func (s *BackupService) CreateBackup(ctx context.Context, create *api.BackupCreate) (*api.BackupRaw, error) {+// createBackupRaw creates a new backup.+func (s *Store) createBackupRaw(ctx context.Context, create *api.BackupCreate) (*backupRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)}defer tx.PTx.Rollback()- backupRaw, err := s.createBackup(ctx, tx.PTx, create)+ backupRaw, err := s.createBackupImpl(ctx, tx.PTx, create)if err != nil {return nil, err}@@ -47,16 +279,17 @@ func (s *BackupService) CreateBackup(ctx context.Context, create *api.BackupCreareturn backupRaw, nil}-// FindBackup retrieves a single backup based on find.+// getBackupRawByID 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.BackupRaw, error) {+func (s *Store) getBackupRawByID(ctx context.Context, id int) (*backupRaw, error) {+ find := &api.BackupFind{ID: &id}tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)}defer tx.PTx.Rollback()- backupRawList, err := s.findBackupList(ctx, tx.PTx, find)+ backupRawList, err := s.findBackupImpl(ctx, tx.PTx, find)if err != nil {return nil, err}@@ -69,15 +302,15 @@ func (s *BackupService) FindBackup(ctx context.Context, find *api.BackupFind) (*return backupRawList[0], nil}-// FindBackupList retrieves a list of backups based on find.-func (s *BackupService) FindBackupList(ctx context.Context, find *api.BackupFind) ([]*api.BackupRaw, error) {+// findBackupRaw retrieves a list of backups based on find.+func (s *Store) findBackupRaw(ctx context.Context, find *api.BackupFind) ([]*backupRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)}defer tx.PTx.Rollback()- backupRawList, err := s.findBackupList(ctx, tx.PTx, find)+ backupRawList, err := s.findBackupImpl(ctx, tx.PTx, find)if err != nil {return nil, err}@@ -85,16 +318,16 @@ func (s *BackupService) FindBackupList(ctx context.Context, find *api.BackupFindreturn backupRawList, nil}-// PatchBackup updates an existing backup by ID.+// patchBackupRaw updates an existing backup by ID.// Returns ENOTFOUND if backup does not exist.-func (s *BackupService) PatchBackup(ctx context.Context, patch *api.BackupPatch) (*api.BackupRaw, error) {+func (s *Store) patchBackupRaw(ctx context.Context, patch *api.BackupPatch) (*backupRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)}defer tx.PTx.Rollback()- backupRaw, err := s.patchBackup(ctx, tx.PTx, patch)+ backupRaw, err := s.patchBackupImpl(ctx, tx.PTx, patch)if err != nil {return nil, FormatError(err)}@@ -106,8 +339,49 @@ func (s *BackupService) PatchBackup(ctx context.Context, patch *api.BackupPatch)return backupRaw, nil}-// createBackup creates a new backup.-func (s *BackupService) createBackup(ctx context.Context, tx *sql.Tx, create *api.BackupCreate) (*api.BackupRaw, error) {+// upsertBackupSettingRaw sets the backup settings for a database.+func (s *Store) upsertBackupSettingRaw(ctx context.Context, upsert *api.BackupSettingUpsert) (*backupSettingRaw, error) {+ backupPlanPolicy, err := s.GetBackupPlanPolicyByEnvID(ctx, upsert.EnvironmentID)+ if err != nil {+ return nil, err+ }+ // Backup plan policy check for backup setting mutation.+ if backupPlanPolicy.Schedule != api.BackupPlanPolicyScheduleUnset {+ if !upsert.Enabled {+ return nil, &common.Error{Code: common.Invalid, Err: fmt.Errorf("backup setting should not be disabled for backup plan policy schedule %q", backupPlanPolicy.Schedule)}+ }+ switch backupPlanPolicy.Schedule {+ case api.BackupPlanPolicyScheduleDaily:+ if upsert.DayOfWeek != -1 {+ return nil, &common.Error{Code: common.Invalid, Err: fmt.Errorf("backup setting DayOfWeek should be unset for backup plan policy schedule %q", backupPlanPolicy.Schedule)}+ }+ case api.BackupPlanPolicyScheduleWeekly:+ if upsert.DayOfWeek == -1 {+ return nil, &common.Error{Code: common.Invalid, Err: fmt.Errorf("backup setting DayOfWeek should be set for backup plan policy schedule %q", backupPlanPolicy.Schedule)}+ }+ }+ }++ tx, err := s.db.BeginTx(ctx, nil)+ if err != nil {+ return nil, FormatError(err)+ }+ defer tx.PTx.Rollback()++ backupRaw, err := s.upsertBackupSettingImpl(ctx, tx.PTx, upsert)+ if err != nil {+ return nil, err+ }++ if err := tx.PTx.Commit(); err != nil {+ return nil, FormatError(err)+ }++ return backupRaw, nil+}++// createBackupImpl creates a new backup.+func (s *Store) createBackupImpl(ctx context.Context, tx *sql.Tx, create *api.BackupCreate) (*backupRaw, error) {// Insert row into backup.row, err := tx.QueryContext(ctx, `INSERT INTO backup (@@ -140,7 +414,7 @@ func (s *BackupService) createBackup(ctx context.Context, tx *sql.Tx, create *apdefer row.Close()row.Next()- var backupRaw api.BackupRaw+ var backupRaw backupRawif err := row.Scan(&backupRaw.ID,&backupRaw.CreatorID,@@ -162,7 +436,7 @@ func (s *BackupService) createBackup(ctx context.Context, tx *sql.Tx, create *apreturn &backupRaw, nil}-func (s *BackupService) findBackupList(ctx context.Context, tx *sql.Tx, find *api.BackupFind) ([]*api.BackupRaw, error) {+func (s *Store) findBackupImpl(ctx context.Context, tx *sql.Tx, find *api.BackupFind) ([]*backupRaw, error) {// Build WHERE clause.where, args := []string{"1 = 1"}, []interface{}{}if v := find.ID; v != nil {@@ -203,9 +477,9 @@ func (s *BackupService) findBackupList(ctx context.Context, tx *sql.Tx, find *apdefer rows.Close()// Iterate over result set and deserialize rows into backupRawList.- var backupRawList []*api.BackupRaw+ var backupRawList []*backupRawfor rows.Next() {- var backupRaw api.BackupRaw+ var backupRaw backupRawif err := rows.Scan(&backupRaw.ID,&backupRaw.CreatorID,@@ -233,8 +507,8 @@ func (s *BackupService) findBackupList(ctx context.Context, tx *sql.Tx, find *apreturn 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.BackupRaw, error) {+// patchBackupImpl updates a backup by ID. Returns the new state of the backup after update.+func (s *Store) patchBackupImpl(ctx context.Context, tx *sql.Tx, patch *api.BackupPatch) (*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,7 +531,7 @@ func (s *BackupService) patchBackup(ctx context.Context, tx *sql.Tx, patch *api.defer row.Close()if row.Next() {- var backupRaw api.BackupRaw+ var backupRaw backupRawif err := row.Scan(&backupRaw.ID,&backupRaw.CreatorID,@@ -281,16 +555,16 @@ func (s *BackupService) patchBackup(ctx context.Context, tx *sql.Tx, patch *api.return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("backup ID not found: %d", patch.ID)}}-// FindBackupSetting finds the backup setting for a database.+// getBackupSettingRaw finds the backup setting for a database.// Returns ECONFLICT if finding more than 1 matching records.-func (s *BackupService) FindBackupSetting(ctx context.Context, find *api.BackupSettingFind) (*api.BackupSettingRaw, error) {+func (s *Store) getBackupSettingRaw(ctx context.Context, find *api.BackupSettingFind) (*backupSettingRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)}defer tx.PTx.Rollback()- list, err := s.findBackupSetting(ctx, tx.PTx, find)+ list, err := s.findBackupSettingImpl(ctx, tx.PTx, find)if err != nil {return nil, err}@@ -303,7 +577,7 @@ func (s *BackupService) FindBackupSetting(ctx context.Context, find *api.BackupSreturn list[0], nil}-func (s *BackupService) findBackupSetting(ctx context.Context, tx *sql.Tx, find *api.BackupSettingFind) ([]*api.BackupSettingRaw, error) {+func (s *Store) findBackupSettingImpl(ctx context.Context, tx *sql.Tx, find *api.BackupSettingFind) ([]*backupSettingRaw, error) {// Build WHERE clause.where, args := []string{"1 = 1"}, []interface{}{}if v := find.ID; v != nil {@@ -335,9 +609,9 @@ func (s *BackupService) findBackupSetting(ctx context.Context, tx *sql.Tx, finddefer rows.Close()// Iterate over result set and deserialize rows into backupSettingRawList.- var backupSettingRawList []*api.BackupSettingRaw+ var backupSettingRawList []*backupSettingRawfor rows.Next() {- var backupSettingRaw api.BackupSettingRaw+ var backupSettingRaw backupSettingRawif err := rows.Scan(&backupSettingRaw.ID,&backupSettingRaw.CreatorID,@@ -362,49 +636,8 @@ func (s *BackupService) findBackupSetting(ctx context.Context, tx *sql.Tx, findreturn backupSettingRawList, nil}-// UpsertBackupSetting sets the backup settings for a database.-func (s *BackupService) UpsertBackupSetting(ctx context.Context, upsert *api.BackupSettingUpsert) (*api.BackupSettingRaw, error) {- backupPlanPolicy, err := s.store.GetBackupPlanPolicyByEnvID(ctx, upsert.EnvironmentID)- if err != nil {- return nil, err- }- // Backup plan policy check for backup setting mutation.- if backupPlanPolicy.Schedule != api.BackupPlanPolicyScheduleUnset {- if !upsert.Enabled {- return nil, &common.Error{Code: common.Invalid, Err: fmt.Errorf("backup setting should not be disabled for backup plan policy schedule %q", backupPlanPolicy.Schedule)}- }- switch backupPlanPolicy.Schedule {- case api.BackupPlanPolicyScheduleDaily:- if upsert.DayOfWeek != -1 {- return nil, &common.Error{Code: common.Invalid, Err: fmt.Errorf("backup setting DayOfWeek should be unset for backup plan policy schedule %q", backupPlanPolicy.Schedule)}- }- case api.BackupPlanPolicyScheduleWeekly:- if upsert.DayOfWeek == -1 {- return nil, &common.Error{Code: common.Invalid, Err: fmt.Errorf("backup setting DayOfWeek should be set for backup plan policy schedule %q", backupPlanPolicy.Schedule)}- }- }- }-- tx, err := s.db.BeginTx(ctx, nil)- if err != nil {- return nil, FormatError(err)- }- defer tx.PTx.Rollback()-- backupRaw, err := s.UpsertBackupSettingTx(ctx, tx.PTx, upsert)- if err != nil {- return nil, err- }-- if err := tx.PTx.Commit(); err != nil {- return nil, FormatError(err)- }-- return backupRaw, nil-}--// UpsertBackupSettingTx updates an existing backup setting.-func (s *BackupService) UpsertBackupSettingTx(ctx context.Context, tx *sql.Tx, upsert *api.BackupSettingUpsert) (*api.BackupSettingRaw, error) {+// upsertBackupSettingImpl updates an existing backup setting.+func (s *Store) upsertBackupSettingImpl(ctx context.Context, tx *sql.Tx, upsert *api.BackupSettingUpsert) (*backupSettingRaw, error) {// Upsert row into backup_setting.row, err := tx.QueryContext(ctx, `INSERT INTO backup_setting (@@ -439,7 +672,7 @@ func (s *BackupService) UpsertBackupSettingTx(ctx context.Context, tx *sql.Tx, udefer row.Close()row.Next()- var backupSettingRaw api.BackupSettingRaw+ var backupSettingRaw backupSettingRawif err := row.Scan(&backupSettingRaw.ID,&backupSettingRaw.CreatorID,@@ -458,8 +691,8 @@ func (s *BackupService) UpsertBackupSettingTx(ctx context.Context, tx *sql.Tx, ureturn &backupSettingRaw, nil}-// FindBackupSettingsMatch retrieves a list of backup settings based on match condition.-func (s *BackupService) FindBackupSettingsMatch(ctx context.Context, match *api.BackupSettingsMatch) ([]*api.BackupSettingRaw, error) {+// findBackupSettingsMatchImpl retrieves a list of backup settings based on match condition.+func (s *Store) findBackupSettingsMatchImpl(ctx context.Context, match *api.BackupSettingsMatch) ([]*backupSettingRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -497,9 +730,9 @@ func (s *BackupService) FindBackupSettingsMatch(ctx context.Context, match *api.defer rows.Close()// Iterate over result set and deserialize rows into backupSettingRawList.- var backupSettingRawList []*api.BackupSettingRaw+ var backupSettingRawList []*backupSettingRawfor rows.Next() {- var backupSettingRaw api.BackupSettingRaw+ var backupSettingRaw backupSettingRawif err := rows.Scan(&backupSettingRaw.ID,&backupSettingRaw.CreatorID,diff --git a/store/database.go b/store/database.goindex 41113bd928..bbd35a9cbd 100644--- a/store/database.go+++ b/store/database.go@@ -21,19 +21,17 @@ type DatabaseService struct {l *zap.Loggerdb *DB- cache api.CacheService- store *Store- backupService api.BackupService+ cache api.CacheService+ store *Store}// NewDatabaseService returns a new instance of DatabaseService.-func NewDatabaseService(logger *zap.Logger, db *DB, cache api.CacheService, store *Store, backupService api.BackupService) *DatabaseService {+func NewDatabaseService(logger *zap.Logger, db *DB, cache api.CacheService, store *Store) *DatabaseService {return &DatabaseService{- l: logger,- db: db,- cache: cache,- store: store,- backupService: backupService,+ l: logger,+ db: db,+ cache: cache,+ store: store,}}@@ -64,7 +62,7 @@ func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, creareturn nil, err}- database, err := s.createDatabase(ctx, tx, create)+ databaseRaw, err := s.createDatabase(ctx, tx, create)if err != nil {return nil, err}@@ -73,7 +71,7 @@ func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, creaif backupPlanPolicy.Schedule != api.BackupPlanPolicyScheduleUnset {backupSettingUpsert := &api.BackupSettingUpsert{UpdaterID: api.SystemBotID,- DatabaseID: database.ID,+ DatabaseID: databaseRaw.ID,Enabled: true,Hour: rand.Intn(24),HookURL: "",@@ -84,16 +82,16 @@ func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, creacase api.BackupPlanPolicyScheduleWeekly:backupSettingUpsert.DayOfWeek = rand.Intn(7)}- if _, err := s.backupService.UpsertBackupSettingTx(ctx, tx, backupSettingUpsert); err != nil {+ if _, err := s.store.upsertBackupSettingImpl(ctx, tx, backupSettingUpsert); err != nil {return nil, err}}- if err := s.cache.UpsertCache(api.DatabaseCache, database.ID, database); err != nil {+ if err := s.cache.UpsertCache(api.DatabaseCache, databaseRaw.ID, databaseRaw); err != nil {return nil, err}- return database, nil+ return databaseRaw, nil}// FindDatabaseList retrieves a list of databases based on find.diff --git a/store/member.go b/store/member.goindex a70d6d8509..16cf3dc68c 100644--- a/store/member.go+++ b/store/member.go@@ -71,7 +71,7 @@ func (s *Store) FindMember(ctx context.Context, find *api.MemberFind) ([]*api.Mefor _, raw := range memberRawList {member, err := s.composeMember(ctx, raw)if err != nil {- return nil, fmt.Errorf("Failed to compose Member role with memberRaw[%+v], error[%w]", raw, err)+ return nil, fmt.Errorf("Failed to compose Member with memberRaw[%+v], error[%w]", raw, err)}memberList = append(memberList, member)}@@ -90,7 +90,7 @@ func (s *Store) GetMemberByPrincipalID(ctx context.Context, id int) (*api.Member}member, err := s.composeMember(ctx, memberRaw)if err != nil {- return nil, fmt.Errorf("Failed to compose Member role with memberRaw[%+v], error[%w]", memberRaw, err)+ return nil, fmt.Errorf("Failed to compose Member with memberRaw[%+v], error[%w]", memberRaw, err)}return member, nil}@@ -107,7 +107,7 @@ func (s *Store) GetMemberByID(ctx context.Context, id int) (*api.Member, error)}member, err := s.composeMember(ctx, memberRaw)if err != nil {- return nil, fmt.Errorf("Failed to compose Member role with memberRaw[%+v], error[%w]", memberRaw, err)+ return nil, fmt.Errorf("Failed to compose Member with memberRaw[%+v], error[%w]", memberRaw, err)}return member, nil}@@ -120,7 +120,7 @@ func (s *Store) PatchMember(ctx context.Context, patch *api.MemberPatch) (*api.M}member, err := s.composeMember(ctx, memberRaw)if err != nil {- return nil, fmt.Errorf("Failed to compose Member role with memberRaw[%+v], error[%w]", memberRaw, err)+ return nil, fmt.Errorf("Failed to compose Member with memberRaw[%+v], error[%w]", memberRaw, err)}return member, nil}diff --git a/store/principal.go b/store/principal.goindex 9a75e0f365..a186198814 100644--- a/store/principal.go+++ b/store/principal.go@@ -251,6 +251,7 @@ func (s *Store) composePrincipal(ctx context.Context, raw *principalRaw) (*api.Pprincipal.Role = api.Owner} else {find := &api.MemberFind{PrincipalID: &principal.ID}+ // NOTE: watch out for recursion here, because Member also contains pointers to PrincipalmemberRaw, err := s.getMemberRaw(ctx, find)if err != nil {return nil, errdiff --git a/store/store.go b/store/store.goindex 2d03e45c87..d2e0f439bb 100644--- a/store/store.go+++ b/store/store.go@@ -7,9 +7,10 @@ import (// Store provides database access to all raw objectstype Store struct {- l *zap.Logger- db *DB- cache api.CacheService+ l *zap.Logger+ db *DB+ cache api.CacheService+ DatabaseService api.DatabaseService}// New creates a new instance of Store