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

@ -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) } }
}