Give a second snapshot create in the same second its own ID (closes #270)
check / check (push) Waiting to run
check / check (push) Waiting to run
The timestamp in a snapshot ID is in whole seconds, so a `snapshot create` that started in the same second as the previous run of that snapshot name got the same ID, and inserting its row failed with `UNIQUE constraint failed: snapshots.id`. CreateSnapshotWithName now looks the ID up in the local index first and, if it is taken, waits a second and takes a new timestamp. The ID format is unchanged. Only the local index is checked. That is where the insert fails, and the process lock serializes runs, so nothing takes the ID between the lookup and the insert. Model: opus-5-5
This commit is contained in:
@@ -386,3 +386,36 @@ func TestCleanSnapshotDBNonExistentSnapshot(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Two creates of one snapshot name back to back start within one second,
|
||||
// the resolution of the timestamp in a snapshot ID. See
|
||||
// https://git.eeqj.de/sneak/vaultik/issues/270.
|
||||
func TestCreateSnapshotWithNameTwiceBackToBack(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
db, err := database.New(ctx, filepath.Join(t.TempDir(), "index.sqlite"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create database: %v", err)
|
||||
}
|
||||
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
sm := &SnapshotManager{repos: database.NewRepositories(db)}
|
||||
|
||||
first, err := sm.CreateSnapshotWithName(ctx, "test-host", "data", "v", "g")
|
||||
if err != nil {
|
||||
t.Fatalf("first create failed: %v", err)
|
||||
}
|
||||
|
||||
second, err := sm.CreateSnapshotWithName(ctx, "test-host", "data", "v", "g")
|
||||
if err != nil {
|
||||
t.Fatalf("second create failed: %v", err)
|
||||
}
|
||||
|
||||
if first == second {
|
||||
t.Fatalf("both creates returned snapshot ID %s", first)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user