-
-
Save rayman22201/aad9a164fcac70003637e4485bc67bd9 to your computer and use it in GitHub Desktop.
Macro version of DB migration for iffy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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