Remove a local snapshot fully when named by its remote key
snapshot remove treated every remote key as naming a snapshot only on the destination store. Given the remote key of a snapshot the local index holds, it deleted the remote metadata and left the local row. findSnapshotToRemove now returns that snapshot's ID, so it is removed from both. Model: opus-5-5
This commit is contained in:
1 parent
fb33bcf6b1
commit
3af64c829c
2 files changed
+60
-13
No files matched your search
@@ -504,6 +504,36 @@ func TestRemoveSnapshot_ByRemoteKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRemoveSnapshot_ByRemoteKeyInLocalIndex checks that a snapshot the
|
||||
// local index holds, named by its remote key, is removed from the local
|
||||
// index as well as from the destination store.
|
||||
func TestRemoveSnapshot_ByRemoteKeyInLocalIndex(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
|
||||
store := newTestStorer()
|
||||
addManifest(t, store, "snapshot-001", nil)
|
||||
|
||||
// A backup wrote to this destination, so it carries the marker.
|
||||
require.NoError(t, store.Put(context.Background(), markerFile,
|
||||
bytes.NewReader(nil)))
|
||||
|
||||
tv := vaultik.NewForTesting(store)
|
||||
tv.Repositories = newLocalIndex(t, "snapshot-001")
|
||||
|
||||
result, err := tv.RemoveSnapshot(snapshot.RemoteSnapshotKey("snapshot-001"),
|
||||
&vaultik.RemoveOptions{Force: true})
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result.RemoteRemoved)
|
||||
assert.False(t, store.hasKey(remoteKeyPath("snapshot-001")))
|
||||
|
||||
snap, err := tv.Repositories.Snapshots.GetByID(context.Background(),
|
||||
"snapshot-001")
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, snap)
|
||||
}
|
||||
|
||||
// TestRemoveSnapshot_NotInLocalIndexDeleteFails checks that removing a
|
||||
// snapshot only the destination store holds fails when its metadata
|
||||
// cannot be deleted there, under --force and under --json: nothing was
|
||||
|
||||
@@ -1467,7 +1467,7 @@ func (v *Vaultik) RemoveSnapshot(
|
||||
return result, err
|
||||
}
|
||||
|
||||
remoteKey, inLocalIndex, err := v.findSnapshotToRemove(
|
||||
snapshotID, remoteKey, inLocalIndex, err := v.findSnapshotToRemove(
|
||||
snapshotID, opts.LocalOnly)
|
||||
if err != nil {
|
||||
return result, err
|
||||
@@ -1531,22 +1531,39 @@ func (v *Vaultik) RemoveSnapshot(
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// findSnapshotToRemove returns the remote key of the snapshot that
|
||||
// identifier names, and whether the local index holds it. A remote key,
|
||||
// or the leading part of one, names a snapshot only on the destination
|
||||
// store, so the local index never holds it.
|
||||
// findSnapshotToRemove returns the ID of the snapshot that identifier
|
||||
// names, its remote key, and whether the local index holds it. A remote
|
||||
// key, or the leading part of one, is resolved on the destination store;
|
||||
// when the local index holds the snapshot with that key, its ID is
|
||||
// returned, so it is removed from both.
|
||||
func (v *Vaultik) findSnapshotToRemove(
|
||||
identifier string, localOnly bool,
|
||||
) (string, bool, error) {
|
||||
if isRemoteKeyOrPrefix(identifier) && !localOnly {
|
||||
remoteKey, err := v.resolveSnapshotRemoteKey(identifier)
|
||||
|
||||
return remoteKey, false, err
|
||||
}
|
||||
|
||||
) (string, string, bool, error) {
|
||||
if !isRemoteKeyOrPrefix(identifier) || localOnly {
|
||||
inLocalIndex, err := v.checkSnapshotExists(identifier, localOnly)
|
||||
|
||||
return snapshot.RemoteSnapshotKey(identifier), inLocalIndex, err
|
||||
return identifier, snapshot.RemoteSnapshotKey(identifier),
|
||||
inLocalIndex, err
|
||||
}
|
||||
|
||||
remoteKey, err := v.resolveSnapshotRemoteKey(identifier)
|
||||
if err != nil {
|
||||
return "", "", false, err
|
||||
}
|
||||
|
||||
localIDs, err := v.localSnapshotIDs()
|
||||
if err != nil {
|
||||
return "", "", false, fmt.Errorf(
|
||||
"looking up snapshot in local index: %w", err)
|
||||
}
|
||||
|
||||
for _, id := range localIDs {
|
||||
if snapshot.RemoteSnapshotKey(id) == remoteKey {
|
||||
return id, remoteKey, true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return identifier, remoteKey, false, nil
|
||||
}
|
||||
|
||||
// checkSnapshotExists returns errSnapshotNotFound when the local index
|
||||
|
||||
Reference in new issue
Block a user