Added stubs for up, down, init. Starting on connection creation (BROKEN).

This commit is contained in:
Jonathan Bernard 2016-01-31 12:27:37 -06:00
parent bccd0439f7
commit 9e0b264cae

View File

@ -3,7 +3,7 @@
## ##
## Simple tool to manage database migrations. ## Simple tool to manage database migrations.
import json, times, os, docopt import json, times, os, strutils, docopt, db_postgres, db_mysql, db_sqlite
type DbMigrateConfig* = tuple[ driver, sqlDir, connectionString: string ] type DbMigrateConfig* = tuple[ driver, sqlDir, connectionString: string ]
@ -16,7 +16,14 @@ proc loadConfig*(filename: string): DbMigrateConfig =
sqlDir: if cfg.hasKey("sqlDir"): cfg["sqlDir"].getStr else: "migrations", sqlDir: if cfg.hasKey("sqlDir"): cfg["sqlDir"].getStr else: "migrations",
connectionString: cfg["connectionString"].getStr) connectionString: cfg["connectionString"].getStr)
proc createMigration*(config: DBMigrateConfig, migrationName: string): seq[string] = proc getDbConnection*(config: DbMigrateConfig): DbConn =
case config.driver
of "postgres": discard
of "sqlite": discard
of "mysql": discard
else: dbError "Unsupported database driver: " & config.driver
proc createMigration*(config: DbMigrateConfig, migrationName: string): seq[string] =
## Create a new set of database migration files. ## Create a new set of database migration files.
let timestamp = getTime().getLocalTime().format("yyyyMMddHHmmss") let timestamp = getTime().getLocalTime().format("yyyyMMddHHmmss")
let filenamePrefix = timestamp & "-" & migrationName let filenamePrefix = timestamp & "-" & migrationName
@ -41,6 +48,9 @@ when isMainModule:
let doc = """ let doc = """
Usage: Usage:
db-migrate [options] create <migration-name> db-migrate [options] create <migration-name>
db-migrate [options] up [<count>]
db-migrate [options] down [<count>]
db-migrate [options] init <schema-name>
Options: Options:
-c --config <config-file> Use the given configuration file (defaults to -c --config <config-file> Use the given configuration file (defaults to
@ -54,7 +64,6 @@ Options:
stderr.writeLine("db_migrate: " & msg) stderr.writeLine("db_migrate: " & msg)
quit(QuitFailure) quit(QuitFailure)
# Load configuration file # Load configuration file
let configFilename = let configFilename =
if args["--config"]: $args["--config"] if args["--config"]: $args["--config"]
@ -86,3 +95,36 @@ Options:
except IOError: except IOError:
exitErr "Unable to create migration scripts: " & getCurrentExceptionMsg() exitErr "Unable to create migration scripts: " & getCurrentExceptionMsg()
elif args["up"]: discard
# Query the database to find out what migrations have been run.
# Diff with the list of migrations that we have in our migrations
# directory.
# Make sure we have no gaps (database is in an unknown state)
# Find the subset of migrations we need to apply (consider the count
# parameter if passed)
# If none: "Up to date."
# Begin a transaction.
# Apply each of the migrations.
# If any fail, roll back the transaction
# Otherwise report success
elif args["down"]: discard
# Query the database to find out what migrations have been run.
# Find how many we need to go down (default to 1 if the count parameter was
# not passed.
# Begin transaction
# Apply each down script
# If any fail, roll back the transaction
# Otherwise report success
elif args["init"]: discard