diff --git a/api/internal/db/db.go b/api/internal/db/db.go index f0bf20acb..fcf6421e1 100644 --- a/api/internal/db/db.go +++ b/api/internal/db/db.go @@ -56,6 +56,14 @@ func Open(ctx context.Context, driver, dsn string) (*Store, error) { // Add a busy timeout to the DSN if not already present, so concurrent // processes (e.g., API and bootstrap-admin) waiting for locks don't fail immediately. dsn = withSQLiteBusyTimeout(dsn) + // Take the write lock when a read-write transaction begins. A deferred + // transaction that reads first and writes later cannot wait in WAL mode: + // if another process wrote in between, SQLite returns SQLITE_BUSY at once + // and skips busy_timeout (seen with bootstrap-admin next to a live API). + // Read-only transactions stay deferred. withSQLiteBusyTimeout always + // leaves a query string, and the driver reads the first _txlock value, + // so a _txlock already in the configured DSN still wins. + dsn += "&_txlock=immediate" db, err := sql.Open("sqlite", dsn) if err != nil { diff --git a/changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md b/changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md new file mode 100644 index 000000000..4d2215700 --- /dev/null +++ b/changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md @@ -0,0 +1,6 @@ +# bootstrap-admin no longer fails with "database is locked" on SQLite + +Running `bootstrap-admin` while the API is serving could fail at once with +`database is locked (SQLITE_BUSY)`, even with the busy timeout set. SQLite +read-write transactions now take the write lock when they begin, so they wait +for the lock instead of failing. No action is needed. [PR #1028](https://github.com/GameplanePanel/Gameplane/pull/1028)