Rename migrationsDir sqlDir.

This commit is contained in:
Jonathan Bernard 2020-09-01 16:30:50 -05:00
parent 4e771345ea
commit daf3a8dad0
3 changed files with 48 additions and 13 deletions

View File

@ -1,4 +1,36 @@
DB Migrate # DB Migrate
==========
Small tool(s) to manage database migrations in various languages. Small tool(s) to manage database migrations in various languages.
## Usage
```
Usage:
db_migrate [options] create <migration-name>
db_migrate [options] up [<count>]
db_migrate [options] down [<count>]
db_migrate [options] init <schema-name>
db_migrate (-V | --version)
db_migrate (-h | --help)
Options:
-c --config <config-file> 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` --

View File

@ -42,7 +42,7 @@ Options:
private static Logger LOGGER = LoggerFactory.getLogger(DbMigrate) private static Logger LOGGER = LoggerFactory.getLogger(DbMigrate)
Sql sql Sql sql
File migrationsDir File sqlDir
public static void main(String[] args) { public static void main(String[] args) {
@ -90,14 +90,14 @@ Options:
givenCfg.clear() } } givenCfg.clear() } }
// Check for migrations directory // Check for migrations directory
File migrationsDir = new File(givenCfg["migrations.dir"] ?: 'migrations') File sqlDir = new File(givenCfg["sqlDir"] ?: 'migrations')
if (!migrationsDir.exists() || !migrationsDir.isDirectory()) { if (!sqlDir.exists() || !sqlDir.isDirectory()) {
clilog.error("'{}' does not exist or is not a directory.", clilog.error("'{}' does not exist or is not a directory.",
migrationsDir.canonicalPath) sqlDir.canonicalPath)
System.exit(1) } System.exit(1) }
// Instantiate the DbMigrate instance // 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 // If we've only been asked to create a new migration, we don't need to
// setup the DB connection. // setup the DB connection.
@ -112,7 +112,7 @@ Options:
// Create the datasource. // Create the datasource.
Properties dsProps = new Properties() 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)) HikariDataSource hds = new HikariDataSource(new HikariConfig(dsProps))
@ -125,8 +125,8 @@ Options:
public List<File> createMigration(String migrationName) { public List<File> createMigration(String migrationName) {
String timestamp = sdf.format(new Date()) String timestamp = sdf.format(new Date())
File upFile = new File(migrationsDir, "$timestamp-$migrationName-up.sql") File upFile = new File(sqlDir, "$timestamp-$migrationName-up.sql")
File downFile = new File(migrationsDir, "$timestamp-$migrationName-down.sql") File downFile = new File(sqlDir, "$timestamp-$migrationName-down.sql")
upFile.text = "-- UP script for $migrationName ($timestamp)" upFile.text = "-- UP script for $migrationName ($timestamp)"
downFile.text = "-- DOWN script for $migrationName ($timestamp)" downFile.text = "-- DOWN script for $migrationName ($timestamp)"
@ -140,7 +140,7 @@ Options:
CREATE TABLE IF NOT EXISTS migrations ( CREATE TABLE IF NOT EXISTS migrations (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL, name VARCHAR NOT NULL,
run_at TIMESTAMP NOT NULL DEFAULT NOW())''') } run_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW())''') }
public def diffMigrations() { public def diffMigrations() {
def results = [notRun: [], missing: []] def results = [notRun: [], missing: []]
@ -150,7 +150,7 @@ CREATE TABLE IF NOT EXISTS migrations (
.collect { it.name }.sort() .collect { it.name }.sort()
SortedSet<String> available = new TreeSet<>() SortedSet<String> available = new TreeSet<>()
available.addAll(migrationsDir available.addAll(sqlDir
.listFiles({ d, n -> n ==~ /.+-(up|down).sql$/ } as FilenameFilter) .listFiles({ d, n -> n ==~ /.+-(up|down).sql$/ } as FilenameFilter)
.collect { f -> f.name.replaceAll(/-(up|down).sql$/, '') }) .collect { f -> f.name.replaceAll(/-(up|down).sql$/, '') })
@ -215,7 +215,7 @@ CREATE TABLE IF NOT EXISTS migrations (
toRun.each { migrationName -> toRun.each { migrationName ->
LOGGER.info(migrationName) LOGGER.info(migrationName)
File migrationFile = new File(migrationsDir, File migrationFile = new File(sqlDir,
"$migrationName-${up ? 'up' : 'down'}.sql") "$migrationName-${up ? 'up' : 'down'}.sql")
if (!migrationFile.exists() || !migrationFile.isFile()) if (!migrationFile.exists() || !migrationFile.isFile())

View File

@ -196,12 +196,15 @@ Usage:
db_migrate [options] down [<count>] db_migrate [options] down [<count>]
db_migrate [options] init <schema-name> db_migrate [options] init <schema-name>
db_migrate (-V | --version) db_migrate (-V | --version)
db_migrate (-h | --help)
Options: Options:
-c --config <config-file> Use the given configuration file (defaults to -c --config <config-file> Use the given configuration file (defaults to
"database.json"). "database.json").
-h --help Show this usage information.
-q --quiet Suppress log information. -q --quiet Suppress log information.
-v --verbose Print detailed log information. -v --verbose Print detailed log information.