Skip to content

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

add backup find method

spinningbot16a54ba9c16b
diff --git a/api/backup.go b/api/backup.go
index 722ca7155a..9f8a1fd7e4 100644
--- a/api/backup.go
+++ b/api/backup.go
@@ -77,6 +77,7 @@ type BackupPatch struct {
type BackupService interface {
CreateBackup(ctx context.Context, create *BackupCreate) (*Backup, error)
+ FindBackup(ctx context.Context, find *BackupFind) (*Backup, error)
FindBackupList(ctx context.Context, find *BackupFind) ([]*Backup, error)
PatchBackup(ctx context.Context, patch *BackupPatch) (*Backup, error)
}
diff --git a/store/backup.go b/store/backup.go
index a307939cab..7dfd1dc4db 100644
--- a/store/backup.go
+++ b/store/backup.go
@@ -45,6 +45,28 @@ func (s *BackupService) CreateBackup(ctx context.Context, create *api.BackupCrea
return backup, nil
}
+// FindBackup retrieves a single backup based on find.
+// Returns ENOTFOUND if no matching record.
+// Returns ECONFLICT if finding more than 1 matching records.
+func (s *BackupService) FindBackup(ctx context.Context, find *api.BackupFind) (*api.Backup, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ list, err := s.findBackupList(ctx, tx, find)
+ if err != nil {
+ return nil, err
+ } else if len(list) == 0 {
+ return nil, &bytebase.Error{Code: bytebase.ENOTFOUND, Message: fmt.Sprintf("backup not found: %+v", find)}
+ } else if len(list) > 1 {
+ return nil, &bytebase.Error{Code: bytebase.ECONFLICT, Message: fmt.Sprintf("found %d backups with filter %+v, expect 1. ", len(list), find)}
+ }
+
+ return list[0], nil
+}
+
// FindBackupList retrieves a list of backups based on find.
func (s *BackupService) FindBackupList(ctx context.Context, find *api.BackupFind) ([]*api.Backup, error) {
tx, err := s.db.BeginTx(ctx, nil)