Separated statement running logic in Groovy implementation to allow reuse.

This commit is contained in:
Jonathan Bernard 2016-05-17 21:35:57 -05:00
parent b49b648358
commit c933d6ac2b
4 changed files with 18 additions and 15 deletions

View File

@ -3,7 +3,7 @@ apply plugin: 'application'
apply plugin: 'maven'
group = 'com.jdblabs'
version = '0.2.2'
version = '0.2.3'
mainClassName = 'com.jdblabs.dbmigrate.DbMigrate'

View File

@ -1,7 +1,7 @@
# Package
bin = @["db_migrate"]
version = "0.2.2"
version = "0.2.3"
author = "Jonathan Bernard"
description = "Simple tool to handle database migrations."
license = "BSD"

View File

@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory
public class DbMigrate {
public static final VERSION = "0.2.2"
public static final VERSION = "0.2.3"
public static final def DOC = """\
db-migrate.groovy v${VERSION}
@ -222,17 +222,7 @@ CREATE TABLE IF NOT EXISTS migrations (
throw new FileNotFoundException(migrationFile.canonicalPath +
"does not exist or is not a regular file.")
LOGGER.trace('Raw statements:\n\n{}\n', migrationFile.text.split(/;/).join('\n'))
List<String> statements = migrationFile.text.split(/;/)
.collect { it.replaceAll(/--.*$/, '').trim() }
.findAll { it.length() > 0 }
LOGGER.trace('Statements:\n\n{}\n', statements.join('\n'))
statements.each {
LOGGER.trace('Executing SQL: {}', it)
sql.execute(it) }
runFile(migrationFile)
if (up) sql.execute(
'INSERT INTO migrations (name) VALUES (?)', migrationName)
@ -248,4 +238,17 @@ CREATE TABLE IF NOT EXISTS migrations (
catch (Exception e) { sql.execute('ROLLBACK'); }
return migrationsRun }
public void runFile(File file) {
LOGGER.trace('Raw statements:\n\n{}\n', file.text.split(/;/).join('\n'))
List<String> statements = file.text.split(/;/)
.collect { it.replaceAll(/--.*$/, '').trim() }
.findAll { it.length() > 0 }
LOGGER.trace('Statements:\n\n{}\n', statements.join('\n'))
statements.each {
LOGGER.trace('Executing SQL: {}', it)
sql.execute(it) } }
}

View File

@ -182,7 +182,7 @@ Options:
"""
# Parse arguments
let args = docopt(doc, version = "db-migrate 0.2.2")
let args = docopt(doc, version = "db-migrate 0.2.3")
let exitErr = proc(msg: string): void =
fatal("db_migrate: " & msg)