Initial commit: create verb only.
This commit is contained in:
commit
66bb8d9d39
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
nimcache/
|
||||
*.sw?
|
||||
build/
|
||||
db_migrate
|
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
||||
Nim DB Migrate
|
||||
==============
|
||||
|
||||
Small tool to manage database migrations in Nim.
|
73
db_migrate.nim
Normal file
73
db_migrate.nim
Normal file
@ -0,0 +1,73 @@
|
||||
## DB Migrate
|
||||
## ==========
|
||||
##
|
||||
## Simple tool to manage database migrations.
|
||||
|
||||
import json, times, os, docopt
|
||||
|
||||
type DbMigrateConfig* = tuple[ driver, sqlDir, connectionString: string ]
|
||||
|
||||
proc loadConfig*(filename: string): DbMigrateConfig =
|
||||
let cfg = json.parseFile(filename)
|
||||
let config = (
|
||||
driver: if cfg.hasKey("driver"): cfg["driver"].getStr else: "postres",
|
||||
sqlDir: if cfg.hasKey("sqlDir"): cfg["sqlDir"].getStr else: "migrations",
|
||||
connectionString: cfg["connectionString"].getStr)
|
||||
|
||||
# Check for migrations directory
|
||||
# TODO: try/except
|
||||
if not existsDir config.sqlDir:
|
||||
echo "SQL directory '" & config.sqlDir &
|
||||
"' does not exist and will be created."
|
||||
createDir config.sqlDir
|
||||
|
||||
return config
|
||||
|
||||
proc createMigration*(config: DBMigrateConfig, migrationName: string): seq[string] =
|
||||
let timestamp = getTime().getLocalTime().format("yyyyMMddHHmmss")
|
||||
let filenamePrefix = timestamp & "-" & migrationName
|
||||
|
||||
let upFilename = joinPath(config.sqlDir, filenamePrefix & "-up.sql")
|
||||
let downFilename = joinPath(config.sqlDir, filenamePrefix & "-down.sql")
|
||||
|
||||
let scriptDesc = migrationName & " (" & timestamp & ")"
|
||||
|
||||
# TODO: try/except and catch errors creating files.
|
||||
let upFile = open(upFilename, fmWrite)
|
||||
let downFile = open(downFilename, fmWrite)
|
||||
|
||||
upFile.writeLine "-- UP script for " & scriptDesc
|
||||
downFile.writeLine "-- DOWN script for " & scriptDesc
|
||||
|
||||
upFile.close()
|
||||
downFile.close()
|
||||
|
||||
return @[upFilename, downFilename]
|
||||
|
||||
when isMainModule:
|
||||
let doc = """
|
||||
Usage:
|
||||
db-migrate [options] create <migration-name>
|
||||
|
||||
Options:
|
||||
-c --config <config-file> Use the given configuration file (defaults to
|
||||
"database.json").
|
||||
"""
|
||||
|
||||
# Parse arguments
|
||||
let args = docopt(doc, version = "db-migrate 0.1.0")
|
||||
|
||||
echo $args
|
||||
|
||||
# Load configuration file
|
||||
let configFilename =
|
||||
if args["--config"]: $args["--config"]
|
||||
else: "database.json"
|
||||
|
||||
let config = loadConfig(configFilename)
|
||||
|
||||
# Execute commands
|
||||
if args["create"]:
|
||||
let filesCreated = createMigration(config, $args["<migration-name>"])
|
||||
echo "Created new migration files:"
|
||||
for filename in filesCreated: echo "\t" & filename
|
12
db_migrate.nimble
Normal file
12
db_migrate.nimble
Normal file
@ -0,0 +1,12 @@
|
||||
# Package
|
||||
|
||||
bin = @["db_migrate"]
|
||||
version = "0.1.0"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Simple tool to handle database migrations."
|
||||
license = "BSD"
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires: @["nim >= 0.13.0", "docopt >= 0.1.0"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user