Skip to content

Instantly share code, notes, and snippets.

@rayman22201
Created September 20, 2018 00:52
Show Gist options
  • Save rayman22201/aad9a164fcac70003637e4485bc67bd9 to your computer and use it in GitHub Desktop.
Save rayman22201/aad9a164fcac70003637e4485bc67bd9 to your computer and use it in GitHub Desktop.
Macro version of DB migration for iffy
import db_sqlite
import strformat
import macros
type
Migration = tuple[name:string, fn: proc(db:DbConn)]
macro sqlList(statements: static[openArray[string]]): untyped =
result = newProc(params=[newEmptyNode(), newIdentDefs(ident("db"), ident("DbConn"))])
var body = newStmtList()
body.add(quote do:
echo "inside proc"
)
for statement in statements:
body.add(quote do:
echo "statement : " & `statement`
db.exec(sql`statement`)
)
result[6] = body
const migrations* = [
(name: "initial", fn: sqlList([
"""CREATE TABLE foo (
id INTEGER PRIMARY KEY
)""",
])),
(name: "second", fn: sqlList([
"""CREATE TABLE another (
id INTEGER PRIMARY KEY
)""",
])),
]
if isMainModule:
let db = db_sqlite.open(":memory:", "", "", "")
for migration in migrations:
migration.fn(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment