Moved DbMigrationtests test to "ebean-tests"

This commit is contained in:
Roland Praml
2022-02-07 11:55:41 +01:00
parent 05ad6da7e6
commit a93e2e05c5
210 changed files with 8956 additions and 2590 deletions
@@ -0,0 +1,90 @@
package io.ebeaninternal.dbmigration;
import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.config.DatabaseConfig;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* This is the Migrationscript generator. It generates 3 migrationscript for the models
* @author Roland Praml, FOCONIS AG
*
*/
public class DbMigrationDropHistoryTest {
private static final Logger logger = LoggerFactory.getLogger(DbMigrationDropHistoryTest.class);
@Test
public void invokeTest() throws IOException {
main(null);
}
public static void main(String[] args) throws IOException {
logger.info("start");
DefaultDbMigration migration = new DefaultDbMigration();
// We use src/test/resources as output directory (so we see in GIT if files will change)
migration.setPathToResources("src/test/resources");
DatabaseConfig config = new DatabaseConfig();
config.setName("migrationtest-history");
config.loadFromProperties();
config.setRegister(false);
config.setDefaultServer(false);
config.setPackages(Arrays.asList("misc.migration.history.v1_0"));
Database server = DatabaseFactory.create(config);
migration.setServer(server);
// First, we clean up the output-directory
assertThat(migration.migrationDirectory().getAbsolutePath()).contains("migrationtest-history");
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");
// and we check repeatative calls
assertThat(migration.generateMigration()).isNull();
// and now for v1_1
config.setPackages(Arrays.asList("misc.migration.history.v1_1"));
server.shutdown();
server = DatabaseFactory.create(config);
migration.setServer(server);
assertThat(migration.generateMigration()).isEqualTo("1.1");
assertThat(migration.generateMigration()).isNull(); // subsequent call
List<String> pendingDrops = migration.getPendingDrops();
assertThat(pendingDrops).contains("1.1");
//System.setProperty("ddl.migration.pendingDropsFor", "1.1");
migration.setGeneratePendingDrop("1.1");
assertThat(migration.generateMigration()).isEqualTo("1.2__dropsFor_1.1");
assertThatThrownBy(()->migration.generateMigration())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("No 'pendingDrops'"); // subsequent call
System.clearProperty("ddl.migration.pendingDropsFor");
server.shutdown();
logger.info("end");
}
}
@@ -0,0 +1,126 @@
package io.ebeaninternal.dbmigration;
import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.annotation.Platform;
import io.ebean.config.DatabaseConfig;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* This is the Migrationscript generator. It generates 3 migrationscript for the models
* @author Roland Praml, FOCONIS AG
*
*/
public class DbMigrationGenerateTest {
private static final Logger logger = LoggerFactory.getLogger(DbMigrationGenerateTest.class);
public static void main(String[] args) throws IOException {
run("ebean-ddl-generator/src/test/resources");
}
@Test
public void invokeTest() throws IOException {
run("src/test/resources");
}
public static void run(String pathToResources) throws IOException {
logger.info("start current directory: " + new File(".").getAbsolutePath());
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(pathToResources);
migration.setMigrationPath("db/migration");
migration.setMigrationPath(null); // use the default for this test
// migration.addPlatform(Platform.GENERIC, "generic"); there is no ddl handler for generic
// migration.addPlatform(Platform.SQLANYWHERE, "sqlanywhere"); and sqlanywhere
migration.addPlatform(Platform.DB2);
migration.addPlatform(Platform.H2);
migration.addPlatform(Platform.HSQLDB, "hsqldb");
migration.addPlatform(Platform.MYSQL, "mysql");
migration.addPlatform(Platform.MYSQL55, "mysql55");
migration.addPlatform(Platform.POSTGRES);
migration.addPlatform(Platform.ORACLE);
migration.addPlatform(Platform.SQLITE);
migration.addPlatform(Platform.SQLSERVER17, "sqlserver17");
migration.addPlatform(Platform.HANA);
DatabaseConfig config = new DatabaseConfig();
config.setName("migrationtest");
config.loadFromProperties();
config.setRegister(false);
config.setDefaultServer(false);
config.getProperties().put("ebean.hana.generateUniqueDdl", "true"); // need to generate unique statements to prevent them from being filtered out as duplicates by the DdlRunner
config.setPackages(Arrays.asList("misc.migration.v1_0"));
Database server = DatabaseFactory.create(config);
migration.setServer(server);
// First, we clean up the output-directory
assertThat(migration.migrationDirectory().getAbsolutePath()).contains("migrationtest");
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");
// and we check repeatative calls
assertThat(migration.generateMigration()).isNull();
// and now for v1_1
config.setPackages(Arrays.asList("misc.migration.v1_1"));
server.shutdown();
server = DatabaseFactory.create(config);
migration.setServer(server);
assertThat(migration.generateMigration()).isEqualTo("1.1");
assertThat(migration.generateMigration()).isNull(); // subsequent call
System.setProperty("ddl.migration.pendingDropsFor", "1.1");
assertThat(migration.generateMigration()).isEqualTo("1.2__dropsFor_1.1");
assertThatThrownBy(()->migration.generateMigration())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("No 'pendingDrops'"); // subsequent call
System.clearProperty("ddl.migration.pendingDropsFor");
assertThat(migration.generateMigration()).isNull(); // subsequent call
// and now for v1_2 with
config.setPackages(Arrays.asList("misc.migration.v1_2"));
server.shutdown();
server = DatabaseFactory.create(config);
migration.setServer(server);
assertThat(migration.generateMigration()).isEqualTo("1.3");
assertThat(migration.generateMigration()).isNull(); // subsequent call
System.setProperty("ddl.migration.pendingDropsFor", "1.3");
assertThat(migration.generateMigration()).isEqualTo("1.4__dropsFor_1.3");
assertThatThrownBy(migration::generateMigration)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("No 'pendingDrops'"); // subsequent call
System.clearProperty("ddl.migration.pendingDropsFor");
assertThat(migration.generateMigration()).isNull(); // subsequent call
server.shutdown();
logger.info("end");
}
}
@@ -0,0 +1,191 @@
package io.ebeaninternal.dbmigration;
import io.localtest.BaseTestCase;
import io.ebean.SqlRow;
import io.ebean.SqlUpdate;
import io.ebean.annotation.IgnorePlatform;
import io.ebean.annotation.Platform;
import io.ebeaninternal.dbmigration.ddlgeneration.Helper;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class DbMigrationTest extends BaseTestCase {
private void runScript(boolean expectErrors, String scriptName) throws IOException {
String ddl = Helper.asText(this, "/dbmigration/migrationtest/" + server().platform().name().toLowerCase() + "/" + scriptName);
runScript(expectErrors, ddl, scriptName);
}
private void runScript(boolean useAutoCommit, String content, String scriptName) {
server().script().runScript(scriptName, content, useAutoCommit);
}
@IgnorePlatform({Platform.ORACLE, Platform.NUODB, Platform.MARIADB})
@Test
public void testRunMigration() throws IOException {
// first clean up previously created objects
cleanup("migtest_ckey_assoc",
"migtest_ckey_detail",
"migtest_ckey_parent",
"migtest_e_basic",
"migtest_e_enum",
"migtest_e_history",
"migtest_e_history2",
"migtest_e_history3",
"migtest_e_history4",
"migtest_e_history5",
"migtest_e_history6",
"migtest_e_ref",
"migtest_e_softdelete",
"migtest_e_user",
"migtest_fk_cascade",
"migtest_fk_cascade_one",
"migtest_fk_none",
"migtest_fk_none_via_join",
"migtest_fk_one",
"migtest_fk_set_null",
"migtest_mtm_c",
"migtest_mtm_m",
"migtest_mtm_c_migtest_mtm_m",
"migtest_mtm_m_migtest_mtm_c",
"migtest_oto_child",
"migtest_oto_master");
if (isSqlServer()) { // || isMySql()
runScript(false, "I__create_procs.sql");
}
runScript(false, "1.0__initial.sql");
if (isOracle() || isHana()) {
SqlUpdate update = server().sqlUpdate("insert into migtest_e_basic (id, old_boolean, user_id) values (1, :false, 1)");
update.setParameter("false", false);
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("insert into migtest_e_basic (id, old_boolean, user_id) values (2, :true, 1)");
update.setParameter("true", true);
assertThat(server().execute(update)).isEqualTo(1);
} else {
SqlUpdate update = server().sqlUpdate("insert into migtest_e_basic (id, old_boolean, user_id) values (1, :false, 1), (2, :true, 1)");
update.setParameter("false", false);
update.setParameter("true", true);
assertThat(server().execute(update)).isEqualTo(2);
}
createHistoryEntities();
// Run migration
runScript(false, "1.1.sql");
List<SqlRow> result = server().sqlQuery("select * from migtest_e_basic order by id").findList();
assertThat(result).hasSize(2);
SqlRow row = result.get(0);
assertThat(row.keySet()).contains("old_boolean", "old_boolean2");
assertThat(row.getInteger("id")).isEqualTo(1);
assertThat(row.getBoolean("old_boolean")).isFalse();
assertThat(row.getBoolean("new_boolean_field")).isFalse(); // test if update old_boolean -> new_boolean_field works well
assertThat(row.getString("new_string_field")).isEqualTo("foo'bar");
assertThat(row.getBoolean("new_boolean_field2")).isTrue();
//assertThat(row.getTimestamp("some_date")).isCloseTo(new Date(), 86_000); // allow 1 minute delta
row = result.get(1);
assertThat(row.getInteger("id")).isEqualTo(2);
assertThat(row.getBoolean("old_boolean")).isTrue();
assertThat(row.getBoolean("new_boolean_field")).isTrue(); // test if update old_boolean -> new_boolean_field works well
assertThat(row.getString("new_string_field")).isEqualTo("foo'bar");
assertThat(row.getBoolean("new_boolean_field2")).isTrue();
//assertThat(row.getTimestamp("some_date")).isCloseTo(new Date(), 60_000); // allow 1 minute delta
runScript(false, "1.2__dropsFor_1.1.sql");
// Oracle caches the statement and does not detect schema change. It fails with
// an ORA-01007
if (isOracle()) {
result = server().sqlQuery("select * from migtest_e_basic order by id,id").findList();
} else {
result = server().sqlQuery("select * from migtest_e_basic order by id").findList();
}
assertThat(result).hasSize(2);
row = result.get(0);
assertThat(row.keySet()).doesNotContain("old_boolean", "old_boolean2");
runScript(false, "1.3.sql");
runScript(false, "1.4__dropsFor_1.3.sql");
// now DB structure shoud be the same as v1_0
result = server().sqlQuery("select * from migtest_e_basic order by id").findList();
assertThat(result).hasSize(2);
row = result.get(0);
assertThat(row.keySet()).contains("old_boolean", "old_boolean2");
}
/**
*
*/
private void createHistoryEntities() {
SqlUpdate update = server().sqlUpdate("insert into migtest_e_history (id, test_string) values (1, '42')");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("update migtest_e_history set test_string = '45' where id = 1");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("insert into migtest_e_history2 (id, test_string, obsolete_string1, obsolete_string2) values (1, 'foo', 'bar', null)");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("update migtest_e_history2 set test_string = 'baz' where id = 1");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("insert into migtest_e_history3 (id, test_string) values (1, '42')");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("update migtest_e_history3 set test_string = '45' where id = 1");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("insert into migtest_e_history4 (id, test_number) values (1, 42)");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("update migtest_e_history4 set test_number = 45 where id = 1");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("insert into migtest_e_history5 (id, test_number) values (1, 42)");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("update migtest_e_history5 set test_number = 45 where id = 1");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("insert into migtest_e_history6 (id, test_number1, test_number2) values (1, 2, 7)");
assertThat(server().execute(update)).isEqualTo(1);
update = server().sqlUpdate("update migtest_e_history6 set test_number2 = 45 where id = 1");
assertThat(server().execute(update)).isEqualTo(1);
}
private void cleanup(String ... tables) {
final boolean sqlServer = isSqlServer();
final boolean postgres = isPostgres();
StringBuilder sb = new StringBuilder();
for (String table : tables) {
// simple and stupid try to execute all commands on all dialects.
if (sqlServer) {
sb.append("alter table ").append(table).append(" set ( system_versioning = OFF );\n");
sb.append("alter table ").append(table).append(" drop system versioning;\n");
}
if (postgres) {
sb.append("drop table ").append(table).append(" cascade;\n");
sb.append("drop table ").append(table).append("_history cascade;\n");
} else {
sb.append("drop table ").append(table).append(";\n");
sb.append("drop table ").append(table).append("_history;\n");
}
sb.append("drop view ").append(table).append("_with_history;\n");
sb.append("drop sequence ").append(table).append("_seq;\n");
}
runScript(true, sb.toString(), "cleanup");
runScript(true, sb.toString(), "cleanup");
}
}
@@ -0,0 +1,85 @@
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.AfterEach;
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&ouml;hler, FOCONIS AG
*/
public class MysqlGenerateMigrationTest {
@AfterEach
public void resetPendingDropsProperty() {
System.clearProperty("ddl.migration.pendingDropsFor");
}
@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;
}
}