Initial Commit

This commit is contained in:
Isaac Parenteau
2019-07-20 12:39:03 -05:00
commit 79529ecc40
66 changed files with 6549 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package net.locusworks.common.migration;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseMigrationManager {
protected List<MigrationItem> migrations = new ArrayList<>();
public abstract void migrate() throws Exception;
}

View File

@@ -0,0 +1,6 @@
package net.locusworks.common.migration;
@FunctionalInterface
public interface MigrationCallback {
void results(String msg);
}

View File

@@ -0,0 +1,74 @@
package net.locusworks.common.migration;
import java.util.Arrays;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.internal.info.MigrationInfoDumper;
public class MigrationItem {
protected Flyway flyway = null;
protected MigrationInfo[] pendingMigrations;
protected MigrationInfo[] allMigrations;
private MigrationCallback callback;
public MigrationItem(Flyway flyway, MigrationCallback callback) {
this.allMigrations = flyway.info().all();
this.pendingMigrations = flyway.info().pending();
this.flyway=flyway;
this.callback = callback;
}
public MigrationItem(Flyway flyway) {
this(flyway, null);
}
public String[] getSchemas() {
return flyway.getSchemas();
}
public int qtyPending() {
return pendingMigrations.length;
}
public void repair() {
// no harm in calling migrate even if none pending, log will contain
// assurance that the migrations were verified
try {
String schemas = Arrays.toString(getSchemas()).replace("[", "").replace("]", "");
String status = String.format("Repair status for %s:%n%s", schemas, getAllMigrationsLog());
callback(status);
flyway.repair();
} catch (Exception e) {
String message = String.format("%nDatabase migration error:%n %s %nPortal webapp cannot continue.", e.getMessage());
throw new RuntimeException(message, e);
}
}
public void migrate() {
// no harm in calling migrate even if none pending, log will contain
// assurance that the migrations were verified
try {
String schemas = Arrays.toString(getSchemas()).replace("[", "").replace("]", "");
String status = String.format("Repair status for %s:%n%s", schemas, getAllMigrationsLog());
callback(status);
flyway.migrate();
} catch (Exception e) {
String message = String.format("%nDatabase migration error:%n %s %nPortal webapp cannot continue.", e.getMessage());
throw new RuntimeException(message, e);
}
}
public String getAllMigrationsLog() {
return MigrationInfoDumper.dumpToAsciiTable(allMigrations);
}
private void callback(String msg) {
if (this.callback != null) this.callback.results(msg);
}
}