From 6ac6a8536d7f875e1d37b2aa9862f78488de4068 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Sat, 16 Jan 2021 09:27:43 -0700 Subject: [PATCH] Obtain write lock during validation. --- db.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/db.go b/db.go index ee776be..d206991 100644 --- a/db.go +++ b/db.go @@ -1490,6 +1490,17 @@ func (db *DB) CRC64() (uint64, Pos, error) { return 0, Pos{}, err } + // Start a transaction to obtain a write lock on the database. + tx, err := db.db.Begin() + if err != nil { + return 0, Pos{}, fmt.Errorf("begin: %w", err) + } + defer tx.Rollback() + + if _, err := tx.ExecContext(db.ctx, `INSERT INTO _litestream_lock (id) VALUES (1);`); err != nil { + return 0, Pos{}, fmt.Errorf("_litestream_lock: %w", err) + } + // Obtain current position. Clear the offset since we are only reading the // DB and not applying the current WAL. pos, err := db.Pos()