Skip to content

Commit

Permalink
Implement a simple sql template to support Postgre
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 7, 2024
1 parent c131b38 commit 008f534
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/seqera/migtool/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.Callable;

import io.seqera.migtool.builder.SqlTemplate;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -51,7 +52,8 @@ public Integer call() {
.withDialect(dialect!=null ? dialect : dialectFromUrl(url))
.withDriver(driver!=null ? driver : driverFromUrl(url))
.withLocations(location)
.withPattern(pattern);
.withPattern(pattern)
.withBuilder(SqlTemplate.from(dialect!=null ? dialect : dialectFromUrl(url)));

try {
tool.run();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/seqera/migtool/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ static public String driverFromUrl(String url) {
return "org.h2.Driver";
if( "sqlite".equals(dialect))
return "org.sqlite.JDBC";
if( "postgresql".equals(dialect))
return "org.postgresql.Driver";
return null;
}
}
16 changes: 12 additions & 4 deletions src/main/java/io/seqera/migtool/MigTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -42,7 +44,7 @@ public class MigTool {

static final String MIGTOOL_TABLE = "MIGTOOL_HISTORY";

static final String[] DIALECTS = {"h2", "mysql", "mariadb","sqlite"};
static final String[] DIALECTS = {"h2", "mysql", "mariadb","sqlite","postgresql"};

String driver;
String url;
Expand All @@ -54,6 +56,7 @@ public class MigTool {
Pattern pattern;
String schema;
String catalog;
SqlTemplate builder = new DefaultSqlTemplate();

private final List<MigRecord> migrationEntries;
private final List<MigRecord> patchEntries;
Expand Down Expand Up @@ -106,6 +109,11 @@ 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 @@ -350,7 +358,7 @@ protected void apply() {

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

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

protected boolean checkMigrated(MigRecord entry) {
String sql = "select `id`, `checksum`, `script` from " + MIGTOOL_TABLE + " where `rank` = ? and `script` = ?";
final String sql = builder.selectMigration(MIGTOOL_TABLE);

try (Connection conn=getConnection(); PreparedStatement stm = conn.prepareStatement(sql)) {
stm.setInt(1, entry.rank);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/io/seqera/migtool/builder/DefaultSqlTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.seqera.migtool.builder;

/**
*
* @author Paolo Di Tommaso <[email protected]>
*/
public class DefaultSqlTemplate extends SqlTemplate {
@Override
public String selectMaxRank(String table) {
return "select max(`rank`) from " + table;
}

@Override
public String insetMigration(String table) {
return "insert into "+table+" (`rank`,`script`,`checksum`,`created_on`,`execution_time`) values (?,?,?,?,?)";
}

@Override
public String selectMigration(String table) {
return "select `id`, `checksum`, `script` from "+table+ " where `rank` = ? and `script` = ?";
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/seqera/migtool/builder/PostgresSqlTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.seqera.migtool.builder;

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

@Override
public String selectMaxRank(String table) {
return "select max(rank) from " + table;
}

@Override
public String insetMigration(String table) {
return "insert into "+table+" (rank,script,checksum,created_on,execution_time) values (?,?,?,?,?)";
}

@Override
public String selectMigration(String table) {
return "select id, checksum, script from "+table+ " where rank = ? and script = ?";
}

}
22 changes: 22 additions & 0 deletions src/main/java/io/seqera/migtool/builder/SqlTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.seqera.migtool.builder;

/**
*
* @author Paolo Di Tommaso <[email protected]>
*/
public abstract class SqlTemplate {

abstract public String selectMaxRank(String table);

abstract public String insetMigration(String table);

abstract public String selectMigration(String table);

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

}

0 comments on commit 008f534

Please sign in to comment.