Separated statement running logic in Groovy implementation to allow reuse.
This commit is contained in:
parent
b49b648358
commit
c933d6ac2b
@ -3,7 +3,7 @@ apply plugin: 'application'
|
|||||||
apply plugin: 'maven'
|
apply plugin: 'maven'
|
||||||
|
|
||||||
group = 'com.jdblabs'
|
group = 'com.jdblabs'
|
||||||
version = '0.2.2'
|
version = '0.2.3'
|
||||||
|
|
||||||
mainClassName = 'com.jdblabs.dbmigrate.DbMigrate'
|
mainClassName = 'com.jdblabs.dbmigrate.DbMigrate'
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
bin = @["db_migrate"]
|
bin = @["db_migrate"]
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
author = "Jonathan Bernard"
|
author = "Jonathan Bernard"
|
||||||
description = "Simple tool to handle database migrations."
|
description = "Simple tool to handle database migrations."
|
||||||
license = "BSD"
|
license = "BSD"
|
||||||
|
@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory
|
|||||||
|
|
||||||
public class DbMigrate {
|
public class DbMigrate {
|
||||||
|
|
||||||
public static final VERSION = "0.2.2"
|
public static final VERSION = "0.2.3"
|
||||||
|
|
||||||
public static final def DOC = """\
|
public static final def DOC = """\
|
||||||
db-migrate.groovy v${VERSION}
|
db-migrate.groovy v${VERSION}
|
||||||
@ -222,17 +222,7 @@ CREATE TABLE IF NOT EXISTS migrations (
|
|||||||
throw new FileNotFoundException(migrationFile.canonicalPath +
|
throw new FileNotFoundException(migrationFile.canonicalPath +
|
||||||
"does not exist or is not a regular file.")
|
"does not exist or is not a regular file.")
|
||||||
|
|
||||||
LOGGER.trace('Raw statements:\n\n{}\n', migrationFile.text.split(/;/).join('\n'))
|
runFile(migrationFile)
|
||||||
|
|
||||||
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) }
|
|
||||||
|
|
||||||
if (up) sql.execute(
|
if (up) sql.execute(
|
||||||
'INSERT INTO migrations (name) VALUES (?)', migrationName)
|
'INSERT INTO migrations (name) VALUES (?)', migrationName)
|
||||||
@ -248,4 +238,17 @@ CREATE TABLE IF NOT EXISTS migrations (
|
|||||||
catch (Exception e) { sql.execute('ROLLBACK'); }
|
catch (Exception e) { sql.execute('ROLLBACK'); }
|
||||||
|
|
||||||
return migrationsRun }
|
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) } }
|
||||||
}
|
}
|
||||||
|
@ -182,7 +182,7 @@ Options:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Parse arguments
|
# 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 =
|
let exitErr = proc(msg: string): void =
|
||||||
fatal("db_migrate: " & msg)
|
fatal("db_migrate: " & msg)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user