mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Optionally allow use of stored procedures for MySql to drop columns in db migration
This commit is contained in:
1 parent
033c579984
commit
091b5fbf26
17 files changed
+474
-7
No files matched your search
+78
@@ -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');");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package misc.migration.mysql_v1_0;
|
||||
|
||||
import io.ebean.annotation.DbDefault;
|
||||
import io.ebean.annotation.EnumValue;
|
||||
import io.ebean.annotation.NotNull;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Entity
|
||||
@Table(name = "migtest_e_basic")
|
||||
public class EBasic {
|
||||
|
||||
public enum Status {
|
||||
@EnumValue("N")
|
||||
NEW,
|
||||
|
||||
@EnumValue("A")
|
||||
ACTIVE,
|
||||
|
||||
@EnumValue("I")
|
||||
INACTIVE,
|
||||
}
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
Status status;
|
||||
|
||||
@DbDefault("N")
|
||||
@NotNull
|
||||
Status status2;
|
||||
|
||||
@Size(max=127)
|
||||
String name;
|
||||
|
||||
@Size(max=127)
|
||||
String description;
|
||||
|
||||
public EBasic() {
|
||||
|
||||
}
|
||||
|
||||
public EBasic(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Status getStatus2() {
|
||||
return status2;
|
||||
}
|
||||
|
||||
public void setStatus2(final Status status2) {
|
||||
this.status2 = status2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package misc.migration.mysql_v1_1;
|
||||
|
||||
import io.ebean.annotation.EnumValue;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Entity
|
||||
@Table(name = "migtest_e_basic")
|
||||
public class EBasic {
|
||||
|
||||
public enum Status {
|
||||
@EnumValue("N")
|
||||
NEW,
|
||||
|
||||
@EnumValue("A")
|
||||
ACTIVE,
|
||||
|
||||
@EnumValue("I")
|
||||
INACTIVE,
|
||||
}
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
Status status;
|
||||
|
||||
@Size(max=127)
|
||||
String name;
|
||||
|
||||
public EBasic() {
|
||||
|
||||
}
|
||||
|
||||
public EBasic(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user