Moved DbMigrationTest/DbMigrationGenerateTest to ebean-test

This commit is contained in:
Roland Praml committed 2022-02-03 07:59:09 +01:00
1 parent dac3fbc120
commit 3fc11a6434
15 files changed
+218 -152

No files matched your search

@@ -0,0 +1,88 @@
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");
migration.setGeneratePendingDrop("1.1");
assertThat(migration.generateMigration()).isEqualTo("1.2__dropsFor_1.1");
assertThatThrownBy(()->migration.generateMigration())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("No 'pendingDrops'"); // subsequent call
server.shutdown();
logger.info("end");
}
}
@@ -0,0 +1,84 @@
package io.ebeaninternal.dbmigration;
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.nio.file.Paths;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 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-test/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());
DatabaseConfig config = new DatabaseConfig();
config.setName("migrationtest");
config.loadFromProperties();
config.setRegister(false);
config.setDefaultServer(false);
config.getProperties().put("ebean.migrationtest.migration.pathToResources", pathToResources);
config.setPackages(Arrays.asList("misc.migration.v1_0"));
// First, we clean up the output-directory
Files.walk(Paths.get(pathToResources, "migrationtest"))
.filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete);
DatabaseFactory.create(config).shutdown();
// then we generate migration scripts for v1_0
assertThat(DbMigrationPlugin.getLastMigration()).isEqualTo("1.0__initial");
assertThat(DbMigrationPlugin.getLastInit()).isEqualTo("1.0__initial");
// and we check repeatative calls
DatabaseFactory.create(config).shutdown();
assertThat(DbMigrationPlugin.getLastMigration()).isNull();
assertThat(DbMigrationPlugin.getLastInit()).isNull();
// and now for v1_1
config.setPackages(Arrays.asList("misc.migration.v1_1"));
DatabaseFactory.create(config).shutdown();
assertThat(DbMigrationPlugin.getLastMigration()).isEqualTo("1.1,1.2__dropsFor_1.1");
assertThat(DbMigrationPlugin.getLastInit()).isEqualTo("1.2");
// subsequent call
DatabaseFactory.create(config).shutdown();
assertThat(DbMigrationPlugin.getLastMigration()).isNull();
assertThat(DbMigrationPlugin.getLastInit()).isNull();
// and now for v1_2
config.setPackages(Arrays.asList("misc.migration.v1_2"));
DatabaseFactory.create(config).shutdown();
assertThat(DbMigrationPlugin.getLastMigration()).isEqualTo("1.3,1.4__dropsFor_1.3");
assertThat(DbMigrationPlugin.getLastInit()).isEqualTo("1.4");
logger.info("end");
}
}
@@ -0,0 +1,207 @@
package io.ebeaninternal.dbmigration;
import io.ebean.BaseTestCase;
import io.ebean.SqlRow;
import io.ebean.SqlUpdate;
import io.ebean.annotation.IgnorePlatform;
import io.ebean.annotation.Platform;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class DbMigrationTest extends BaseTestCase {
private void runScript(String scriptName) throws IOException {
URL url = getClass().getResource("/migrationtest/dbmigration/" + server().platform().name().toLowerCase() + "/" + scriptName);
assert url != null : scriptName + " not found";
server().script().run(url);
}
@Test
public void lastVersion() {
File d = new File("src/test/resources/migrationtest/dbmigration/h2");
assertThat(LastMigration.lastVersion(d, null)).isEqualTo("1.4");
assertThat(LastMigration.nextVersion(d, null, false)).isEqualTo("1.5");
assertThat(LastMigration.nextVersion(d, null, true)).isEqualTo("1.4");
}
@Test
public void lastVersion_no_v_Prefix() {
File d = new File("src/test/resources/migrationtest-history/dbmigration");
assertThat(LastMigration.lastVersion(d, null)).isEqualTo("1.2");
}
@IgnorePlatform({Platform.ORACLE, Platform.NUODB})
// Note: This test is currently only executed against H2.
// it should be moved to ebean-tests, so that it takes advantage of the docker tests
@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_m_phone_numbers",
"migtest_mtm_c_migtest_mtm_m",
"migtest_mtm_m_migtest_mtm_c",
"migtest_oto_child",
"migtest_oto_master");
if (isSqlServer()) { // || isMySql()
runScript("I__create_procs.sql");
}
runScript("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("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("1.2__dropsFor_1.1.sql");
// Oracle caches the statement and does not detect schema change. It fails with
// an ORA-01007
if (isOracle() || isDb2()) {
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("1.3.sql");
runScript("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");
}
server().script().runScript("cleanup", sb.toString(), true);
server().script().runScript("cleanup", sb.toString(), true);
}
}
@@ -0,0 +1,68 @@
package io.ebeaninternal.dbmigration;
import io.ebean.DatabaseFactory;
import io.ebean.config.DatabaseConfig;
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.nio.file.Paths;
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 {
@Test
public void testMysqlStoredProcedures() throws Exception {
String pathToResources = "src/test/resources";
DatabaseConfig config = new DatabaseConfig();
config.setName("migrationtest");
config.loadFromProperties();
config.setRegister(false);
config.setDefaultServer(false);
config.getPlatformConfig().setUseMigrationStoredProcedures(true);
config.setRunMigration(false);
config.getProperties().put("ebean.migrationtest.migration.pathToResources", pathToResources);
config.getProperties().put("ebean.migrationtest.migration.platforms", "mysql");
config.getProperties().put("ebean.migrationtest.migration.migrationPath", "migrationtest-procedures/dbmigration");
config.getProperties().put("ebean.migrationtest.migration.migrationInitPath", "migrationtest-procedures/dbinit");
config.setPackages(Arrays.asList("misc.migration.mysql_v1_0"));
// First, we clean up the output-directory
Path path = Paths.get(pathToResources , "migrationtest-procedures");
Files.walk(path)
.filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete);
DatabaseFactory.create(config).shutdown();
// then we generate migration scripts for v1_0
assertThat(DbMigrationPlugin.getLastMigration()).isEqualTo("1.0__initial");
assertThat(DbMigrationPlugin.getLastInit()).isEqualTo("1.0__initial");
// and now for v1_1
config.setPackages(Arrays.asList("misc.migration.mysql_v1_1"));
DatabaseFactory.create(config).shutdown();
assertThat(DbMigrationPlugin.getLastMigration()).isEqualTo("1.1,1.2__dropsFor_1.1");
assertThat(DbMigrationPlugin.getLastInit()).isEqualTo("1.2");
final Path sqlFile = path.resolve("dbmigration/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;
}
}
@@ -51,10 +51,11 @@ public class EBasic {
@Size(max=127)
String name;
@DbMigration(preAlter = { "-- db2 does not support parial null indices :( - so we have to clean",
"update ${table} set status = 'N' where id = 1" }, platforms = Platform.DB2)
@DbMigration(preAlter = "-- rename all collisions")
@Column(unique = true)
@Size(max=127)
@Size(max = 127)
String description;
//@NotNull
@@ -1,5 +1,6 @@
package misc.migration.v1_1;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@@ -18,6 +19,9 @@ public class MtmMaster {
@ManyToMany
List<MtmChild> children;
@ElementCollection
List<String> phoneNumbers;
public Long getId() {
return id;
}
+17 -4
View File
@@ -195,18 +195,30 @@ datasource.hana.username=EBEAN_TEST
datasource.hana.password=Eb3an_test
datasource.hana.url=jdbc:sap://hxehost:39013/?databaseName=HXE
#datasource.hana.driver=com.sap.db.jdbc.Driver
#
# parameters for migration test
datasource.migrationtest.username=SA
datasource.migrationtest.password=SA
datasource.migrationtest.url=jdbc:h2:mem:migration
datasource.migrationtest.url=jdbc:h2:mem:migration;NON_KEYWORDS=KEY,VALUE
ebean.migrationtest.applyPrefix=V
ebean.migrationtest.ddl.generate=false
ebean.migrationtest.ddl.run=false
ebean.migrationtest.ddl.header=-- Migrationscripts for ebean unittest
ebean.migrationtest.migration.appName=migrationtest
ebean.migrationtest.migration.migrationPath=dbmigration/migrationtest
ebean.migrationtest.migration.migrationPath=migrationtest/dbmigration
ebean.migrationtest.migration.migrationInitPath=migrationtest/dbinit
ebean.migrationtest.migration.strict=true
ebean.migrationtest.migration.generate=true
ebean.migrationtest.migration.run=true
ebean.migrationtest.migration.includeIndex=true
ebean.migrationtest.migration.generateInit=true
ebean.migrationtest.migration.generatePendingDrop=auto
ebean.migrationtest.migration.platforms=db2luw=db2,h2,hsqldb,mysql,mysql55=mysql55,postgres,oracle,sqlite,sqlserver17=sqlserver17,hana
# need to generate unique statements to prevent them from being filtered out as duplicates by the DdlRunner
ebean.hana.generateUniqueDdl=true
# parameters for migration test
datasource.migrationtest-history.username=SA
@@ -217,5 +229,6 @@ ebean.migrationtest-history.ddl.generate=false
ebean.migrationtest-history.ddl.run=false
ebean.migrationtest-history.ddl.header=-- Migrationscripts for ebean unittest DbMigrationDropHistoryTest
ebean.migrationtest-history.migration.appName=migrationtest-history
ebean.migrationtest-history.migration.migrationPath=dbmigration/migrationtest-history
ebean.migrationtest-history.migration.strict=true
ebean.migrationtest-history.migration.migrationPath=migrationtest-history/dbmigration
ebean.migrationtest-history.migration.migrationInitPath=migrationtest-history/dbinit
ebean.migrationtest-history.migration.strict=true