Skip to content

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

refactor: store return nil on backup setting non-existence (#302)

spinningbotec9508ed9bc9
diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.go
index 23982cb9c4..072bae4223 100644
--- a/server/anomaly_scanner.go
+++ b/server/anomaly_scanner.go
@@ -377,20 +377,18 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.I
}
backupSetting, err := s.server.BackupService.FindBackupSetting(ctx, backupSettingFind)
if err != nil {
- if common.ErrorCode(err) != common.NotFound {
- s.l.Error("Failed to retrieve backup setting",
- zap.String("instance", instance.Name),
- zap.String("database", database.Name),
- zap.Error(err))
- return
- }
- } else {
- if backupSetting.Enabled && backupSetting.Hour != -1 {
- if backupSetting.DayOfWeek == -1 {
- schedule = api.BackupPlanPolicyScheduleDaily
- } else {
- schedule = api.BackupPlanPolicyScheduleWeekly
- }
+ s.l.Error("Failed to retrieve backup setting",
+ zap.String("instance", instance.Name),
+ zap.String("database", database.Name),
+ zap.Error(err))
+ return
+ }
+
+ if backupSetting != nil && backupSetting.Enabled && backupSetting.Hour != -1 {
+ if backupSetting.DayOfWeek == -1 {
+ schedule = api.BackupPlanPolicyScheduleDaily
+ } else {
+ schedule = api.BackupPlanPolicyScheduleWeekly
}
}
diff --git a/server/database.go b/server/database.go
index 34379aca63..bc6404856b 100644
--- a/server/database.go
+++ b/server/database.go
@@ -655,13 +655,12 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
}
backupSetting, err := s.BackupService.FindBackupSetting(ctx, backupSettingFind)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- // Returns the backup setting with UNKNOWN_ID to indicate the database has no backup
- backupSetting = &api.BackupSetting{
- ID: api.UnknownID,
- }
- } else {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get backup setting for database id: %d", id)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get backup setting for database id: %d", id)).SetInternal(err)
+ }
+ if backupSetting == nil {
+ // Returns the backup setting with UNKNOWN_ID to indicate the database has no backup
+ backupSetting = &api.BackupSetting{
+ ID: api.UnknownID,
}
}
diff --git a/store/backup.go b/store/backup.go
index e05dad75fb..5357868c1a 100644
--- a/store/backup.go
+++ b/store/backup.go
@@ -283,7 +283,6 @@ func (s *BackupService) patchBackup(ctx context.Context, tx *Tx, patch *api.Back
}
// FindBackupSetting finds the backup setting for a database.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *BackupService) FindBackupSetting(ctx context.Context, find *api.BackupSettingFind) (*api.BackupSetting, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -295,12 +294,13 @@ func (s *BackupService) FindBackupSetting(ctx context.Context, find *api.BackupS
list, err := s.findBackupSetting(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("backup setting not found: %+v", find)}
+ }
+
+ if len(list) == 0 {
+ return nil, nil
} else if len(list) > 1 {
return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d backup settings with filter %+v, expect 1. ", len(list), find)}
}
-
return list[0], nil
}