diff --git a/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java b/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java index e0d3b319f..c00b4c77f 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java +++ b/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java @@ -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; } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/runner/Checksum.java b/src/main/java/com/avaje/ebean/dbmigration/runner/Checksum.java new file mode 100644 index 000000000..515bf3606 --- /dev/null +++ b/src/main/java/com/avaje/ebean/dbmigration/runner/Checksum.java @@ -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(); + } +} diff --git a/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationMetaRow.java b/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationMetaRow.java new file mode 100644 index 000000000..5a066ac87 --- /dev/null +++ b/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationMetaRow.java @@ -0,0 +1,27 @@ +package com.avaje.ebean.dbmigration.runner; + +import java.sql.Timestamp; + +/** + * + */ +public class MigrationMetaRow implements Comparable { + + 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 (?,?,?,?,?,?,?,?,?)"; +} diff --git a/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationTable.java b/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationTable.java index 73fb01754..c5d15a6ad 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationTable.java @@ -42,7 +42,7 @@ public class MigrationTable { private final ServerConfig serverConfig; private final String envUserName; - int metaRowPosition; + int currentSearchPosition; List 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(); } diff --git a/src/test/java/com/avaje/ebean/dbmigration/runner/ChecksumTest.java b/src/test/java/com/avaje/ebean/dbmigration/runner/ChecksumTest.java new file mode 100644 index 000000000..b268c4444 --- /dev/null +++ b/src/test/java/com/avaje/ebean/dbmigration/runner/ChecksumTest.java @@ -0,0 +1,18 @@ +package com.avaje.ebean.dbmigration.runner; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ChecksumTest { + + @Test + public void test_calculate() throws Exception { + + int checkFoo = Checksum.calculate("foo"); + + assertThat(Checksum.calculate("foo")).isEqualTo(checkFoo); + assertThat(Checksum.calculate("Foo")).isNotEqualTo(checkFoo); + } + +} \ No newline at end of file