mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Update DB Migration runner
This commit is contained in:
@@ -46,10 +46,14 @@ public class MigrationRunner {
|
||||
MigrationTable table = new MigrationTable(server, migrationConfig, connection);
|
||||
table.createIfNeeded();
|
||||
|
||||
LocalMigrationResource priorVersion = null;
|
||||
List<LocalMigrationResource> localVersions = resources.getVersions();
|
||||
for (int i = 0; i < localVersions.size(); i++) {
|
||||
LocalMigrationResource localVersion = localVersions.get(i);
|
||||
if (!table.shouldRun(i, localVersion)) {
|
||||
if (i > 0) {
|
||||
priorVersion = localVersions.get(i-1);
|
||||
}
|
||||
if (!table.shouldRun(i, localVersion, priorVersion)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.avaje.ebean.dbmigration.model.MigrationVersion;
|
||||
import org.avaje.classpath.scanner.Resource;
|
||||
|
||||
/**
|
||||
*
|
||||
* A DB migration resource.
|
||||
*/
|
||||
public class LocalMigrationResource implements Comparable<LocalMigrationResource> {
|
||||
|
||||
@@ -14,22 +14,42 @@ public class LocalMigrationResource implements Comparable<LocalMigrationResource
|
||||
|
||||
private final Resource resource;
|
||||
|
||||
public LocalMigrationResource(MigrationVersion v0, String location, Resource resource) {
|
||||
this.version = v0;
|
||||
public LocalMigrationResource(MigrationVersion version, String location, Resource resource) {
|
||||
this.version = version;
|
||||
this.location = location;
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public MigrationVersion getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
public String toString() {
|
||||
return version.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default ordering by version.
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(LocalMigrationResource o) {
|
||||
return version.compareTo(o.version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the underlying migration version.
|
||||
*/
|
||||
public MigrationVersion getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the resource location.
|
||||
*/
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the content for the migration apply ddl script.
|
||||
*/
|
||||
public String getContent() {
|
||||
return resource.loadAsString("UTF-8");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class LocalMigrationResources {
|
||||
ClassLoader classLoader = serverConfig.getClassLoadConfig().getClassLoader();
|
||||
|
||||
Scanner scanner = new Scanner(classLoader);
|
||||
List<Resource> resourceList = scanner.scanForResources(new Location(migrationPath), new Match(migrationConfig));
|
||||
List<Resource> resourceList = scanner.scanForResources(migrationPath, new Match(migrationConfig));
|
||||
|
||||
logger.debug("resources: {}", resourceList);
|
||||
|
||||
@@ -49,8 +49,8 @@ public class LocalMigrationResources {
|
||||
int pos = filename.lastIndexOf(migrationConfig.getApplySuffix());
|
||||
String mainName = filename.substring(0, pos);
|
||||
|
||||
MigrationVersion v0 = MigrationVersion.parse(mainName);
|
||||
LocalMigrationResource res = new LocalMigrationResource(v0, resource.getLocation(), resource);
|
||||
MigrationVersion migrationVersion = MigrationVersion.parse(mainName);
|
||||
LocalMigrationResource res = new LocalMigrationResource(migrationVersion, resource.getLocation(), resource);
|
||||
versions.add(res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ public class MigrationTable {
|
||||
private final String schema;
|
||||
private final String table;
|
||||
private final ServerConfig serverConfig;
|
||||
private final String envUserName;
|
||||
|
||||
int metaRowPosition;
|
||||
|
||||
@@ -56,6 +57,8 @@ public class MigrationTable {
|
||||
this.catalog = null;
|
||||
this.schema = null;
|
||||
this.table = "ebean_migration";
|
||||
|
||||
this.envUserName = System.getProperty("user.name");
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +96,15 @@ public class MigrationTable {
|
||||
}
|
||||
|
||||
|
||||
public boolean shouldRun(int runPosition, LocalMigrationResource localVersion) {
|
||||
public boolean shouldRun(int runPosition, 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");
|
||||
@@ -112,6 +123,12 @@ public class MigrationTable {
|
||||
|
||||
logger.debug("run migration "+localVersion.getLocation());
|
||||
|
||||
|
||||
String script = localVersion.getContent();
|
||||
|
||||
MigrationScriptRunner run = new MigrationScriptRunner(connection);
|
||||
run.runScript(false, script, "run migration version: "+localVersion.getVersion());
|
||||
|
||||
String sql = "insert into ebean_migration (id,status,run_version,dep_version,comment,checksum,run_on,run_by,run_ip) "+
|
||||
"values (?,?,?,?,?,?,?,?,?)";
|
||||
|
||||
@@ -123,7 +140,7 @@ public class MigrationTable {
|
||||
sqlUpdate.setParameter(5, "comm");
|
||||
sqlUpdate.setParameter(6, 0);
|
||||
sqlUpdate.setParameter(7, new Timestamp(System.currentTimeMillis()));
|
||||
sqlUpdate.setParameter(8, "user");
|
||||
sqlUpdate.setParameter(8, envUserName);
|
||||
sqlUpdate.setParameter(9, "userIp");
|
||||
|
||||
ExternalJdbcTransaction t = new ExternalJdbcTransaction(connection);
|
||||
|
||||
Reference in New Issue
Block a user