From 4ac4ab2a41839ddbb09cdd5f4c9bba9b93e91dd5 Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Wed, 10 Jul 2024 18:34:28 +0200 Subject: [PATCH] chore: sqlite make sure code is always run (#2891) --- waku/common/databases/db_sqlite.nim | 12 +++++---- .../driver/sqlite_driver/queries.nim | 27 ++++++++++++------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/waku/common/databases/db_sqlite.nim b/waku/common/databases/db_sqlite.nim index 917d35d28..26d2d955e 100644 --- a/waku/common/databases/db_sqlite.nim +++ b/waku/common/databases/db_sqlite.nim @@ -234,11 +234,13 @@ proc query*( return err($sqlite3_errstr(v)) return ok gotResults except Exception, CatchableError: - # release implicit transaction - discard sqlite3_reset(s) # same return information as step - discard sqlite3_clear_bindings(s) # no errors possible - discard sqlite3_finalize(s) - # NB: dispose of the prepared query statement and free associated memory + error "exception in query", query = query, error = getCurrentExceptionMsg() + + # release implicit transaction + discard sqlite3_reset(s) # same return information as step + discard sqlite3_clear_bindings(s) # no errors possible + discard sqlite3_finalize(s) + # NB: dispose of the prepared query statement and free associated memory proc prepareStmt*( db: SqliteDatabase, stmt: string, Params: type, Res: type diff --git a/waku/waku_archive/driver/sqlite_driver/queries.nim b/waku/waku_archive/driver/sqlite_driver/queries.nim index f58f0e96c..ff0bd904a 100644 --- a/waku/waku_archive/driver/sqlite_driver/queries.nim +++ b/waku/waku_archive/driver/sqlite_driver/queries.nim @@ -1,6 +1,7 @@ {.push raises: [].} import std/[options, sequtils], stew/byteutils, sqlite3_abi, results +import chronicles import ../../../common/databases/db_sqlite, ../../../common/databases/common, @@ -427,9 +428,12 @@ proc execSelectMessagesV2WithLimitStmt( else: return err($sqlite3_errstr(v)) except Exception, CatchableError: - # release implicit transaction - discard sqlite3_reset(s) # same return information as step - discard sqlite3_clear_bindings(s) # no errors possible + error "exception in execSelectMessagesV2WithLimitStmt", + error = getCurrentExceptionMsg() + + # release implicit transaction + discard sqlite3_reset(s) # same return information as step + discard sqlite3_clear_bindings(s) # no errors possible proc selectMessagesByHistoryQueryWithLimit*( db: SqliteDatabase, @@ -498,9 +502,11 @@ proc execSelectMessageByHash( else: return err($sqlite3_errstr(v)) except Exception, CatchableError: - # release implicit transaction - discard sqlite3_reset(s) # same return information as step - discard sqlite3_clear_bindings(s) # no errors possible + error "exception in execSelectMessageByHash", error = getCurrentExceptionMsg() + + # release implicit transaction + discard sqlite3_reset(s) # same return information as step + discard sqlite3_clear_bindings(s) # no errors possible proc selectMessageByHashQuery(): SqlQueryStr = var query: string @@ -629,9 +635,12 @@ proc execSelectMessagesWithLimitStmt( else: return err($sqlite3_errstr(v)) except Exception, CatchableError: - # release implicit transaction - discard sqlite3_reset(s) # same return information as step - discard sqlite3_clear_bindings(s) # no errors possible + error "exception in execSelectMessagesWithLimitStmt", + error = getCurrentExceptionMsg() + + # release implicit transaction + discard sqlite3_reset(s) # same return information as step + discard sqlite3_clear_bindings(s) # no errors possible proc selectMessagesWithLimitQuery( table: string, where: Option[string], limit: uint, ascending = true, v3 = false