From a3b422fabf63d47ff83aaa778fde333324d0980b Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Fri, 7 Jun 2024 17:54:26 +0200 Subject: [PATCH] chore: postgres_driver - acquire/release advisory lock when creating partitions (#2784) --- .../driver/postgres_driver/postgres_driver.nim | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim index 3afd79c1f2..00bee087b4 100644 --- a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim +++ b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim @@ -883,7 +883,7 @@ proc acquireDatabaseLock*( proc releaseDatabaseLock*( s: PostgresDriver, lockId: int = 841886 ): Future[ArchiveDriverResult[void]] {.async.} = - ## Acquire an advisory lock (useful to avoid more than one application running migrations at the same time) + ## Release an advisory lock (useful to avoid more than one application running migrations at the same time) let unlocked = ( await s.getStr( fmt""" @@ -930,6 +930,16 @@ proc addPartition( "CREATE TABLE IF NOT EXISTS " & partitionName & " PARTITION OF " & "messages FOR VALUES FROM ('" & fromInNanoSec & "') TO ('" & untilInNanoSec & "');" + # Lock the db + (await self.acquireDatabaseLock()).isOkOr: + error "failed to acquire lock", error = error + return err("failed to lock the db") + + defer: + (await self.releaseDatabaseLock()).isOkOr: + error "failed to release lock", error = error + return err("failed to unlock the db.") + (await self.performWriteQuery(createPartitionQuery)).isOkOr: if error.contains("already exists"): debug "skip create new partition as it already exists: ", skipped_error = $error