From 9e0b264cae1efa67f2d89e49827e961e6a8ad202 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sun, 31 Jan 2016 12:27:37 -0600 Subject: [PATCH] Added stubs for up, down, init. Starting on connection creation (BROKEN). --- db_migrate.nim | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/db_migrate.nim b/db_migrate.nim index 0cfe76a..41d6ccf 100644 --- a/db_migrate.nim +++ b/db_migrate.nim @@ -3,7 +3,7 @@ ## ## 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 ] @@ -16,7 +16,14 @@ proc loadConfig*(filename: string): DbMigrateConfig = sqlDir: if cfg.hasKey("sqlDir"): cfg["sqlDir"].getStr else: "migrations", 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. let timestamp = getTime().getLocalTime().format("yyyyMMddHHmmss") let filenamePrefix = timestamp & "-" & migrationName @@ -41,6 +48,9 @@ when isMainModule: let doc = """ Usage: db-migrate [options] create + db-migrate [options] up [] + db-migrate [options] down [] + db-migrate [options] init Options: -c --config Use the given configuration file (defaults to @@ -54,7 +64,6 @@ Options: stderr.writeLine("db_migrate: " & msg) quit(QuitFailure) - # Load configuration file let configFilename = if args["--config"]: $args["--config"] @@ -86,3 +95,36 @@ Options: except IOError: 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 +