Last active
March 22, 2020 09:14
-
-
Save flisboac/8fa832dad0c678986d75b79d85875751 to your computer and use it in GitHub Desktop.
Creates a new INSERT line for a Flyway SQL migration. Useful for patching migration information in the schema_version table (I won't judge, don't worry). Parts of this code were extracted from Flyway v6.1.4.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.nio.charset.StandardCharsets; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.Date; | |
import java.util.zip.CRC32; | |
public class CalculateFlywayChecksum { | |
public static void main(String[] args) throws Exception { | |
String tableName = "schema_version"; | |
if (args.length < 1) { | |
System.err.println("Missing next installed_rank ID."); | |
System.exit(1); | |
} | |
if (args.length < 2) { | |
System.err.println("Missing input filename for the migration script."); | |
System.exit(1); | |
} | |
if (args.length > 2) { | |
tableName = args[2]; | |
} | |
long installedRank = Long.parseLong(args[0]); | |
String filename = args[1]; | |
int checksum = calculateChecksum(filename); | |
String description = filename.replaceAll("^.+?__", "").replaceAll(".[sS][qQ][lL]$", "").replace("_", " "); | |
String version = filename.replaceAll("^\\w", "").replaceAll("__.+$", "").replaceAll("_", "."); | |
String type = "SQL"; | |
String installedBy = "admin"; | |
String installedOn = DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss").format(LocalDateTime.now()); | |
String executionTime = "0"; | |
String success = "true"; | |
System.out.println(String.format( | |
"INSERT INTO %s (" + | |
"installed_rank, " + | |
"version, " + | |
"description, " + | |
"type, " + | |
"script, " + | |
"checksum, " + | |
"installed_by, " + | |
"installed_on, " + | |
"execution_time, " + | |
"success" + | |
") values (%d, '%s', '%s', '%s', '%s', %d, '%s', '%s', %s, %s);", | |
tableName, | |
installedRank, | |
version, | |
description, | |
type, | |
filename, | |
checksum, | |
installedBy, | |
installedOn, | |
executionTime, | |
success | |
)); | |
} | |
public static final int calculateChecksum(String filename) throws Exception { | |
final CRC32 crc32 = new CRC32(); | |
try ( | |
FileReader file = new FileReader(filename); | |
BufferedReader reader = new BufferedReader(file, 4096) | |
) { | |
String line = reader.readLine(); | |
if (line != null) { | |
line = filterBomFromString(line); | |
do { | |
//noinspection Since15 | |
crc32.update(trimLineBreak(line).getBytes(StandardCharsets.UTF_8)); | |
} while ((line = reader.readLine()) != null); | |
} | |
} | |
return (int) crc32.getValue(); | |
} | |
public static String trimLineBreak(String str) { | |
if (!hasLength(str)) { | |
return str; | |
} | |
StringBuilder buf = new StringBuilder(str); | |
while (buf.length() > 0 && isLineBreakCharacter(buf.charAt(buf.length() - 1))) { | |
buf.deleteCharAt(buf.length() - 1); | |
} | |
return buf.toString(); | |
} | |
private static boolean isLineBreakCharacter(char ch) { | |
return '\n' == ch || '\r' == ch; | |
} | |
public static boolean hasLength(String str) { | |
return str != null && str.length() > 0; | |
} | |
private static final char BOM = '\ufeff'; | |
public static boolean isBom(char c) { | |
return c == BOM; | |
} | |
public static String filterBomFromString(String s) { | |
if (s.isEmpty()) { | |
return s; | |
} | |
if (isBom(s.charAt(0))) { | |
return s.substring(1); | |
} | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment