Skip to content

Commit

Permalink
postgres_driver/migrations.nim: add breakInfoStatements proc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Feb 27, 2024
1 parent fd7828d commit e7df8bb
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions waku/waku_archive/driver/postgres_driver/migrations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,49 @@ logScope:

const SchemaVersion* = 1 # increase this when there is an update in the database schema

proc breakIntoStatements*(script: string): seq[string] =
## Given a full migration script, that can potentially contain a list
## of SQL statements, this proc splits it into the contained isolated statements
## that should be executed one after the other.
var statements = newSeq[string]()

let lines = script.split('\n')

var simpleStmt: string
var plSqlStatement: string
var insidePlSqlScript = false
for line in lines:
if line.strip().len == 0:
continue

if insidePlSqlScript:
if line.contains("END $$"):
## End of the Pl/SQL script
plSqlStatement &= line
statements.add(plSqlStatement)
plSqlStatement = ""
insidePlSqlScript = false
continue

else:
plSqlStatement &= line & "\n"

if line.contains("DO $$"):
## Beginning of the Pl/SQL script
insidePlSqlScript = true
plSqlStatement &= line & "\n"

if not insidePlSqlScript:
if line.contains(';'):
## End of simple statement
simpleStmt &= line
statements.add(simpleStmt)
simpleStmt = ""
else:
simpleStmt &= line & "\n"

return statements

proc migrate*(driver: PostgresDriver,
targetVersion = SchemaVersion):
Future[DatabaseResult[void]] {.async.} =
Expand Down

0 comments on commit e7df8bb

Please sign in to comment.