Skip to content

Commit

Permalink
Give an error on duplicate snapshot anme, fixes ddev#2515 (ddev#2739)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay authored Jan 11, 2021
1 parent 51ede53 commit d374ae9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,15 @@ func (app *DdevApp) Snapshot(snapshotName string) (string, error) {
t := time.Now()
snapshotName = app.Name + "_" + t.Format("20060102150405")
}

existingSnapshots, err := app.ListSnapshots()
if err != nil {
return "", err
}
if nodeps.ArrayContainsString(existingSnapshots, snapshotName) {
return "", fmt.Errorf("snapshot %s already exists, please use another snapshot name or clean up snapshots with `ddev snapshot --cleanup`", snapshotName)
}

// Container side has to use path.Join instead of filepath.Join because they are
// targeted at the linux filesystem, so won't work with filepath on Windows
snapshotDir := path.Join("db_snapshots", snapshotName)
Expand Down
6 changes: 5 additions & 1 deletion pkg/ddevapp/ddevapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ func TestGetLatestSnapshot(t *testing.T) {
runTime()
}

// TestDdevRestoreSnapshot tests creating a snapshot and reverting to it. This runs with Mariadb 10.2
// TestDdevRestoreSnapshot tests creating a snapshot and reverting to it.
func TestDdevRestoreSnapshot(t *testing.T) {
assert := asrt.New(t)
testDir, _ := os.Getwd()
Expand Down Expand Up @@ -1536,6 +1536,10 @@ func TestDdevRestoreSnapshot(t *testing.T) {
err = os.Remove("hello-post-snapshot-" + app.Name)
assert.NoError(err)

// Make sure duplicate snapshot name gives an error
_, err = app.Snapshot(snapshotName)
assert.Error(err)

err = app.ImportDB(d7testerTest2Dump, "", false, false, "db")
assert.NoError(err, "Failed to app.ImportDB path: %s err: %v", d7testerTest2Dump, err)
_, _ = testcommon.EnsureLocalHTTPContent(t, app.GetHTTPSURL(), "d7 tester test 2 has 2 nodes", 45)
Expand Down

0 comments on commit d374ae9

Please sign in to comment.