Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
6bf2b86592 | |||
9b3e7b4d26 | |||
5b9de9b10f | |||
109a667fd5 | |||
462da00dd3 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ nimcache/
|
||||
*.sw?
|
||||
build/
|
||||
db_migrate
|
||||
.gradle/
|
||||
|
@ -1,4 +1,4 @@
|
||||
Nim DB Migrate
|
||||
==============
|
||||
DB Migrate
|
||||
==========
|
||||
|
||||
Small tool to manage database migrations in Nim.
|
||||
Small tool(s) to manage database migrations in various languages.
|
||||
|
26
build.gradle
Normal file
26
build.gradle
Normal file
@ -0,0 +1,26 @@
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'application'
|
||||
|
||||
group = 'com.jdblabs'
|
||||
version = '0.2.2'
|
||||
|
||||
mainClassName = 'com.jdblabs.dbmigrate.DbMigrate'
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile localGroovy()
|
||||
compile 'ch.qos.logback:logback-classic:1.1.3'
|
||||
compile 'ch.qos.logback:logback-core:1.1.3'
|
||||
compile 'com.jdbernard:jdb-util:4.4'
|
||||
compile 'com.offbytwo:docopt:0.6.0.20150202'
|
||||
compile 'com.zaxxer:HikariCP:2.4.3'
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
|
||||
runtime 'com.h2database:h2:1.4.185'
|
||||
runtime 'org.postgresql:postgresql:9.4.1207.jre7'
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
# Package
|
||||
|
||||
bin = @["db_migrate"]
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Simple tool to handle database migrations."
|
||||
license = "BSD"
|
||||
srcDir = "src/main/nim"
|
||||
|
||||
# Dependencies
|
||||
|
||||
|
1
settings.gradle
Normal file
1
settings.gradle
Normal file
@ -0,0 +1 @@
|
||||
rootProject.name = "db-migrate.groovy"
|
251
src/main/groovy/com/jdblabs/dbmigrate/DbMigrate.groovy
Normal file
251
src/main/groovy/com/jdblabs/dbmigrate/DbMigrate.groovy
Normal file
@ -0,0 +1,251 @@
|
||||
package com.jdblabs.dbmigrate
|
||||
|
||||
import groovy.sql.Sql
|
||||
|
||||
import ch.qos.logback.classic.Level
|
||||
import ch.qos.logback.classic.Logger as LBLogger
|
||||
import com.jdbernard.util.AnsiEscapeCodeSequence as ANSI
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import java.io.FilenameFilter
|
||||
import java.text.SimpleDateFormat
|
||||
import org.docopt.Docopt
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
public class DbMigrate {
|
||||
|
||||
public static final VERSION = "0.2.2"
|
||||
|
||||
public static final def DOC = """\
|
||||
db-migrate.groovy v${VERSION}
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
private static sdf = new SimpleDateFormat('yyyyMMddHHmmss')
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(DbMigrate)
|
||||
|
||||
Sql sql
|
||||
File migrationsDir
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Parse arguments
|
||||
def opts = new Docopt(DOC).withVersion("wdiwtlt v$VERSION").parse(args)
|
||||
|
||||
if (opts['--version']) {
|
||||
println "db-migrate.groovy v$VERSION"
|
||||
System.exit(0) }
|
||||
|
||||
if (opts['--help']) { println DOC; System.exit(0) }
|
||||
|
||||
// TODO: Setup logging & output levels
|
||||
Logger clilog = LoggerFactory.getLogger("db-migrate.cli")
|
||||
|
||||
if (opts['--quiet']) {
|
||||
((LBLogger) LOGGER).level = Level.ERROR
|
||||
((LBLogger) LoggerFactory.getLogger(LBLogger.ROOT_LOGGER_NAME))
|
||||
.level = Level.ERROR }
|
||||
if (opts['--verbose']) {
|
||||
((LBLogger) LOGGER).level = Level.DEBUG
|
||||
((LBLogger) LoggerFactory.getLogger(LBLogger.ROOT_LOGGER_NAME))
|
||||
.level = Level.INFO }
|
||||
if (opts['--very-verbose']) {
|
||||
((LBLogger) LOGGER).level = Level.TRACE
|
||||
((LBLogger) LoggerFactory.getLogger(LBLogger.ROOT_LOGGER_NAME))
|
||||
.level = Level.DEBUG }
|
||||
|
||||
// Load the configuration file
|
||||
def givenCfg = new Properties()
|
||||
File cfgFile
|
||||
if (opts["--config"]) cfgFile = new File(opts["--config"])
|
||||
|
||||
if (!cfgFile || !cfgFile.exists() || !cfgFile.isFile())
|
||||
cfgFile = new File("database.properties")
|
||||
|
||||
if (!cfgFile.exists() || !cfgFile.isFile()) {
|
||||
clilog.warn("Config file '{}' does not exist or is not a regular file.",
|
||||
cfgFile.canonicalPath) }
|
||||
|
||||
if (cfgFile.exists() && cfgFile.isFile()) {
|
||||
try { cfgFile.withInputStream { givenCfg.load(it) } }
|
||||
catch (Exception e) {
|
||||
clilog.error("Could not read configuration file.", e)
|
||||
givenCfg.clear() } }
|
||||
|
||||
// Check for migrations directory
|
||||
File migrationsDir = new File(givenCfg["migrations.dir"] ?: 'migrations')
|
||||
if (!migrationsDir.exists() || !migrationsDir.isDirectory()) {
|
||||
clilog.error("'{}' does not exist or is not a directory.",
|
||||
migrationsDir.canonicalPath)
|
||||
System.exit(1) }
|
||||
|
||||
// Instantiate the DbMigrate instance
|
||||
DbMigrate dbmigrate = new DbMigrate(migrationsDir: migrationsDir)
|
||||
|
||||
// If we've only been asked to create a new migration, we don't need to
|
||||
// setup the DB connection.
|
||||
if (opts['create']) {
|
||||
try {
|
||||
List<File> files = dbmigrate.createMigration(opts['<migration-name>'])
|
||||
clilog.info("Created new migration files:\n\t${files.name.join('\n\t')}")
|
||||
return }
|
||||
catch (Exception e) {
|
||||
clilog.error('Unable to create migration scripts.', e)
|
||||
System.exit(1) } }
|
||||
|
||||
// Create the datasource.
|
||||
Properties dsProps = new Properties()
|
||||
dsProps.putAll(givenCfg.findAll { it.key != 'migrations.dir' })
|
||||
|
||||
HikariDataSource hds = new HikariDataSource(new HikariConfig(dsProps))
|
||||
|
||||
dbmigrate.sql = new Sql(hds)
|
||||
|
||||
// Execute the appropriate command.
|
||||
if (opts['up']) dbmigrate.up(opts['<count>'])
|
||||
else if (opts['down']) dbmigrate.down(opts['<count>'] ?: 1) }
|
||||
|
||||
public List<File> 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")
|
||||
|
||||
upFile.text = "-- UP script for $migrationName ($timestamp)"
|
||||
downFile.text = "-- DOWN script for $migrationName ($timestamp)"
|
||||
|
||||
return [upFile, downFile] }
|
||||
|
||||
public def createMigrationsTable() {
|
||||
LOGGER.trace('Checking for the existence of the migrations table and ' +
|
||||
'creating it if it does not exist.')
|
||||
sql.execute('''
|
||||
CREATE TABLE IF NOT EXISTS migrations (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL,
|
||||
run_at TIMESTAMP NOT NULL DEFAULT NOW())''') }
|
||||
|
||||
public def diffMigrations() {
|
||||
def results = [notRun: [], missing: []]
|
||||
|
||||
LOGGER.trace('Diffing migrations...')
|
||||
results.run = sql.rows('SELECT name FROM migrations ORDER BY name')
|
||||
.collect { it.name }.sort()
|
||||
|
||||
SortedSet<String> available = new TreeSet<>()
|
||||
available.addAll(migrationsDir
|
||||
.listFiles({ d, n -> n ==~ /.+-(up|down).sql$/ } as FilenameFilter)
|
||||
.collect { f -> f.name.replaceAll(/-(up|down).sql$/, '') })
|
||||
|
||||
available.each { migrationName ->
|
||||
if (!results.run.contains(migrationName))
|
||||
results.notRun << migrationName
|
||||
|
||||
// If we've already seen some migrations that have not been run but this
|
||||
// one has been run, that means we have a gap and are missing migrations.
|
||||
else if (results.notRun.size() > 0) {
|
||||
results.missing += reults.notRun
|
||||
results.notRun = [] } }
|
||||
|
||||
LOGGER.trace('Migrations diff:\n\trun: {}\n\tnot run: {}\n\tmissing: {}',
|
||||
results.run, results.notRun, results.missing)
|
||||
|
||||
return results }
|
||||
|
||||
public List<String> up(Integer count = null) {
|
||||
createMigrationsTable()
|
||||
def diff = diffMigrations()
|
||||
|
||||
if (diff.missing) {
|
||||
LOGGER.error('Missing migrations:\n\t{}', diff.missing)
|
||||
throw new Exception('Database is in an inconsistent state.') }
|
||||
|
||||
LOGGER.debug('Migrating up.')
|
||||
List<String> toRun
|
||||
|
||||
if (!count || count >= diff.notRun.size()) toRun = diff.notRun
|
||||
else toRun = diff.notRun[0..<count]
|
||||
|
||||
LOGGER.debug('{} migrations to run.', toRun.size())
|
||||
LOGGER.trace('Migrations: {}.', toRun)
|
||||
|
||||
return runMigrations(toRun, true) }
|
||||
|
||||
public List<String> down(Integer count = 1) {
|
||||
createMigrationsTable()
|
||||
def diff = diffMigrations()
|
||||
|
||||
if (diff.missing) {
|
||||
LOGGER.error('Missing migrations:\n\t{}', diff.missing)
|
||||
throw new Exception('Database is in an inconsistent state.') }
|
||||
|
||||
LOGGER.debug('Migrating down.')
|
||||
|
||||
List<String> toRun = count < diff.run.size() ?
|
||||
diff.run.reverse()[0..<count] : diff.run.reverse()
|
||||
|
||||
LOGGER.debug('{} migrations to run.', toRun.size())
|
||||
LOGGER.trace('Migrations: {}.', toRun)
|
||||
|
||||
return runMigrations(toRun, false) }
|
||||
|
||||
private List<String> runMigrations(List<String> toRun, boolean up = true) {
|
||||
List<String> migrationsRun = []
|
||||
|
||||
try {
|
||||
LOGGER.trace("Beginning transaction.")
|
||||
sql.execute('BEGIN')
|
||||
|
||||
toRun.each { migrationName ->
|
||||
LOGGER.info(migrationName)
|
||||
File migrationFile = new File(migrationsDir,
|
||||
"$migrationName-${up ? 'up' : 'down'}.sql")
|
||||
|
||||
if (!migrationFile.exists() || !migrationFile.isFile())
|
||||
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) }
|
||||
|
||||
if (up) sql.execute(
|
||||
'INSERT INTO migrations (name) VALUES (?)', migrationName)
|
||||
else sql.execute(
|
||||
'DELETE FROM migrations WHERE name = ?', migrationName)
|
||||
|
||||
migrationsRun << migrationName }
|
||||
|
||||
sql.execute('COMMIT')
|
||||
LOGGER.info('Went {} {} migrations.',
|
||||
up ? 'up' : 'down', migrationsRun.size()) }
|
||||
|
||||
catch (Exception e) { sql.execute('ROLLBACK'); }
|
||||
|
||||
return migrationsRun }
|
||||
}
|
@ -182,7 +182,7 @@ Options:
|
||||
"""
|
||||
|
||||
# Parse arguments
|
||||
let args = docopt(doc, version = "db-migrate 0.2.1")
|
||||
let args = docopt(doc, version = "db-migrate 0.2.2")
|
||||
|
||||
let exitErr = proc(msg: string): void =
|
||||
fatal("db_migrate: " & msg)
|
17
src/main/resources/logback.groovy
Normal file
17
src/main/resources/logback.groovy
Normal file
@ -0,0 +1,17 @@
|
||||
import ch.qos.logback.core.*;
|
||||
import ch.qos.logback.core.encoder.*;
|
||||
import ch.qos.logback.core.read.*;
|
||||
import ch.qos.logback.core.rolling.*;
|
||||
import ch.qos.logback.core.status.*;
|
||||
import ch.qos.logback.classic.net.*;
|
||||
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
|
||||
|
||||
|
||||
appender("STDOUT", ConsoleAppender) {
|
||||
encoder(PatternLayoutEncoder) {
|
||||
pattern = "db-migrate.groovy: %level - %msg%n"
|
||||
}
|
||||
}
|
||||
|
||||
root(WARN, ["STDOUT"])
|
||||
logger('com.jdblabs.dbmigrate', INFO)
|
Reference in New Issue
Block a user