Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
Added jakarta annotations; added new constructor to be compatible with Java; changed the visibility of a method, to avoid the need of extending the base class just to use it
  • Loading branch information
pgeadas committed Jun 21, 2024
1 parent 8bd78dc commit ba83f79
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.0
1.6.0
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ dependencies {
implementation "org.codehaus.groovy:groovy:3.0.9"
implementation "org.codehaus.groovy:groovy-sql:3.0.9"

// brings some standard annotations to be used
implementation 'jakarta.annotation:jakarta.annotation-api:3.0.0'

// Use the latest Groovy version for Spock testing
testImplementation "org.codehaus.groovy:groovy:3.0.9"
testImplementation "org.codehaus.groovy:groovy-nio:3.0.9"
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/seqera/migtool/Dialect.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.seqera.migtool;

import groovyjarjarantlr4.v4.runtime.misc.Nullable;
import jakarta.annotation.Nullable;
import java.util.List;

public enum Dialect {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/seqera/migtool/Driver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.seqera.migtool;

import groovyjarjarantlr4.v4.runtime.misc.Nullable;
import jakarta.annotation.Nullable;

public enum Driver {
MYSQL("com.mysql.cj.jdbc.Driver"),
Expand Down
47 changes: 22 additions & 25 deletions src/main/java/io/seqera/migtool/MigRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ static Language from(String fileName) {
boolean isPatch;
boolean isOverride;

/**
* This constructor is needed to be called from Java applications
* **/
public MigRecord(List<String> statements) {
this.statements = statements;
}

public MigRecord() {
}

@Override
public int compareTo(MigRecord other) {
return this.rank - other.rank;
Expand All @@ -55,9 +65,13 @@ static MigRecord parseResourcePath(String path, Pattern pattern) {

int p = path.lastIndexOf("/");
String fileName = p==-1 ? path : path.substring(p+1);


return extractRankAndCreateRecord(pattern, fileName, readContent(path));
}

private static MigRecord extractRankAndCreateRecord(Pattern pattern, String fileName, String path) {
Matcher m = pattern.matcher(fileName);
if( !m.matches() ) {
if (!m.matches()) {
return null;
}

Expand All @@ -69,34 +83,17 @@ static MigRecord parseResourcePath(String path, Pattern pattern) {
return null;
}

String content = readContent(path);
int rank = Integer.parseInt(m.group(1));

return createRecord(fileName, rank, content);
return createRecord(fileName, rank, path);
}

static MigRecord parseFilePath(Path path, Pattern pattern) {
if( pattern == null ) pattern = DEFAULT_PATTERN;

String fileName = path.getFileName().toString();

Matcher m = pattern.matcher(fileName);
if( !m.matches() ) {
return null;
}

// We do not allow files with double fix or amend, ex. V01__organisation.fixed.fixed.sql
if (countMatch(fileName, OVERRIDE_PATTERN) > 1) {
return null;
}
if (countMatch(fileName, PATCH_PATTERN) > 1) {
return null;
}

String content = readContent(path);
int rank = Integer.parseInt(m.group(1));

return createRecord(fileName, rank, content);
return extractRankAndCreateRecord(pattern, fileName, readContent(path));
}

private static long countMatch(String name, Pattern pattern) {
Expand Down Expand Up @@ -144,10 +141,10 @@ private static List<String> getSqlStatements(String sql) {
String[] tokens = sql.split(";");
List<String> result = new ArrayList<>(tokens.length);

for( int i=0; i<tokens.length; i++) {
String clean = tokens[i].trim();
if( clean.length()>0 )
result.add( clean + ';' ) ;
for (String token : tokens) {
String clean = token.trim();
if (!clean.isEmpty())
result.add(clean + ';');
}

return result;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/seqera/migtool/MigTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,10 @@ private void runSqlMigration(MigRecord entry) {
}
}

protected void runGroovyMigration(MigRecord entry) {
/**
* Allows to run a single groovy migration. Usually used for testing purposes.
* **/
public void runGroovyMigration(MigRecord entry) {
final long ts = System.currentTimeMillis();

try (Connection conn = getConnection()) {
Expand Down

0 comments on commit ba83f79

Please sign in to comment.