WIP update for migration runner

This commit is contained in:
Robin Bygrave
2016-02-10 20:51:55 +13:00
parent 12e6c0b139
commit 84fa373728
5 changed files with 104 additions and 13 deletions
@@ -53,7 +53,7 @@ public class MigrationRunner {
if (i > 0) {
priorVersion = localVersions.get(i-1);
}
if (!table.shouldRun(i, localVersion, priorVersion)) {
if (!table.shouldRun(localVersion, priorVersion)) {
break;
}
}
@@ -0,0 +1,32 @@
package com.avaje.ebean.dbmigration.runner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.zip.CRC32;
/**
* Calculates the checksum for the given string content.
*/
public class Checksum {
/**
* Returns the checksum of this string.
*/
public static int calculate(String str) {
final CRC32 crc32 = new CRC32();
BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
try {
String line;
while ((line = bufferedReader.readLine()) != null) {
crc32.update(line.getBytes("UTF-8"));
}
} catch (IOException e) {
throw new RuntimeException("Failed to calculate checksum", e);
}
return (int) crc32.getValue();
}
}
@@ -0,0 +1,27 @@
package com.avaje.ebean.dbmigration.runner;
import java.sql.Timestamp;
/**
*
*/
public class MigrationMetaRow implements Comparable<MigrationMetaRow> {
int id;
String status;
String runVersion;
String depVersion;
String comment;
int checksum;
Timestamp runOn;
String runBy;
@Override
public int compareTo(MigrationMetaRow o) {
return (id < o.id) ? -1 : ((id == o.id) ? 0 : 1);
}
//String sql = "insert into ebean_migration (id,status,run_version,dep_version,comment,checksum,run_on,run_by,run_ip) "+
// "values (?,?,?,?,?,?,?,?,?)";
}
@@ -42,7 +42,7 @@ public class MigrationTable {
private final ServerConfig serverConfig;
private final String envUserName;
int metaRowPosition;
int currentSearchPosition;
List<SqlRow> metaRows;
@@ -96,25 +96,39 @@ public class MigrationTable {
}
public boolean shouldRun(int runPosition, LocalMigrationResource localVersion, LocalMigrationResource priorVersion) {
public boolean shouldRun(LocalMigrationResource localVersion, LocalMigrationResource priorVersion) {
// if prior != null check previous version installed
// if previous version not installed ... error - missing version (prior version)
// if localVersion installed
// check checksum and return ok, or re-installable?
// else
// install version and continue
if (runPosition >= metaRows.size()) {
logger.debug("No matching row");
runMigration(runPosition, localVersion);
return true;
}
// if (!priorVersionNotInstalled(priorVersion)) {
//
// }
//
// // if localVersion installed
// // check checksum and return ok, or re-installable?
// // else
// // install version and continue
//
// if (runPosition >= metaRows.size()) {
// logger.debug("No matching row");
// runMigration(runPosition, localVersion);
// return true;
// }
return false;
}
private void checkInstalled(LocalMigrationResource priorVersion) {
if (priorVersion != null) {
searchFor(priorVersion);
}
}
private void searchFor(LocalMigrationResource priorVersion) {
}
public void commit() throws SQLException {
connection.commit();
}