feat(postgres): integration of postgres in wakunode2 (#1808)

* Making the wakunode2 to support postgres driver

* driver/builder.nim: controling possible errors when creating the messages table

* postgres_driver.nim: adding protection in getInt and fixing typo
This commit is contained in:
Ivan Folgueira Bande 2023-06-28 18:47:28 +02:00 committed by GitHub
parent cf46fb7cf6
commit 88b7481f29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 8 deletions

View File

@ -6,18 +6,21 @@ else:
import
stew/results,
chronicles
chronicles,
chronos
import
../driver,
../../../common/databases/dburl,
../../../common/databases/db_sqlite,
./sqlite_driver,
./sqlite_driver/migrations as archive_driver_sqlite_migrations,
./queue_driver
./queue_driver,
./postgres_driver
export
sqlite_driver,
queue_driver
queue_driver,
postgres_driver
proc new*(T: type ArchiveDriver,
url: string,
@ -69,6 +72,24 @@ proc new*(T: type ArchiveDriver,
return ok(res.get())
of "postgres":
const MaxNumConns = 5 #TODO: we may need to set that from app args (maybe?)
let res = PostgresDriver.new(url, MaxNumConns)
if res.isErr():
return err("failed to init postgres archive driver: " & res.error)
let driver = res.get()
try:
# The table should exist beforehand.
let newTableRes = waitFor driver.createMessageTable()
if newTableRes.isErr():
return err("error creating table: " & newTableRes.error)
except CatchableError:
return err("exception creating table: " & getCurrentExceptionMsg())
return ok(driver)
else:
debug "setting up in-memory waku archive driver"
let driver = QueueDriver.new() # Defaults to a capacity of 25.000 messages

View File

@ -52,8 +52,8 @@ proc new*(T: type PostgresDriver,
return ok(PostgresDriver(connPool: connPoolRes.get()))
proc createMessageTable(s: PostgresDriver):
Future[ArchiveDriverResult[void]] {.async.} =
proc createMessageTable*(s: PostgresDriver):
Future[ArchiveDriverResult[void]] {.async.} =
let execRes = await s.connPool.exec(createTableQuery())
if execRes.isErr():
@ -238,9 +238,10 @@ proc getInt(s: PostgresDriver,
if fields.len != 1:
return err("failed in getRow: Expected one field but got " & $fields.len)
var retInt: int64
var retInt = 0'i64
try:
retInt = parseInt(fields[0])
if fields[0] != "":
retInt = parseInt(fields[0])
except ValueError:
return err("exception in getRow, parseInt: " & getCurrentExceptionMsg())
@ -269,7 +270,7 @@ method getNewestMessageTimestamp*(s: PostgresDriver):
let intRes = await s.getInt("SELECT MAX(storedAt) FROM messages")
if intRes.isErr():
return err("error in getOldestMessageTimestamp: " & intRes.error)
return err("error in getNewestMessageTimestamp: " & intRes.error)
return ok(Timestamp(intRes.get()))