Transform extra-ddl.xml files when running in ddl mode (no migration) (#1531)

This commit is contained in:
Manuel Danisch
2018-11-13 20:20:01 +13:00
committed by Rob Bygrave
parent acd6ba0a42
commit 2806972032
3 changed files with 108 additions and 1 deletions
@@ -1,7 +1,9 @@
package io.ebeaninternal.dbmigration;
import io.ebean.config.DbMigrationConfig;
import io.ebean.config.ServerConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.migration.MigrationConfig;
import io.ebean.migration.ddl.DdlRunner;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.api.SpiEbeanServer;
@@ -23,6 +25,7 @@ import java.io.LineNumberReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
/**
* Controls the generation and execution of "Create All" and "Drop All" DDL scripts.
@@ -42,6 +45,7 @@ public class DdlGenerator {
private final boolean jaxbPresent;
private final boolean ddlCommitOnCreateIndex;
private final String dbSchema;
private final ScriptTransform scriptTransform;
private CurrentModel currentModel;
private String dropAllContent;
@@ -61,6 +65,7 @@ public class DdlGenerator {
this.runDdl = serverConfig.isDdlRun();
this.ddlCommitOnCreateIndex = server.getDatabasePlatform().isDdlCommitOnCreateIndex();
}
this.scriptTransform = createScriptTransform(serverConfig.getMigrationConfig());
}
/**
@@ -146,7 +151,7 @@ public class DdlGenerator {
} else if (ddlCommitOnCreateIndex) {
runner.setCommitOnCreateIndex();
}
int count = runner.runAll(content, connection);
int count = runner.runAll(scriptTransform.transform(content), connection);
if (expectErrors) {
connection.setAutoCommit(false);
}
@@ -348,4 +353,13 @@ public class DdlGenerator {
}
}
/**
* Create the ScriptTransform for placeholder key/value replacement.
*/
private ScriptTransform createScriptTransform(DbMigrationConfig config) {
Map<String, String> map = PlaceholderBuilder.build(config.getRunPlaceholders(), config.getRunPlaceholderMap());
return new ScriptTransform(map);
}
}
@@ -0,0 +1,53 @@
package io.ebeaninternal.dbmigration;
import java.util.HashMap;
import java.util.Map;
/**
* Joins placeholder map and comma/equals delimited string.
*/
class PlaceholderBuilder {
private final Map<String,String> map = new HashMap<>();
/**
* Create with raw comma and equals delimited pairs plus map of key value pairs.
*/
static Map<String,String> build(String commaDelimited, Map<String,String> placeholders) {
PlaceholderBuilder builder = new PlaceholderBuilder();
builder.add(commaDelimited);
builder.add(placeholders);
return builder.map;
}
private PlaceholderBuilder() {
}
/**
* Add a comma and equals delimited string to parse for key value pairs.
*/
private void add(String commaDelimited) {
if (commaDelimited != null) {
String[] split = commaDelimited.split("[,;]");
for (String keyValue : split) {
String[] pair = keyValue.split("=");
if (pair.length == 2) {
map.put(pair[0].trim(), pair[1].trim());
}
}
}
}
/**
* Add a map of key value placeholder pairs.
*/
private void add(Map<String,String> placeholders) {
if (placeholders != null) {
map.putAll(placeholders);
}
}
}
@@ -0,0 +1,40 @@
package io.ebeaninternal.dbmigration;
import java.util.HashMap;
import java.util.Map;
/**
* Transforms a SQL script given a map of key/value substitutions.
*/
class ScriptTransform {
/**
* Transform just ${table} with the table name.
*/
static String replace(String key, String value, String script) {
return script.replace(key, value);
}
private final Map<String,String> placeholders = new HashMap<>();
ScriptTransform(Map<String,String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
placeholders.put(wrapKey(entry.getKey()), entry.getValue());
}
}
private String wrapKey(String key) {
return "${"+key+"}";
}
/**
* Transform the script replacing placeholders in the form <code>${key}</code> with <code>value</code>.
*/
String transform(String source) {
for (Map.Entry<String, String> entry : placeholders.entrySet()) {
source = source.replace(entry.getKey(), entry.getValue());
}
return source;
}
}