Implements a no-frills DB migration tool inspired by Flyway.
Migration files are a simple collection of SQL DDL/DML statements separated by ; character,
that can be stored either in a local directory or as resources in the application
classpath.
In addition, the migration files can be written in Groovy in order to run scripts using groovy-sql.
Only requirement is that migration files follow the pattern V99__Some_name.[sql|groovy], where
99 can be any integer value.
For each migration file that needs to be corrected a pair of .patch.sql and .override.sql must exists.
The behaviour will follow these rules:
- WHEN file
.sqlexists and was executed AND file.patch.sql, file.override.sql pair exists -> apply patch - WHEN file
.sqlexists and was not executed AND file.patch.sql, file.override.sqlpair exists -> apply override
If for a given file there is only either a single .patch or a single .override then the tool will throw an illegal state exception stopping the migration process
MigTool is designed to be embedded in the target app. Use the following idiom:
MigTool tool = new MigTool();
.withDriver('org.h2.Driver')
.withDialect('h2')
.withUrl('jdbc:h2:mem:test')
.withUser('sa')
.withPassword('')
.withLocations('classpath:test')
try {
tool.run();
}
finally {
tool.close();
}MigTool is also released as a pre-compiled Linux binary including H2 and MySQL drivers. To use it, download the binary package at this link, then grant execute permission, finally run it following the example below:
./migtool \
--username <username> \
--password <password> \
--url jdbc:mysql://<host>:3306/<schema> \
--location file:/path/to/migration/scriptsWarning
Due to current limitations in GraalVM when it comes to running arbitrary Groovy scripts, the binary can only run SQL migration files.
Note
When building on macOS, if you encounter the error
com.oracle.svm.core.util.UserError$UserException: DARWIN does not support building static executable images, you need to remove the-H:+StaticExecutableWithDynamicLibCflag from thebuildArgsinbuild.gradle.
username: Target database connection username.password: Target database connection password.url: Target database JDBC connection URL.location: File system path where migration scripts are located. Path must be prefixed withfile:.dialect: Eithermysqlorh2(optional).driver: JDBC driver class name to connect the target database (optional).