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

View File

@ -438,7 +438,9 @@ proc init*(
if inMemory: ":memory:" if inMemory: ":memory:"
else: basepath / name & ".sqlite3" else: basepath / name & ".sqlite3"
flags = 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 else: SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE
if not inMemory: if not inMemory:
@ -463,12 +465,15 @@ proc init*(
discard sqlite3_finalize(journalModePragma) discard sqlite3_finalize(journalModePragma)
return err("Invalid pragma result: " & $x) return err("Invalid pragma result: " & $x)
# TODO: check current version and implement schema versioning if not readOnly:
checkExec env.val, "PRAGMA user_version = 3;" # 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;") let journalModePragma = prepare(env.val, "PRAGMA journal_mode = WAL;")
checkWalPragmaResult(journalModePragma) checkWalPragmaResult(journalModePragma)
checkExec journalModePragma checkExec journalModePragma
if manualCheckpoint: if manualCheckpoint:
checkErr sqlite3_wal_autocheckpoint(env.val, 0) checkErr sqlite3_wal_autocheckpoint(env.val, 0)