Skip to content

Commit

Permalink
feat(rdb): add to backup download an export step in case it was not d…
Browse files Browse the repository at this point in the history
…one previously (#2498)
  • Loading branch information
yfodil authored Sep 9, 2022
1 parent 68fae1d commit ca7d94d
Show file tree
Hide file tree
Showing 6 changed files with 1,024 additions and 202 deletions.
19 changes: 16 additions & 3 deletions internal/namespaces/rdb/v1/custom_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,30 @@ func backupDownloadCommand() *core.Command {
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
args := argsI.(*backupDownloadArgs)
api := rdb.NewAPI(core.ExtractClient(ctx))
backup, err := api.WaitForDatabaseBackup(&rdb.WaitForDatabaseBackupRequest{
backupRequest := &rdb.WaitForDatabaseBackupRequest{
DatabaseBackupID: args.BackupID,
Region: args.Region,
Timeout: scw.TimeDurationPtr(backupActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
backup, err := api.WaitForDatabaseBackup(backupRequest)
if err != nil {
return nil, err
}
if backup.DownloadURL == nil {
return nil, fmt.Errorf("no download URL found")
exportRequest := rdb.ExportDatabaseBackupRequest{
DatabaseBackupID: args.BackupID,
Region: args.Region,
}
_, err = api.ExportDatabaseBackup(&exportRequest)
if err != nil {
return nil, err
}
}

backup, err = api.WaitForDatabaseBackup(backupRequest)
if err != nil {
return nil, err
}

httpClient := core.ExtractHTTPClient(ctx)
Expand Down
29 changes: 27 additions & 2 deletions internal/namespaces/rdb/v1/custom_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,40 @@ func Test_DownloadBackup(t *testing.T) {
"scw rdb backup export {{ .Backup.ID }} --wait",
),
),
Cmd: "scw rdb backup download {{ .Backup.ID }} output=dump",
Cmd: "scw rdb backup download {{ .Backup.ID }} output=simple_dump",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteInstance(),
func(ctx *core.AfterFuncCtx) error {
err := os.Remove("dump")
err := os.Remove("simple_dump")
return err
},
),
DefaultRegion: scw.RegionNlAms,
TmpHomeDir: true,
}))

t.Run("With no previous export backup", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createInstance(engine),
core.ExecStoreBeforeCmd(
"Backup",
"scw rdb backup create name=foobar expires-at=2999-01-02T15:04:05-07:00 instance-id={{ .Instance.ID }} database-name=rdb --wait",
),
),
Cmd: "scw rdb backup download {{ .Backup.ID }} output=no_previous_export_dump",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteInstance(),
func(ctx *core.AfterFuncCtx) error {
err := os.Remove("no_previous_export_dump")
return err
},
),
Expand Down
Loading

0 comments on commit ca7d94d

Please sign in to comment.