-
-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reintroduce stored procedures to drop columns on MySql #2403
Changes from 2 commits
091b5fb
3fe521c
b60f920
c67e66c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ public class DdlGenerator implements SpiDdlGenerator { | |
private final ScriptTransform scriptTransform; | ||
private final Platform platform; | ||
private final String platformName; | ||
private final boolean useMigrationStoredProcedures; | ||
|
||
private CurrentModel currentModel; | ||
private String dropAllContent; | ||
|
@@ -71,9 +72,11 @@ public DdlGenerator(SpiEbeanServer server) { | |
log.warn("DDL can't be run on startup with TenantMode " + config.getTenantMode()); | ||
this.runDdl = false; | ||
this.ddlAutoCommit = false; | ||
this.useMigrationStoredProcedures = false; | ||
} else { | ||
this.runDdl = config.isDdlRun(); | ||
this.ddlAutoCommit = databasePlatform.isDdlAutoCommit(); | ||
this.useMigrationStoredProcedures = config.getDatabasePlatform().isUseMigrationStoredProcedures(); | ||
} | ||
this.scriptTransform = createScriptTransform(config); | ||
this.baseDir = initBaseDir(); | ||
|
@@ -187,7 +190,7 @@ private DdlRunner createDdlRunner(boolean expectErrors, String scriptName) { | |
|
||
protected void runDropSql(Connection connection) throws IOException { | ||
if (!createOnly) { | ||
if (extraDdl && jaxbPresent) { | ||
if (extraDdl && jaxbPresent && useMigrationStoredProcedures) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not look correct - useMigrationStoredProcedures must NOT be here. |
||
String extraApply = ExtraDdlXmlReader.buildExtra(platform, true); | ||
if (extraApply != null) { | ||
runScript(connection, false, extraApply, "extra-ddl"); | ||
|
@@ -210,7 +213,7 @@ protected void runCreateSql(Connection connection) throws IOException { | |
if (extraDdl && jaxbPresent) { | ||
if (currentModel().isTablePartitioning()) { | ||
String extraPartitioning = ExtraDdlXmlReader.buildPartitioning(platform); | ||
if (extraPartitioning != null && !extraPartitioning.isEmpty()) { | ||
if (extraPartitioning != null && !extraPartitioning.isEmpty() && useMigrationStoredProcedures) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not look correct for Postgres at least - useMigrationStoredProcedures check must NOT be here. |
||
runScript(connection, false, extraPartitioning, "builtin-partitioning-ddl"); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,19 +392,25 @@ private void configurePlatforms() { | |
private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform, boolean tablePartitioning) throws IOException { | ||
if (dbPlatform != null) { | ||
if (tablePartitioning && includeBuiltInPartitioning) { | ||
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltinTablePartitioning()); | ||
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltinTablePartitioning(), true); | ||
} | ||
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin()); | ||
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.read()); | ||
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin(), true); | ||
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.read(), false); | ||
} | ||
} | ||
|
||
private void generateExtraDdlFor(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl) throws IOException { | ||
private void generateExtraDdlFor(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl, boolean isBuiltin) throws IOException { | ||
if (extraDdl != null) { | ||
List<DdlScript> ddlScript = extraDdl.getDdlScript(); | ||
for (DdlScript script : ddlScript) { | ||
if (!script.isDrop() && matchPlatform(dbPlatform.getPlatform(), script.getPlatforms())) { | ||
writeExtraDdl(migrationDir, script); | ||
if (script.isInit()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth refactoring this if/else |
||
if (!isBuiltin || dbPlatform.isUseMigrationStoredProcedures()) { | ||
writeExtraDdl(migrationDir, script); | ||
} | ||
} else { | ||
writeExtraDdl(migrationDir, script); | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.ebeaninternal.dbmigration; | ||
|
||
import io.ebean.Database; | ||
import io.ebean.DatabaseFactory; | ||
import io.ebean.annotation.Platform; | ||
import io.ebean.config.DatabaseConfig; | ||
import io.ebean.config.PlatformConfig; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Class to test the alternative drop behaviour using stored procedures for MySql databases . | ||
* | ||
* @author Jonas Pöhler, FOCONIS AG | ||
*/ | ||
public class MysqlGenerateMigrationTest { | ||
|
||
@Test | ||
public void testMysqlStoredProcedures() throws Exception { | ||
DefaultDbMigration migration = new DefaultDbMigration(); | ||
migration.setIncludeIndex(true); | ||
// We use src/test/resources as output directory (so we see in GIT if files will change) | ||
migration.setPathToResources("src/test/resources"); | ||
|
||
migration.addPlatform(Platform.MYSQL, "mysql"); | ||
|
||
final PlatformConfig platformConfig = new PlatformConfig(); | ||
platformConfig.setUseMigrationStoredProcedures(true); | ||
|
||
DatabaseConfig config = new DatabaseConfig(); | ||
config.setName("migrationtest"); | ||
config.loadFromProperties(); | ||
config.setPlatformConfig(platformConfig); | ||
config.setRegister(false); | ||
config.setDefaultServer(false); | ||
config.getProperties().put("ebean.migration.migrationPath", "db/migration/mysql"); | ||
|
||
config.setPackages(Arrays.asList("misc.migration.mysql_v1_0")); | ||
Database server = DatabaseFactory.create(config); | ||
migration.setServer(server); | ||
migration.setMigrationPath("mysql/procedures"); | ||
|
||
// First, we clean up the output-directory | ||
assertThat(migration.migrationDirectory().getAbsolutePath()).contains("procedures"); | ||
Files.walk(migration.migrationDirectory().toPath()) | ||
.filter(Files::isRegularFile) | ||
.map(Path::toFile).forEach(File::delete); | ||
|
||
// then we generate migration scripts for v1_0 | ||
assertThat(migration.generateMigration()).isEqualTo("1.0__initial"); | ||
|
||
config.setPackages(Arrays.asList("misc.migration.mysql_v1_1")); | ||
server.shutdown(); | ||
server = DatabaseFactory.create(config); | ||
migration.setServer(server); | ||
migration.setMigrationPath("mysql/procedures"); | ||
assertThat(migration.generateMigration()).isEqualTo("1.1"); | ||
|
||
System.setProperty("ddl.migration.pendingDropsFor", "1.1"); | ||
assertThat(migration.generateMigration()).isEqualTo("1.2__dropsFor_1.1"); | ||
|
||
final Path sqlFile = migration.migrationDirectory().toPath() | ||
.resolve("mysql/1.2__dropsFor_1.1.sql"); | ||
|
||
assertThat(sqlFile).isNotEmptyFile(); | ||
assertThat(Files.readAllLines(sqlFile, StandardCharsets.UTF_8)) | ||
.contains("CALL usp_ebean_drop_column('migtest_e_basic', 'status2');") | ||
.contains("CALL usp_ebean_drop_column('migtest_e_basic', 'description');"); | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config.getDatabasePlatform() -> databasePlatform