From daf3a8dad0ccd81d56aa060e9720e8abdee10987 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Tue, 1 Sep 2020 16:30:50 -0500 Subject: [PATCH] Rename migrationsDir sqlDir. --- README.md | 36 +++++++++++++++++-- .../com/jdblabs/dbmigrate/DbMigrate.groovy | 22 ++++++------ src/main/nim/db_migrate.nim | 3 ++ 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c8752eb..ba0d121 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,36 @@ -DB Migrate -========== +# DB Migrate Small tool(s) to manage database migrations in various languages. + +## Usage + +``` +Usage: + db_migrate [options] create + db_migrate [options] up [] + db_migrate [options] down [] + db_migrate [options] init + db_migrate (-V | --version) + db_migrate (-h | --help) + +Options: + -c --config Use the given configuration file (defaults to + "database.properties"). + -q --quiet Suppress log information. + -v --verbose Print detailed log information. + --very-verbose Print very detailed log information. + -V --version Print the tools version information. + -h --help Print this usage information. +``` + + +## Database Config Format + +The database config is formatted as JSON. The following keys are supported by +all of the implementations: + +* `sqlDir` -- Directory to store SQL files. + +The following keys are supported by the Nim implementation: + +* `connectionString` -- diff --git a/src/main/groovy/com/jdblabs/dbmigrate/DbMigrate.groovy b/src/main/groovy/com/jdblabs/dbmigrate/DbMigrate.groovy index 9da2fb7..672877c 100644 --- a/src/main/groovy/com/jdblabs/dbmigrate/DbMigrate.groovy +++ b/src/main/groovy/com/jdblabs/dbmigrate/DbMigrate.groovy @@ -42,7 +42,7 @@ Options: private static Logger LOGGER = LoggerFactory.getLogger(DbMigrate) Sql sql - File migrationsDir + File sqlDir public static void main(String[] args) { @@ -90,14 +90,14 @@ Options: givenCfg.clear() } } // Check for migrations directory - File migrationsDir = new File(givenCfg["migrations.dir"] ?: 'migrations') - if (!migrationsDir.exists() || !migrationsDir.isDirectory()) { + File sqlDir = new File(givenCfg["sqlDir"] ?: 'migrations') + if (!sqlDir.exists() || !sqlDir.isDirectory()) { clilog.error("'{}' does not exist or is not a directory.", - migrationsDir.canonicalPath) + sqlDir.canonicalPath) System.exit(1) } // Instantiate the DbMigrate instance - DbMigrate dbmigrate = new DbMigrate(migrationsDir: migrationsDir) + DbMigrate dbmigrate = new DbMigrate(sqlDir: sqlDir) // If we've only been asked to create a new migration, we don't need to // setup the DB connection. @@ -112,7 +112,7 @@ Options: // Create the datasource. Properties dsProps = new Properties() - dsProps.putAll(givenCfg.findAll { it.key != 'migrations.dir' }) + dsProps.putAll(givenCfg.findAll { it.key != 'sqlDir' }) HikariDataSource hds = new HikariDataSource(new HikariConfig(dsProps)) @@ -125,8 +125,8 @@ Options: public List createMigration(String migrationName) { String timestamp = sdf.format(new Date()) - File upFile = new File(migrationsDir, "$timestamp-$migrationName-up.sql") - File downFile = new File(migrationsDir, "$timestamp-$migrationName-down.sql") + File upFile = new File(sqlDir, "$timestamp-$migrationName-up.sql") + File downFile = new File(sqlDir, "$timestamp-$migrationName-down.sql") upFile.text = "-- UP script for $migrationName ($timestamp)" downFile.text = "-- DOWN script for $migrationName ($timestamp)" @@ -140,7 +140,7 @@ Options: CREATE TABLE IF NOT EXISTS migrations ( id SERIAL PRIMARY KEY, name VARCHAR NOT NULL, - run_at TIMESTAMP NOT NULL DEFAULT NOW())''') } + run_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW())''') } public def diffMigrations() { def results = [notRun: [], missing: []] @@ -150,7 +150,7 @@ CREATE TABLE IF NOT EXISTS migrations ( .collect { it.name }.sort() SortedSet available = new TreeSet<>() - available.addAll(migrationsDir + available.addAll(sqlDir .listFiles({ d, n -> n ==~ /.+-(up|down).sql$/ } as FilenameFilter) .collect { f -> f.name.replaceAll(/-(up|down).sql$/, '') }) @@ -215,7 +215,7 @@ CREATE TABLE IF NOT EXISTS migrations ( toRun.each { migrationName -> LOGGER.info(migrationName) - File migrationFile = new File(migrationsDir, + File migrationFile = new File(sqlDir, "$migrationName-${up ? 'up' : 'down'}.sql") if (!migrationFile.exists() || !migrationFile.isFile()) diff --git a/src/main/nim/db_migrate.nim b/src/main/nim/db_migrate.nim index 93ae483..0e2dba6 100644 --- a/src/main/nim/db_migrate.nim +++ b/src/main/nim/db_migrate.nim @@ -196,12 +196,15 @@ Usage: db_migrate [options] down [] db_migrate [options] init db_migrate (-V | --version) + db_migrate (-h | --help) Options: -c --config Use the given configuration file (defaults to "database.json"). + -h --help Show this usage information. + -q --quiet Suppress log information. -v --verbose Print detailed log information.