From fe1bb4c4e75c9d20698e767427effa5fc29bbbc7 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Tue, 16 Nov 2021 13:45:28 +0200 Subject: [PATCH] Allow Sqlite keystores to be used in read-only mode This is useful for tools such as `ncli_db` that can work with the database of a running Nimbus instances. --- eth/db/kvstore_sqlite3.nim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/eth/db/kvstore_sqlite3.nim b/eth/db/kvstore_sqlite3.nim index 9cbcfe8..bc9afe1 100644 --- a/eth/db/kvstore_sqlite3.nim +++ b/eth/db/kvstore_sqlite3.nim @@ -29,6 +29,7 @@ type # can be created env: Sqlite managedStmts: seq[RawStmtPtr] + readOnly*: bool SqStoreCheckpointKind* {.pure.} = enum passive, full, restart, truncate @@ -509,6 +510,7 @@ proc init*( ok(SqStoreRef( env: env.release, + readOnly: readOnly )) proc openKvStore*(db: SqStoreRef, name = "kvstore", withoutRowid = false): KvResult[SqKeyspaceRef] = @@ -519,15 +521,15 @@ proc openKvStore*(db: SqStoreRef, name = "kvstore", withoutRowid = false): KvRes ## rows (the row being the sum of key and value) - see ## https://www.sqlite.org/withoutrowid.html ## - let - createSql = """ + + if not db.readOnly: + let createSql = """ CREATE TABLE IF NOT EXISTS """ & name & """ ( key BLOB PRIMARY KEY, value BLOB )""" - - checkExec db.env, - if withoutRowid: createSql & " WITHOUT ROWID;" else: createSql & ";" + checkExec db.env, + if withoutRowid: createSql & " WITHOUT ROWID;" else: createSql & ";" var tmp: SqKeyspace