diff --git a/src/main/java/io/ebean/config/PlatformConfig.java b/src/main/java/io/ebean/config/PlatformConfig.java index d06868d1b..f891b3d47 100644 --- a/src/main/java/io/ebean/config/PlatformConfig.java +++ b/src/main/java/io/ebean/config/PlatformConfig.java @@ -54,6 +54,8 @@ public class PlatformConfig { private boolean caseSensitiveCollation = true; + private boolean useMigrationStoredProcedures; + /** * Modify the default mapping of standard types such as default precision for DECIMAL etc. */ @@ -76,6 +78,7 @@ public class PlatformConfig { this.idType = platformConfig.idType; this.geometrySRID = platformConfig.geometrySRID; this.dbUuid = platformConfig.dbUuid; + this.useMigrationStoredProcedures = platformConfig.useMigrationStoredProcedures; this.caseSensitiveCollation = platformConfig.caseSensitiveCollation; this.allQuotedIdentifiers = platformConfig.allQuotedIdentifiers; this.databaseInetAddressVarchar = platformConfig.databaseInetAddressVarchar; @@ -110,6 +113,20 @@ public class PlatformConfig { this.caseSensitiveCollation = caseSensitiveCollation; } + /** + * Return true if force use of helper stored procedures for migrations. + */ + public boolean isUseMigrationStoredProcedures() { + return useMigrationStoredProcedures; + } + + /** + * Set true to force use of helper stored procedures for migrations. + */ + public void setUseMigrationStoredProcedures(boolean useMigrationStoredProcedures) { + this.useMigrationStoredProcedures = useMigrationStoredProcedures; + } + /** * Return a value used to represent TRUE in the database. *

@@ -276,6 +293,7 @@ public class PlatformConfig { databaseBooleanFalse = p.get("databaseBooleanFalse", databaseBooleanFalse); databaseInetAddressVarchar = p.getBoolean("databaseInetAddressVarchar", databaseInetAddressVarchar); caseSensitiveCollation = p.getBoolean("caseSensitiveCollation", caseSensitiveCollation); + useMigrationStoredProcedures = p.getBoolean("useMigrationStoredProcedures", useMigrationStoredProcedures);; DbUuid dbUuid = p.getEnum(DbUuid.class, "dbuuid", null); if (dbUuid != null) { diff --git a/src/main/java/io/ebean/config/dbplatform/DatabasePlatform.java b/src/main/java/io/ebean/config/dbplatform/DatabasePlatform.java index 43396fab9..c3d699f7a 100644 --- a/src/main/java/io/ebean/config/dbplatform/DatabasePlatform.java +++ b/src/main/java/io/ebean/config/dbplatform/DatabasePlatform.java @@ -53,6 +53,8 @@ public class DatabasePlatform { protected boolean supportsSavepointId = true; + protected boolean useMigrationStoredProcedures = true; + /** * The behaviour used when ending a read only transaction at read committed isolation level. */ @@ -233,6 +235,9 @@ public class DatabasePlatform { public void configure(PlatformConfig config) { this.sequenceBatchSize = config.getDatabaseSequenceBatchSize(); this.caseSensitiveCollation = config.isCaseSensitiveCollation(); + if (config.isUseMigrationStoredProcedures()) { + useMigrationStoredProcedures = true; + } configureIdType(config.getIdType()); configure(config, config.isAllQuotedIdentifiers()); } @@ -339,6 +344,13 @@ public class DatabasePlatform { return supportsSavepointId; } + /** + * Return true if migrations should use stored procedures. + */ + public boolean isUseMigrationStoredProcedures() { + return useMigrationStoredProcedures; + } + /** * Return the maximum table name length. *

diff --git a/src/main/java/io/ebean/config/dbplatform/mysql/MySqlPlatform.java b/src/main/java/io/ebean/config/dbplatform/mysql/MySqlPlatform.java index 83d509da4..761c9425a 100644 --- a/src/main/java/io/ebean/config/dbplatform/mysql/MySqlPlatform.java +++ b/src/main/java/io/ebean/config/dbplatform/mysql/MySqlPlatform.java @@ -28,6 +28,7 @@ public class MySqlPlatform extends DatabasePlatform { this.useExtraTransactionOnIterateSecondaryQueries = true; this.selectCountWithAlias = true; this.supportsSavepointId = false; + this.useMigrationStoredProcedures = false; this.dbEncrypt = new MySqlDbEncrypt(); this.historySupport = new MySqlHistorySupport(); this.columnAliasPrefix = null; diff --git a/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java b/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java index 45282cd2d..01001d9d0 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java +++ b/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java @@ -47,6 +47,7 @@ public class DdlGenerator { private final boolean ddlAutoCommit; private final String dbSchema; private final ScriptTransform scriptTransform; + private final boolean useMigrationStoredProcedures; private CurrentModel currentModel; private String dropAllContent; @@ -64,9 +65,11 @@ public class DdlGenerator { log.warn("DDL can't be run on startup with TenantMode " + serverConfig.getTenantMode()); this.runDdl = false; this.ddlAutoCommit = false; + this.useMigrationStoredProcedures = false; } else { this.runDdl = serverConfig.isDdlRun(); this.ddlAutoCommit = server.getDatabasePlatform().isDdlAutoCommit(); + this.useMigrationStoredProcedures = server.getDatabasePlatform().isUseMigrationStoredProcedures(); } this.scriptTransform = createScriptTransform(serverConfig.getMigrationConfig()); this.baseDir = initBaseDir(); @@ -179,7 +182,7 @@ public class DdlGenerator { protected void runDropSql(Connection connection) throws IOException { if (!createOnly) { - if (extraDdl && jaxbPresent) { + if (extraDdl && jaxbPresent && useMigrationStoredProcedures) { String extraApply = ExtraDdlXmlReader.buildExtra(server.getDatabasePlatform().getName(), true); if (extraApply != null) { runScript(connection, false, extraApply, "extra-ddl"); @@ -199,7 +202,7 @@ public class DdlGenerator { } runScript(connection, false, createAllContent, getCreateFileName()); - if (extraDdl && jaxbPresent) { + if (extraDdl && jaxbPresent && useMigrationStoredProcedures) { if (currentModel().isTablePartitioning()) { String extraPartitioning = ExtraDdlXmlReader.buildPartitioning(server.getDatabasePlatform().getName()); if (extraPartitioning != null && !extraPartitioning.isEmpty()) { diff --git a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java index b4793a172..afb88c712 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java +++ b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java @@ -18,12 +18,15 @@ public class MySqlDdl extends PlatformDdl { // this flag is for compatibility. Use it with care. private static final boolean USE_CHECK_CONSTRAINT = Boolean.getBoolean("ebean.mysql.useCheckConstraint"); + private final boolean useMigrationStoredProcedures; + public MySqlDdl(DatabasePlatform platform) { super(platform); this.alterColumn = "modify"; this.dropUniqueConstraint = "drop index"; this.historyDdl = new MySqlHistoryDdl(); this.inlineComments = true; + this.useMigrationStoredProcedures = platform.isUseMigrationStoredProcedures(); } /** @@ -49,7 +52,12 @@ public class MySqlDdl extends PlatformDdl { @Override public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { - buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement(); + if (useMigrationStoredProcedures) { + buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement(); + } else { + buffer.append("alter table ").append(tableName).append(" ").append(dropColumn).append(" ").append(columnName) + .append(dropColumnSuffix).endOfStatement(); + } } @Override @@ -60,6 +68,7 @@ public class MySqlDdl extends PlatformDdl { return null; } } + @Override public String alterTableAddCheckConstraint(String tableName, String checkConstraintName, String checkConstraint) { if (USE_CHECK_CONSTRAINT) { @@ -96,17 +105,13 @@ public class MySqlDdl extends PlatformDdl { @Override public String alterColumnNotnull(String tableName, String columnName, boolean notnull) { - // can't alter itself - done in alterColumnBaseAttributes() return null; } @Override public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) { - String suffix = DdlHelp.isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + convertDefaultValue(defaultValue); - - // use alter return "alter table " + tableName + " alter " + columnName + " " + suffix; } @@ -124,7 +129,6 @@ public class MySqlDdl extends PlatformDdl { boolean notnull = (alter.isNotnull() != null) ? alter.isNotnull() : Boolean.TRUE.equals(alter.isCurrentNotnull()); String notnullClause = notnull ? " not null" : ""; - // use modify return "alter table " + tableName + " modify " + columnName + " " + type + notnullClause; } @@ -161,7 +165,7 @@ public class MySqlDdl extends PlatformDdl { } @Override - public void addColumnComment(DdlBuffer apply, String table, String column, String comment) throws IOException { + public void addColumnComment(DdlBuffer apply, String table, String column, String comment) { // alter comment currently not supported as it requires to repeat whole column definition } @@ -182,7 +186,6 @@ public class MySqlDdl extends PlatformDdl { i++; } buffer.endOfStatement(); - } } diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql index a22adf90b..b3317f701 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql @@ -3,17 +3,17 @@ drop view if exists migtest_e_history2_with_history; -- apply changes -CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean'); +alter table migtest_e_basic drop column old_boolean; -CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean2'); +alter table migtest_e_basic drop column old_boolean2; -CALL usp_ebean_drop_column('migtest_e_basic', 'eref_id'); +alter table migtest_e_basic drop column eref_id; -CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string1'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string1'); +alter table migtest_e_history2 drop column obsolete_string1; +alter table migtest_e_history2_history drop column obsolete_string1; -CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string2'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string2'); +alter table migtest_e_history2 drop column obsolete_string2; +alter table migtest_e_history2_history drop column obsolete_string2; drop table if exists migtest_e_ref; create view migtest_e_history2_with_history as select * from migtest_e_history2 union all select * from migtest_e_history2_history; diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql index efa0bbc8a..ff8fa50d0 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql @@ -3,45 +3,45 @@ drop trigger migtest_e_history_history_upd; drop trigger migtest_e_history_history_del; drop view migtest_e_history_with_history; -CALL usp_ebean_drop_column('migtest_e_history', 'sys_period_start'); -CALL usp_ebean_drop_column('migtest_e_history', 'sys_period_end'); +alter table migtest_e_history drop column sys_period_start; +alter table migtest_e_history drop column sys_period_end; drop table migtest_e_history_history; drop view if exists migtest_e_history2_with_history; drop view if exists migtest_e_history5_with_history; -- apply changes -CALL usp_ebean_drop_column('migtest_ckey_detail', 'one_key'); +alter table migtest_ckey_detail drop column one_key; -CALL usp_ebean_drop_column('migtest_ckey_detail', 'two_key'); +alter table migtest_ckey_detail drop column two_key; -CALL usp_ebean_drop_column('migtest_ckey_parent', 'assoc_id'); +alter table migtest_ckey_parent drop column assoc_id; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_string_field'); +alter table migtest_e_basic drop column new_string_field; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field'); +alter table migtest_e_basic drop column new_boolean_field; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field2'); +alter table migtest_e_basic drop column new_boolean_field2; -CALL usp_ebean_drop_column('migtest_e_basic', 'progress'); +alter table migtest_e_basic drop column progress; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_integer'); +alter table migtest_e_basic drop column new_integer; -CALL usp_ebean_drop_column('migtest_e_history2', 'test_string2'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string2'); +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; -CALL usp_ebean_drop_column('migtest_e_history2', 'test_string3'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string3'); +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; -CALL usp_ebean_drop_column('migtest_e_history2', 'new_column'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'new_column'); +alter table migtest_e_history2 drop column new_column; +alter table migtest_e_history2_history drop column new_column; -CALL usp_ebean_drop_column('migtest_e_history5', 'test_boolean'); -CALL usp_ebean_drop_column('migtest_e_history5_history', 'test_boolean'); +alter table migtest_e_history5 drop column test_boolean; +alter table migtest_e_history5_history drop column test_boolean; -CALL usp_ebean_drop_column('migtest_e_softdelete', 'deleted'); +alter table migtest_e_softdelete drop column deleted; -CALL usp_ebean_drop_column('migtest_oto_child', 'master_id'); +alter table migtest_oto_child drop column master_id; drop table if exists migtest_e_user; drop table if exists migtest_mtm_c_migtest_mtm_m; diff --git a/src/test/resources/dbmigration/migrationtest/mysql55/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/mysql55/1.2__dropsFor_1.1.sql index a22adf90b..b3317f701 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql55/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql55/1.2__dropsFor_1.1.sql @@ -3,17 +3,17 @@ drop view if exists migtest_e_history2_with_history; -- apply changes -CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean'); +alter table migtest_e_basic drop column old_boolean; -CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean2'); +alter table migtest_e_basic drop column old_boolean2; -CALL usp_ebean_drop_column('migtest_e_basic', 'eref_id'); +alter table migtest_e_basic drop column eref_id; -CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string1'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string1'); +alter table migtest_e_history2 drop column obsolete_string1; +alter table migtest_e_history2_history drop column obsolete_string1; -CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string2'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string2'); +alter table migtest_e_history2 drop column obsolete_string2; +alter table migtest_e_history2_history drop column obsolete_string2; drop table if exists migtest_e_ref; create view migtest_e_history2_with_history as select * from migtest_e_history2 union all select * from migtest_e_history2_history; diff --git a/src/test/resources/dbmigration/migrationtest/mysql55/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/mysql55/1.4__dropsFor_1.3.sql index efa0bbc8a..ff8fa50d0 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql55/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql55/1.4__dropsFor_1.3.sql @@ -3,45 +3,45 @@ drop trigger migtest_e_history_history_upd; drop trigger migtest_e_history_history_del; drop view migtest_e_history_with_history; -CALL usp_ebean_drop_column('migtest_e_history', 'sys_period_start'); -CALL usp_ebean_drop_column('migtest_e_history', 'sys_period_end'); +alter table migtest_e_history drop column sys_period_start; +alter table migtest_e_history drop column sys_period_end; drop table migtest_e_history_history; drop view if exists migtest_e_history2_with_history; drop view if exists migtest_e_history5_with_history; -- apply changes -CALL usp_ebean_drop_column('migtest_ckey_detail', 'one_key'); +alter table migtest_ckey_detail drop column one_key; -CALL usp_ebean_drop_column('migtest_ckey_detail', 'two_key'); +alter table migtest_ckey_detail drop column two_key; -CALL usp_ebean_drop_column('migtest_ckey_parent', 'assoc_id'); +alter table migtest_ckey_parent drop column assoc_id; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_string_field'); +alter table migtest_e_basic drop column new_string_field; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field'); +alter table migtest_e_basic drop column new_boolean_field; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field2'); +alter table migtest_e_basic drop column new_boolean_field2; -CALL usp_ebean_drop_column('migtest_e_basic', 'progress'); +alter table migtest_e_basic drop column progress; -CALL usp_ebean_drop_column('migtest_e_basic', 'new_integer'); +alter table migtest_e_basic drop column new_integer; -CALL usp_ebean_drop_column('migtest_e_history2', 'test_string2'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string2'); +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; -CALL usp_ebean_drop_column('migtest_e_history2', 'test_string3'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string3'); +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; -CALL usp_ebean_drop_column('migtest_e_history2', 'new_column'); -CALL usp_ebean_drop_column('migtest_e_history2_history', 'new_column'); +alter table migtest_e_history2 drop column new_column; +alter table migtest_e_history2_history drop column new_column; -CALL usp_ebean_drop_column('migtest_e_history5', 'test_boolean'); -CALL usp_ebean_drop_column('migtest_e_history5_history', 'test_boolean'); +alter table migtest_e_history5 drop column test_boolean; +alter table migtest_e_history5_history drop column test_boolean; -CALL usp_ebean_drop_column('migtest_e_softdelete', 'deleted'); +alter table migtest_e_softdelete drop column deleted; -CALL usp_ebean_drop_column('migtest_oto_child', 'master_id'); +alter table migtest_oto_child drop column master_id; drop table if exists migtest_e_user; drop table if exists migtest_mtm_c_migtest_mtm_m;