Skip to content

Commit

Permalink
Add tests + rename package
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Aug 8, 2024
1 parent 008f534 commit c79052f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 24 deletions.
7 changes: 2 additions & 5 deletions src/main/java/io/seqera/migtool/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import java.util.concurrent.Callable;

import io.seqera.migtool.builder.SqlTemplate;
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;
import static io.seqera.migtool.Helper.driverFromUrl;

/**
* Mig tool main launcher
Expand Down Expand Up @@ -52,8 +50,7 @@ public Integer call() {
.withDialect(dialect!=null ? dialect : dialectFromUrl(url))
.withDriver(driver!=null ? driver : driverFromUrl(url))
.withLocations(location)
.withPattern(pattern)
.withBuilder(SqlTemplate.from(dialect!=null ? dialect : dialectFromUrl(url)));
.withPattern(pattern);

try {
tool.run();
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/io/seqera/migtool/MigTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

import groovy.lang.Binding;
import groovy.lang.Closure;
import groovy.lang.GroovyShell;
import groovy.sql.Sql;
import io.seqera.migtool.builder.DefaultSqlTemplate;
import io.seqera.migtool.builder.SqlTemplate;
import io.seqera.migtool.template.SqlTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,7 +62,7 @@ public class MigTool {
Pattern pattern;
String schema;
String catalog;
SqlTemplate builder = new DefaultSqlTemplate();
SqlTemplate template = SqlTemplate.defaultTemplate();

private final List<MigRecord> migrationEntries;
private final List<MigRecord> patchEntries;
Expand Down Expand Up @@ -90,6 +96,7 @@ public MigTool withPassword(String password) {

public MigTool withDialect(String dialect) {
this.dialect = dialect;
this.template = SqlTemplate.from(dialect);
return this;
}

Expand All @@ -109,11 +116,6 @@ public MigTool withPattern(String pattern) {
return this;
}

public MigTool withBuilder(SqlTemplate builder) {
this.builder = builder;
return this;
}

/**
* Main application entry point
*/
Expand Down Expand Up @@ -358,7 +360,7 @@ protected void apply() {

protected void checkRank(MigRecord entry) {
try(Connection conn=getConnection(); Statement stm = conn.createStatement()) {
ResultSet rs = stm.executeQuery(builder.selectMaxRank(MIGTOOL_TABLE));
ResultSet rs = stm.executeQuery(template.selectMaxRank(MIGTOOL_TABLE));
int last = rs.next() ? rs.getInt(1) : 0;
int expected = last+1;
if( entry.rank != expected) {
Expand Down Expand Up @@ -442,7 +444,7 @@ private int migrate(MigRecord entry) throws SQLException {
int delta = (int)(System.currentTimeMillis()-now);

// save the current migration
final String insertSql = builder.insetMigration(MIGTOOL_TABLE);
final String insertSql = template.insetMigration(MIGTOOL_TABLE);
try (Connection conn=getConnection(); PreparedStatement insert = conn.prepareStatement(insertSql)) {
insert.setInt(1, entry.rank);
insert.setString(2, entry.script);
Expand All @@ -455,7 +457,7 @@ private int migrate(MigRecord entry) throws SQLException {
}

protected boolean checkMigrated(MigRecord entry) {
final String sql = builder.selectMigration(MIGTOOL_TABLE);
final String sql = template.selectMigration(MIGTOOL_TABLE);

try (Connection conn=getConnection(); PreparedStatement stm = conn.prepareStatement(sql)) {
stm.setInt(1, entry.rank);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.seqera.migtool.builder;
package io.seqera.migtool.template;

/**
* Default SQL template for migtool SQL statements
*
* @author Paolo Di Tommaso <[email protected]>
*/
public class DefaultSqlTemplate extends SqlTemplate {
class DefaultSqlTemplate extends SqlTemplate {
@Override
public String selectMaxRank(String table) {
return "select max(`rank`) from " + table;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.seqera.migtool.builder;
package io.seqera.migtool.template;

/**
* PostreSQL dialect implementation
*
* @author Paolo Di Tommaso <[email protected]>
*/
class PostgresSqlTemplate extends SqlTemplate {
class PostgreSqlTemplate extends SqlTemplate {

@Override
public String selectMaxRank(String table) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.seqera.migtool.builder;
package io.seqera.migtool.template;

/**
* Implements a simple template pattern to provide specialised
* version of required SQL statements depending on the specified SQL "dialect"
*
* @author Paolo Di Tommaso <[email protected]>
*/
Expand All @@ -14,9 +16,13 @@ public abstract class SqlTemplate {

static public SqlTemplate from(String dialect) {
if( "postgresql".equals(dialect) )
return new PostgresSqlTemplate();
return new PostgreSqlTemplate();
else
return new DefaultSqlTemplate();
}

public static SqlTemplate defaultTemplate() {
return new DefaultSqlTemplate();
}

}
29 changes: 28 additions & 1 deletion src/test/groovy/io/seqera/migtool/MigToolTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package io.seqera.migtool

import java.nio.file.Files
import io.seqera.migtool.resources.ClassFromJarWithResources

import io.seqera.migtool.template.SqlTemplate
import io.seqera.migtool.resources.ClassFromJarWithResources
import spock.lang.Specification

class MigToolTest extends Specification {
Expand Down Expand Up @@ -657,4 +658,30 @@ class MigToolTest extends Specification {
thrown(IllegalArgumentException)
}

def 'should validate tool parameters' () {
when:
def tool = new MigTool()
.withDriver('org.h2.Driver')
.withDialect('h2')
.withUrl('jdbc:h2:mem:test15;DB_CLOSE_DELAY=-1')
.withUser('sa')
.withPassword('')
.withLocations("/foo/bar")
then:
tool.driver == 'org.h2.Driver'
tool.dialect == 'h2'
tool.url == 'jdbc:h2:mem:test15;DB_CLOSE_DELAY=-1'
tool.user == 'sa'
tool.password == ''
tool.locations == "/foo/bar"
tool.template.class == SqlTemplate.defaultTemplate().class

when:
tool = new MigTool()
.withDialect('postgresql')
then:
tool.dialect == 'postgresql'
tool.template.class == SqlTemplate.from('postgresql').class
}

}
30 changes: 30 additions & 0 deletions src/test/groovy/io/seqera/migtool/template/SqlTemplateTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.seqera.migtool.template

import spock.lang.Specification

/**
*
* @author Paolo Di Tommaso <[email protected]>
*/
class SqlTemplateTest extends Specification{

def 'should validate default template' () {
given:
def t = SqlTemplate.from('anything')

expect:
t.selectMaxRank('FOO') == 'select max(`rank`) from FOO'
t.insetMigration('FOO') == 'insert into FOO (`rank`,`script`,`checksum`,`created_on`,`execution_time`) values (?,?,?,?,?)'
t.selectMigration('FOO') == 'select `id`, `checksum`, `script` from FOO where `rank` = ? and `script` = ?'
}

def 'should validate postgre template' () {
given:
def t = SqlTemplate.from('postgresql')

expect:
t.selectMaxRank('FOO') == 'select max(rank) from FOO'
t.insetMigration('FOO') == 'insert into FOO (rank,script,checksum,created_on,execution_time) values (?,?,?,?,?)'
t.selectMigration('FOO') == 'select id, checksum, script from FOO where rank = ? and script = ?'
}
}

0 comments on commit c79052f

Please sign in to comment.