Update for Nim 1.4.x+

This commit is contained in:
Jonathan Bernard 2021-07-03 22:00:14 -05:00
parent daf3a8dad0
commit 6837e5448b
2 changed files with 10 additions and 11 deletions

View File

@ -1,7 +1,7 @@
# Package
bin = @["db_migrate"]
version = "0.2.7"
version = "0.2.8"
author = "Jonathan Bernard"
description = "Simple tool to handle database migrations."
license = "BSD"
@ -9,5 +9,4 @@ srcDir = "src/main/nim"
# Dependencies
requires: @["nim >= 0.13.0", "docopt >= 0.1.0"]
requires: @["nim >= 1.4.0", "docopt >= 0.1.0"]

View File

@ -3,15 +3,15 @@
##
## Simple tool to manage database migrations.
import algorithm, json, times, os, strutils, docopt, db_postgres, sets,
sequtils, logging
import algorithm, db_postgres, docopt, json, logging, os, sequtils, sets,
strutils, times
type
DbMigrateConfig* = tuple[ driver, sqlDir, connectionString: string, logLevel: Level ]
proc ensureMigrationsTableExists(conn: DbConn): void =
let tableCount = conn.getValue(sql"""
SELECT COUNT(*) FROM information_schema.tables
SELECT COUNT(*) FROM information_schema.tables
WHERE table_name = 'migrations';""")
if tableCount.strip == "0":
@ -55,7 +55,7 @@ proc loadConfig*(filename: string): DbMigrateConfig =
proc createMigration*(config: DbMigrateConfig, migrationName: string): seq[string] =
## Create a new set of database migration files.
let timestamp = getTime().getLocalTime().format("yyyyMMddHHmmss")
let timestamp = now().format("yyyyMMddHHmmss")
let filenamePrefix = timestamp & "-" & migrationName
let upFilename = joinPath(config.sqlDir, filenamePrefix & "-up.sql")
@ -78,13 +78,13 @@ proc diffMigrations*(pgConn: DbConn, config: DbMigrateConfig):
tuple[ run, notRun, missing: seq[string] ] =
# Query the database to find out what migrations have been run.
var migrationsRun = initSet[string]()
var migrationsRun = initHashSet[string]()
for row in pgConn.fastRows(sql"SELECT * FROM migrations ORDER BY name", @[]):
migrationsRun.incl(row[1])
# Inspect the filesystem to see what migrations are available.
var migrationsAvailable = initSet[string]()
var migrationsAvailable = initHashSet[string]()
for filePath in walkFiles(joinPath(config.sqlDir, "*.sql")):
var migrationName = filePath.extractFilename
migrationName.removeSuffix("-up.sql")
@ -215,7 +215,7 @@ Options:
"""
# Parse arguments
let args = docopt(doc, version = "db-migrate (Nim) 0.2.7\nhttps://git.jdb-labs.com/jdb/db-migrate")
let args = docopt(doc, version = "db-migrate (Nim) 0.2.8\nhttps://git.jdb-software.com/jdb/db-migrate")
let exitErr = proc(msg: string): void =
fatal("db_migrate: " & msg)
@ -243,7 +243,7 @@ Options:
else: logging.setLogFilter(config.logLevel)
# Check for migrations directory
if not existsDir config.sqlDir:
if not dirExists config.sqlDir:
try:
warn "SQL directory '" & config.sqlDir &
"' does not exist and will be created."