sqlite: fix readonly mode (#359)

* sqlite: fix readonly mode

* document kvstore versions
This commit is contained in:
Jacek Sieka 2021-05-25 20:57:28 +02:00 committed by GitHub
parent 8abe6b7144
commit 2a292cfb62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 6 deletions

View File

@ -438,7 +438,9 @@ proc init*(
if inMemory: ":memory:"
else: basepath / name & ".sqlite3"
flags =
if readOnly: SQLITE_OPEN_READONLY
# For some reason, opening multiple in-memory databases doesn't work if
# one of them is read-only - for now, disable read-only mode for them
if readOnly and not inMemory: SQLITE_OPEN_READONLY
else: SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE
if not inMemory:
@ -463,12 +465,15 @@ proc init*(
discard sqlite3_finalize(journalModePragma)
return err("Invalid pragma result: " & $x)
# TODO: check current version and implement schema versioning
checkExec env.val, "PRAGMA user_version = 3;"
if not readOnly:
# user_version = 1: single kvstore table without rowid
# user_version = 2: single kvstore table with rowid
# user_version = 3: multiple named kvstore tables via openKvStore
checkExec env.val, "PRAGMA user_version = 3;"
let journalModePragma = prepare(env.val, "PRAGMA journal_mode = WAL;")
checkWalPragmaResult(journalModePragma)
checkExec journalModePragma
let journalModePragma = prepare(env.val, "PRAGMA journal_mode = WAL;")
checkWalPragmaResult(journalModePragma)
checkExec journalModePragma
if manualCheckpoint:
checkErr sqlite3_wal_autocheckpoint(env.val, 0)