mirror of
https://github.com/logos-messaging/nim-chat-sdk.git
synced 2026-01-02 14:13:07 +00:00
chore: refore code
This commit is contained in:
parent
670b128c71
commit
3fe8fb2a03
2
.gitignore
vendored
2
.gitignore
vendored
@ -17,4 +17,6 @@ nimcache/
|
||||
|
||||
# Compiled files
|
||||
chat_sdk/*
|
||||
!*.nim
|
||||
apps/*
|
||||
!*.nim
|
||||
1
apps/nim.cfg
Normal file
1
apps/nim.cfg
Normal file
@ -0,0 +1 @@
|
||||
path = ".."
|
||||
12
apps/run_migration.nim
Normal file
12
apps/run_migration.nim
Normal file
@ -0,0 +1,12 @@
|
||||
import db_connector/db_sqlite
|
||||
import chat_sdk/migration
|
||||
|
||||
proc main() =
|
||||
let db = open("test.db", "", "", "")
|
||||
try:
|
||||
runMigrations(db)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
when isMainModule:
|
||||
main()
|
||||
@ -18,4 +18,4 @@ task buildStaticLib, "Build static library for C bindings":
|
||||
exec "nim c --app:staticLib --out:../library/c-bindings/libchatsdk.a chat_sdk/chat_sdk.nim"
|
||||
|
||||
task migrate, "Run database migrations":
|
||||
exec "nim c -r chat_sdk/migration.nim"
|
||||
exec "nim c -r apps/run_migration.nim"
|
||||
@ -1,5 +1,6 @@
|
||||
import os, osproc, sequtils, algorithm
|
||||
import os, sequtils, algorithm
|
||||
import db_connector/db_sqlite
|
||||
import chronicles
|
||||
|
||||
proc ensureMigrationTable(db: DbConn) =
|
||||
db.exec(sql"""
|
||||
@ -18,23 +19,16 @@ proc markMigrationRun(db: DbConn, filename: string) =
|
||||
db.exec(sql"INSERT INTO schema_migrations (filename) VALUES (?)", filename)
|
||||
|
||||
proc runMigrations*(db: DbConn, dir = "migrations") =
|
||||
info "Migration process starting up"
|
||||
ensureMigrationTable(db)
|
||||
let files = walkFiles(dir / "*.sql").toSeq().sorted()
|
||||
for file in files:
|
||||
if hasMigrationRun(db, file):
|
||||
echo "Already applied: ", file
|
||||
info "Migration already applied", file
|
||||
info "..."
|
||||
else:
|
||||
echo "Applying: ", file
|
||||
info "Applying migration", file
|
||||
info "..."
|
||||
let sql = readFile(file)
|
||||
db.exec(sql(sql))
|
||||
markMigrationRun(db, file)
|
||||
|
||||
proc main() =
|
||||
let db = open("test.db", "", "", "")
|
||||
try:
|
||||
runMigrations(db)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
when isMainModule:
|
||||
main()
|
||||
Loading…
x
Reference in New Issue
Block a user