Add debug logging for migration diff.

This commit is contained in:
Jonathan Bernard 2021-08-07 23:54:10 -05:00
parent 7cf53a4702
commit 9acbc27710
2 changed files with 14 additions and 2 deletions

View File

@ -1,7 +1,7 @@
# Package
bin = @["db_migrate"]
version = "0.3.0"
version = "0.3.1"
author = "Jonathan Bernard"
description = "Simple tool to handle database migrations."
license = "BSD"

View File

@ -89,6 +89,9 @@ proc diffMigrations*(
run: seq[string],
notRun, missing: seq[MigrationEntry] ] =
debug "diffMigrations: inspecting database and configured directories " &
"for migrations"
# Query the database to find out what migrations have been run.
var migrationsRun = initHashSet[string]()
@ -98,7 +101,9 @@ proc diffMigrations*(
# Inspect the filesystem to see what migrations are available.
var migrationsAvailable = newTable[string, MigrationEntry]()
for sqlDir in config.sqlDirs:
debug "Looking in " & sqlDir
for filePath in walkFiles(joinPath(sqlDir, "*.sql")):
debug "Saw migration file: " & filePath
var migrationName = filePath.extractFilename
migrationName.removeSuffix("-up.sql")
migrationName.removeSuffix("-down.sql")
@ -125,11 +130,18 @@ proc diffMigrations*(
missingMigrations.add(migrationsNotRun)
migrationsNotRun = newSeq[MigrationEntry]()
return (available: migrationsAvailable,
result = (available: migrationsAvailable,
run: toSeq(migrationsRun.items).sorted(system.cmp),
notRun: migrationsNotRun,
missing: missingMigrations)
debug "diffMigration: Results" &
"\n\tavailable: " & $toSeq(result[0].keys) &
"\n\trun: " & $result[1] &
"\n\tnotRun: " & $(result[2].mapIt(it.name)) &
"\n\tmissing: " & $(result[3].mapIt(it.name))
proc readStatements*(filename: string): seq[SqlQuery] =
result = @[]
var stmt: string = ""