mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-15 01:14:56 +00:00
fix(waku_archive): only allow a single instance to execute migrations (#2736)
This commit is contained in:
parent
8b42f199ba
commit
88b8e1867a
@ -72,6 +72,16 @@ proc migrate*(
|
|||||||
# Load migration scripts
|
# Load migration scripts
|
||||||
let scripts = pg_migration_manager.getMigrationScripts(currentVersion, targetVersion)
|
let scripts = pg_migration_manager.getMigrationScripts(currentVersion, targetVersion)
|
||||||
|
|
||||||
|
# Lock the db
|
||||||
|
(await driver.acquireDatabaseLock()).isOkOr:
|
||||||
|
error "failed to acquire lock", error = error
|
||||||
|
return err("failed to lock the db")
|
||||||
|
|
||||||
|
defer:
|
||||||
|
(await driver.releaseDatabaseLock()).isOkOr:
|
||||||
|
error "failed to release lock", error = error
|
||||||
|
return err("failed to unlock the db.")
|
||||||
|
|
||||||
# Run the migration scripts
|
# Run the migration scripts
|
||||||
for script in scripts:
|
for script in scripts:
|
||||||
for statement in script.breakIntoStatements():
|
for statement in script.breakIntoStatements():
|
||||||
|
@ -853,6 +853,42 @@ proc sleep*(
|
|||||||
|
|
||||||
return ok()
|
return ok()
|
||||||
|
|
||||||
|
proc acquireDatabaseLock*(
|
||||||
|
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)
|
||||||
|
let locked = (
|
||||||
|
await s.getStr(
|
||||||
|
fmt"""
|
||||||
|
SELECT pg_try_advisory_lock({lockId})
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
).valueOr:
|
||||||
|
return err("error acquiring a lock: " & error)
|
||||||
|
|
||||||
|
if locked == "f":
|
||||||
|
return err("another waku instance is currently executing a migration")
|
||||||
|
|
||||||
|
return ok()
|
||||||
|
|
||||||
|
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)
|
||||||
|
let unlocked = (
|
||||||
|
await s.getStr(
|
||||||
|
fmt"""
|
||||||
|
SELECT pg_advisory_unlock({lockId})
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
).valueOr:
|
||||||
|
return err("error releasing a lock: " & error)
|
||||||
|
|
||||||
|
if unlocked == "f":
|
||||||
|
return err("could not release advisory lock")
|
||||||
|
|
||||||
|
return ok()
|
||||||
|
|
||||||
proc performWriteQuery*(
|
proc performWriteQuery*(
|
||||||
s: PostgresDriver, query: string
|
s: PostgresDriver, query: string
|
||||||
): Future[ArchiveDriverResult[void]] {.async.} =
|
): Future[ArchiveDriverResult[void]] {.async.} =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user