From 07fe1cde6bc7cef7c5abe6b1c4456fbdb513ec7b Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Mon, 2 May 2016 16:25:27 +1200 Subject: [PATCH] #689 - Refactor DB Migration to support "V" prefix --- .../avaje/ebean/config/DbMigrationConfig.java | 22 ++++++++++- .../avaje/ebean/dbmigration/DbMigration.java | 2 +- .../ebean/dbmigration/MigrationRunner.java | 1 + .../dbmigration/runner/MigrationMetaRow.java | 32 ++++------------ .../dbmigration/runner/MigrationTable.java | 37 +++++++------------ .../default-create-table.sql | 4 +- 6 files changed, 45 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java b/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java index 9ebb4337f..ffa07076a 100644 --- a/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java +++ b/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java @@ -60,6 +60,11 @@ public class DbMigrationConfig { protected String applySuffix = ".sql"; + /** + * Set this to "V" to be compatible with FlywayDB. + */ + protected String applyPrefix = ""; + protected String modelSuffix = ".model.xml"; protected boolean includeGeneratedFileComment; @@ -177,6 +182,20 @@ public class DbMigrationConfig { this.applySuffix = applySuffix; } + /** + * Return the apply prefix. + */ + public String getApplyPrefix() { + return applyPrefix; + } + + /** + * Set the apply prefix. This might be set to "V" for use with FlywayDB. + */ + public void setApplyPrefix(String applyPrefix) { + this.applyPrefix = applyPrefix; + } + /** * Return true if the generated file comment should be included. */ @@ -351,6 +370,7 @@ public class DbMigrationConfig { } else { modelPath = properties.get("migration.modelPath", modelPath); } + applyPrefix = properties.get("migration.applyPrefix", applyPrefix); applySuffix = properties.get("migration.applySuffix", applySuffix); modelSuffix = properties.get("migration.modelSuffix", modelSuffix); includeGeneratedFileComment = properties.getBoolean("migration.includeGeneratedFileComment", includeGeneratedFileComment); @@ -360,7 +380,7 @@ public class DbMigrationConfig { generate = properties.getBoolean("migration.generate", generate); version = properties.get("migration.version", version); - this.name = properties.get("migration.name", this.name); + name = properties.get("migration.name", name); runMigration = properties.getBoolean("migration.run", runMigration); metaTable = properties.get("migration.metaTable", metaTable); diff --git a/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java b/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java index 59297de2c..af24792c2 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java +++ b/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java @@ -398,7 +398,7 @@ public class DbMigration { version = migrationModel.getNextVersion(initialVersion); } - String fullVersion = version; + String fullVersion = migrationConfig.getApplyPrefix() + version; if (migrationConfig.getName() != null) { fullVersion += "__" + toUnderScore(migrationConfig.getName()); diff --git a/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java b/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java index f06a1bf1c..a4a217dee 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java +++ b/src/main/java/com/avaje/ebean/dbmigration/MigrationRunner.java @@ -104,6 +104,7 @@ public class MigrationRunner { break; } priorVersion = localVersion; + connection.commit(); } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationMetaRow.java b/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationMetaRow.java index b34fc75d8..6d629c887 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationMetaRow.java +++ b/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationMetaRow.java @@ -24,10 +24,12 @@ class MigrationMetaRow { private String runBy; + private long runTime; + /** * Construct for inserting into table. */ - MigrationMetaRow(int id, String type, String version, String comment, int checksum, String runBy, Timestamp runOn) { + MigrationMetaRow(int id, String type, String version, String comment, int checksum, String runBy, Timestamp runOn, long runTime) { this.id = id; this.type = type; this.version = version; @@ -35,6 +37,7 @@ class MigrationMetaRow { this.comment = comment; this.runBy = runBy; this.runOn = runOn; + this.runTime = runTime; } /** @@ -48,6 +51,7 @@ class MigrationMetaRow { checksum = row.getInteger("mchecksum"); runOn = row.getTimestamp("run_on"); runBy = row.getString("run_by"); + runTime = row.getLong("run_time"); } public String toString() { @@ -87,23 +91,7 @@ class MigrationMetaRow { insert.setParameter(6, checksum); insert.setParameter(7, runOn); insert.setParameter(8, runBy); - insert.setParameter(9, "ip"); - } - - /** - * Bind to an update statement. - */ - public void bindUpdate(int checksum, String runBy, Timestamp runOn, SqlUpdate update) { - - this.checksum = checksum; - this.runOn = runOn; - this.runBy = runBy; - - update.setParameter(1, checksum); - update.setParameter(2, runOn); - update.setParameter(3, runBy); - update.setParameter(4, "ip"); - update.setParameter(5, id); + insert.setParameter(9, runTime); } /** @@ -111,14 +99,8 @@ class MigrationMetaRow { */ static String insertSql(String table) { return "insert into " + table - + " (id, mtype, mstatus, mversion, mcomment, mchecksum, run_on, run_by, run_ip)" + + " (id, mtype, mstatus, mversion, mcomment, mchecksum, run_on, run_by, run_time)" + " values (?,?,?,?,?,?,?,?,?)"; } - static String updateSql(String table) { - return "update " + table - + " set mchecksum = ?, run_on = ?, run_by = ?, run_ip = ?" - + " where id = ?"; - } - } 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 068fbec91..74e0fb21f 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/runner/MigrationTable.java @@ -42,14 +42,12 @@ public class MigrationTable { private final ServerConfig serverConfig; private final String envUserName; - private final Timestamp runTime = new Timestamp(System.currentTimeMillis()); + private final Timestamp runOn = new Timestamp(System.currentTimeMillis()); private final ScriptTransform scriptTransform; private final String insertSql; - private final String updateSql; - private final LinkedHashMap migrations; private MigrationMetaRow lastMigration; @@ -65,13 +63,10 @@ public class MigrationTable { SpiServer pluginApi = server.getPluginApi(); this.serverConfig = pluginApi.getServerConfig(); this.databasePlatform = pluginApi.getDatabasePlatform(); - this.catalog = null; this.schema = null; this.table = migrationConfig.getMetaTable(); this.insertSql = MigrationMetaRow.insertSql(table); - this.updateSql = MigrationMetaRow.updateSql(table); - this.scriptTransform = createScriptTransform(migrationConfig); this.envUserName = System.getProperty("user.name"); } @@ -195,41 +190,35 @@ public class MigrationTable { } } - runMigration(local, existing, script, checksum); + runMigration(local, script, checksum); return true; } /** * Run a migration script as new migration or update on existing repeatable migration. */ - private void runMigration(LocalMigrationResource local, MigrationMetaRow existing, String script, int checksum) throws SQLException { + private void runMigration(LocalMigrationResource local, String script, int checksum) throws SQLException { logger.debug("run migration {}", local.getLocation()); + long start = System.currentTimeMillis(); MigrationScriptRunner run = new MigrationScriptRunner(connection); run.runScript(false, script, "run migration version: " + local.getVersion()); - if (existing != null) { - // update existing migration row - SqlUpdate update = server.createSqlUpdate(updateSql); - existing.bindUpdate(checksum, envUserName, runTime, update); - server.execute(update, new ExternalJdbcTransaction(connection)); + long exeMillis = System.currentTimeMillis() - start; + // insert new migration row + SqlUpdate insert = server.createSqlUpdate(insertSql); + MigrationMetaRow metaRow = createMetaRow(local, checksum, exeMillis); + metaRow.bindInsert(insert); + server.execute(insert, new ExternalJdbcTransaction(connection)); - } else { - // insert new migration row - SqlUpdate insert = server.createSqlUpdate(insertSql); - MigrationMetaRow metaRow = createMetaRow(local, checksum); - metaRow.bindInsert(insert); - server.execute(insert, new ExternalJdbcTransaction(connection)); - - addMigration(local.key(), metaRow); - } + addMigration(local.key(), metaRow); } /** * Create the MigrationMetaRow for this migration. */ - private MigrationMetaRow createMetaRow(LocalMigrationResource migration, int checksum) { + private MigrationMetaRow createMetaRow(LocalMigrationResource migration, int checksum, long exeMillis) { int nextId = 1; if (lastMigration != null) { @@ -240,7 +229,7 @@ public class MigrationTable { String runVersion = migration.key(); String comment = migration.getComment(); - return new MigrationMetaRow(nextId, type, runVersion, comment, checksum, envUserName, runTime); + return new MigrationMetaRow(nextId, type, runVersion, comment, checksum, envUserName, runOn, exeMillis); } /** diff --git a/src/main/resources/migration-support/default-create-table.sql b/src/main/resources/migration-support/default-create-table.sql index 9075c0df3..ee399473a 100644 --- a/src/main/resources/migration-support/default-create-table.sql +++ b/src/main/resources/migration-support/default-create-table.sql @@ -3,11 +3,11 @@ create table ${table} ( mtype varchar(1) not null, mstatus varchar(10) not null, mversion varchar(150) not null, - mcomment varchar(150), + mcomment varchar(150) not null, mchecksum integer not null, run_on timestamp not null, run_by varchar(30) not null, - run_ip varchar(30), + run_time integer not null, constraint pk_${table} primary key (id) );