|
|
|
@@ -171,7 +171,7 @@ proc up*(
|
|
|
|
|
|
|
|
|
|
# Apply each of the migrations.
|
|
|
|
|
for migration in toRun:
|
|
|
|
|
info migration.name
|
|
|
|
|
info "Applying up script for " & migration.name
|
|
|
|
|
|
|
|
|
|
if not migration.upPath.fileExists:
|
|
|
|
|
pgConn.rollbackWithErr "Can not find UP file for " & migration.name &
|
|
|
|
@@ -202,7 +202,7 @@ proc down*(
|
|
|
|
|
pgConn.exec(sql"BEGIN")
|
|
|
|
|
|
|
|
|
|
for migration in migrationsToDown:
|
|
|
|
|
info migration.name
|
|
|
|
|
info "Applying down script for " & migration.name
|
|
|
|
|
|
|
|
|
|
if not migration.downPath.fileExists:
|
|
|
|
|
pgConn.rollbackWithErr "Can not find DOWN file for " & migration.name &
|
|
|
|
@@ -229,6 +229,7 @@ Usage:
|
|
|
|
|
db_migrate [options] up [<count>]
|
|
|
|
|
db_migrate [options] down [<count>]
|
|
|
|
|
db_migrate [options] init <schema-name>
|
|
|
|
|
db_migrate [options] status
|
|
|
|
|
db_migrate (-V | --version)
|
|
|
|
|
db_migrate (-h | --help)
|
|
|
|
|
|
|
|
|
@@ -249,7 +250,7 @@ Options:
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
|
let args = docopt(doc, version = "db-migrate (Nim) 0.3.2\nhttps://git.jdb-software.com/jdb/db-migrate")
|
|
|
|
|
let args = docopt(doc, version = "db-migrate (Nim) 0.4.1\nhttps://git.jdb-software.com/jdb/db-migrate")
|
|
|
|
|
|
|
|
|
|
let exitErr = proc(msg: string): void =
|
|
|
|
|
fatal("db_migrate: " & msg)
|
|
|
|
@@ -307,12 +308,12 @@ Options:
|
|
|
|
|
|
|
|
|
|
let (available, run, notRun, missing) = diffMigrations(pgConn, config)
|
|
|
|
|
|
|
|
|
|
if args["up"]:
|
|
|
|
|
# Make sure we have no gaps (database is in an unknown state)
|
|
|
|
|
if missing.len > 0:
|
|
|
|
|
exitErr "Database is in an inconsistent state. Migrations have been " &
|
|
|
|
|
"run that are not sequential."
|
|
|
|
|
|
|
|
|
|
if args["up"]:
|
|
|
|
|
try:
|
|
|
|
|
let count = if args["<count>"]: parseInt($args["<count>"]) else: high(int)
|
|
|
|
|
let toRun = if count < notRun.len: notRun[0..<count] else: notRun
|
|
|
|
@@ -331,9 +332,29 @@ Options:
|
|
|
|
|
except DbError:
|
|
|
|
|
exitErr "Unable to migrate database: " & getCurrentExceptionMsg()
|
|
|
|
|
|
|
|
|
|
elif args["status"]:
|
|
|
|
|
|
|
|
|
|
info "Database Migration Status" &
|
|
|
|
|
"\n\nSQL Migration Folders: " & join(config.sqlDirs.mapIt("\n " & it), "") &
|
|
|
|
|
"\n\nMigrations: " &
|
|
|
|
|
"\n available: " & join(
|
|
|
|
|
toSeq(available.keys)
|
|
|
|
|
.sorted(system.cmp)
|
|
|
|
|
.mapIt("\n " & it),
|
|
|
|
|
"") &
|
|
|
|
|
"\n run: " & join(run.mapIt("\n " & it), "") &
|
|
|
|
|
"\n notRun: " & join(notRun.mapIt("\n " & it.name), "") &
|
|
|
|
|
"\n missing: " & join(missing.mapIt("\n " & it.name), "") &
|
|
|
|
|
"\n"
|
|
|
|
|
|
|
|
|
|
elif args["init"]: discard
|
|
|
|
|
|
|
|
|
|
let newResults = diffMigrations(pgConn, config)
|
|
|
|
|
|
|
|
|
|
if newResults.missing.len > 0:
|
|
|
|
|
exitErr "Database is in an inconsistent state. Migrations have been " &
|
|
|
|
|
"run that are not sequential."
|
|
|
|
|
|
|
|
|
|
if newResults.notRun.len > 0:
|
|
|
|
|
info "Database is behind by " & $(newResults.notRun.len) & " migrations."
|
|
|
|
|
else: info "Database is up to date."
|
|
|
|
|