Skip to content

Commit

Permalink
fix: match snapshot using full filename, fixes ddev#6694 (ddev#6704) …
Browse files Browse the repository at this point in the history
…[skip ci]

Co-authored-by: Stanislav Zhuk <[email protected]>
  • Loading branch information
castanearie and stasadev authored Nov 9, 2024
1 parent 62bf39b commit 9dd929b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/ddevapp/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,25 @@ func (app *DdevApp) RestoreSnapshot(snapshotName string) error {
func GetSnapshotFileFromName(name string, app *DdevApp) (string, error) {
snapshotsDir := app.GetConfigPath("db_snapshots")
snapshotFullPath := filepath.Join(snapshotsDir, name)

// If old-style directory-based snapshot, then use the name, no massaging required
if fileutil.IsDirectory(snapshotFullPath) {
return name, nil
}

// But if it's a gzipped tarball, we have to get the filename.
files, err := fileutil.ListFilesInDir(snapshotsDir)
if err != nil {
return "", err
}

m := regexp.MustCompile("^" + regexp.QuoteMeta(name) + `-(mariadb|mysql|postgres)_[0-9.]*\.gz$`)

for _, file := range files {
if strings.HasPrefix(file, name+"-") {
if m.MatchString(file) {
return file, nil
}
}

return "", fmt.Errorf("snapshot %s not found in %s", name, snapshotsDir)
}
4 changes: 4 additions & 0 deletions pkg/ddevapp/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func TestDdevSnapshotCleanup(t *testing.T) {
snapshotName, err := app.Snapshot(t.Name() + "_1")
assert.NoError(err)

// Provide a second snapshot that should not be deleted and whose filename will be lexicographically earlier (See https://github.com/ddev/ddev/issues/6694).
_, err = app.Snapshot(t.Name() + "_1-a")
assert.NoError(err)

err = app.Init(site.Dir)
require.NoError(t, err)

Expand Down

0 comments on commit 9dd929b

Please sign in to comment.