Skip to content

Commit

Permalink
Add CLI readme + improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pditommaso committed Mar 14, 2022
1 parent fcf7ad4 commit 7554a0d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ignore Gradle project-specific cache directory
.gradle
.idea
.nextflow

# Ignore Gradle build output directory
build
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ MigTool is designed to be embedded in the target app requiring. Use the followin
```

## Binary release

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](https://github.com/seqeralabs/migtool/releases/latest), then grant execute permission,
finally run it following the example below:

```
./migtool \
--user <foo> \
--password <some secret>
--url <jdbc:mysql://host.com:3306/some-schema>
--location <file:/some/path/to/migration/scripts.sql>
```
#### Options

* `user`: 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 with `file:`.
* `dialect`: Either `mysql` or `h2` (optional).
* `driver`: JDBC driver class name to connect the target database (optional).

## License

[Mozilla Public License v2.0](LICENSE.txt)
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/io/seqera/migtool/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import static io.seqera.migtool.Helper.driverFromUrl;
import static io.seqera.migtool.Helper.dialectFromUrl;

/**
* Mig tool main launcher
*
Expand All @@ -30,8 +34,8 @@ public class App implements Callable<Integer> {
@Option(names = {"--driver"}, description = "JDBC driver class name")
private String driver;

@Option(names = {"--locations"}, description = "DB migration scripts location path (local path should be prefixed with `file:`)")
private String locations;
@Option(names = {"--location"}, description = "DB migration scripts location path (local path should be prefixed with `file:`)")
private String location;

@Option(names = {"--pattern"}, description = "DB migration scripts file names pattern")
private String pattern = "^m(\\d+)__(.+)";
Expand All @@ -44,9 +48,9 @@ public Integer call() throws Exception {
.withUser(username)
.withPassword(password)
.withUrl(url)
.withDialect(dialect)
.withDriver(driver)
.withLocations(locations)
.withDialect(dialect!=null ? dialect : dialectFromUrl(url))
.withDriver(driver!=null ? driver : driverFromUrl(url))
.withLocations(location)
.withPattern(pattern);

try {
Expand All @@ -63,6 +67,7 @@ public Integer call() throws Exception {
}
}


public static void main(String[] args) {
int exitCode = new CommandLine(new App()).execute(args);
System.exit(exitCode);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/seqera/migtool/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,19 @@ private static void closeWithWarning(Closeable closeable) {
tryClose(closeable);
}

static public String dialectFromUrl(String url) {
if( url==null )
return null;
String[] parts = url.split(":");
return parts.length > 1 ? parts[1] : null;
}

static public String driverFromUrl(String url) {
final String dialect = dialectFromUrl(url);
if( "mysql".equals(dialect) )
return "com.mysql.cj.jdbc.Driver";
if( "h2".equals(dialect))
return "org.h2.Driver";
return null;
}
}
4 changes: 1 addition & 3 deletions src/nativeCliTest/groovy/io/seqera/migtool/MysqlTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class MysqlTest extends Specification {
'-u', container.username,
'-p', container.password,
'--url', container.jdbcUrl,
'--driver', 'com.mysql.cj.jdbc.Driver',
'--dialect', 'mysql',
'--locations', 'file:src/test/resources/migrate-db/mysql' ]
'--location', 'file:src/test/resources/migrate-db/mysql' ]

when:
println "Running: ${CLI.join( )}"
Expand Down
13 changes: 13 additions & 0 deletions src/test/groovy/io/seqera/migtool/HelperTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ class HelperTest extends Specification {
Helper.computeSha256('Hola mundo' ) == 'ca8f60b2cc7f05837d98b208b57fb6481553fc5f1219d59618fd025002a66f5c'
}

def 'should parse dialect from url' () {
expect:
Helper.dialectFromUrl() == null
Helper.dialectFromUrl('jdbc:h2:file:./.db/h2/tower') == 'h2'
Helper.dialectFromUrl('jdbc:mysql://foo.com:3306/licman') == 'mysql'
}

def 'should get driver from url' () {
expect:
Helper.driverFromUrl() == null
Helper.driverFromUrl('jdbc:h2:file:./.db/h2/tower') == 'org.h2.Driver'
Helper.driverFromUrl('jdbc:mysql://foo.com:3306/licman') == 'com.mysql.cj.jdbc.Driver'
}
}

0 comments on commit 7554a0d

Please sign in to comment.