mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Regenerated scripts & tests
This commit is contained in:
1 parent
a93e2e05c5
commit
cb89b903fd
101 files changed
+315
-5981
No files matched your search
+10
-1
@@ -11,6 +11,7 @@ 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 java.util.List;
|
||||
|
||||
@@ -32,10 +33,18 @@ public class DbMigrationDropHistoryTest {
|
||||
main(null);
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
logger.info("start");
|
||||
|
||||
// First, we clean up the output-directory
|
||||
Files.walk(Paths.get("src/test/resources/migrationtest-history"))
|
||||
.filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete);
|
||||
DefaultDbMigration migration = new DefaultDbMigration();
|
||||
|
||||
// We use src/test/resources as output directory (so we see in GIT if files will change)
|
||||
|
||||
+29
-19
@@ -12,6 +12,7 @@ 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;
|
||||
@@ -28,7 +29,7 @@ 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");
|
||||
run("ebean-test/src/test/resources");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -39,25 +40,40 @@ public class DbMigrationGenerateTest {
|
||||
public static void run(String pathToResources) throws IOException {
|
||||
logger.info("start current directory: " + new File(".").getAbsolutePath());
|
||||
|
||||
|
||||
// First, we clean up the output-directory
|
||||
Files.walk(Paths.get(pathToResources, "migrationtest"))
|
||||
.filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete);
|
||||
|
||||
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.CLICKHOUSE);
|
||||
migration.addPlatform(Platform.COCKROACH);
|
||||
migration.addPlatform(Platform.DB2FORI);
|
||||
migration.addPlatform(Platform.DB2LUW);
|
||||
migration.addPlatform(Platform.DB2ZOS);
|
||||
migration.addPlatform(Platform.GENERIC);
|
||||
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);
|
||||
migration.addPlatform(Platform.HSQLDB);
|
||||
migration.addPlatform(Platform.MARIADB);
|
||||
migration.addPlatform(Platform.MARIADB, "mariadb-noprocs");
|
||||
migration.addPlatform(Platform.MYSQL);
|
||||
migration.addPlatform(Platform.MYSQL55);
|
||||
migration.addPlatform(Platform.NUODB);
|
||||
migration.addPlatform(Platform.ORACLE);
|
||||
migration.addPlatform(Platform.ORACLE11);
|
||||
migration.addPlatform(Platform.POSTGRES);
|
||||
migration.addPlatform(Platform.POSTGRES9);
|
||||
migration.addPlatform(Platform.SQLANYWHERE);
|
||||
migration.addPlatform(Platform.SQLITE);
|
||||
migration.addPlatform(Platform.SQLSERVER16);
|
||||
migration.addPlatform(Platform.SQLSERVER17);
|
||||
migration.addPlatform(Platform.YUGABYTE);
|
||||
|
||||
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setName("migrationtest");
|
||||
@@ -66,16 +82,10 @@ public class DbMigrationGenerateTest {
|
||||
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
|
||||
|
||||
@@ -1,30 +1,38 @@
|
||||
package io.ebeaninternal.dbmigration;
|
||||
|
||||
import io.localtest.BaseTestCase;
|
||||
import io.ebean.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.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(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(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");
|
||||
}
|
||||
|
||||
private void runScript(boolean useAutoCommit, String content, String scriptName) {
|
||||
server().script().runScript(scriptName, content, useAutoCommit);
|
||||
}
|
||||
|
||||
@IgnorePlatform({Platform.ORACLE, Platform.NUODB, Platform.MARIADB})
|
||||
@IgnorePlatform({Platform.ORACLE, Platform.NUODB, Platform.POSTGRES, Platform.YUGABYTE})
|
||||
// Note: Postgres locks up on build server
|
||||
// Note: YUGABYTE complains on "alter table migtest_e_basic alter column status set not null;"
|
||||
@Test
|
||||
public void testRunMigration() throws IOException {
|
||||
// first clean up previously created objects
|
||||
@@ -50,16 +58,17 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
"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(false, "I__create_procs.sql");
|
||||
if (isSqlServer() || isMariaDB()) { // || isMySql()
|
||||
runScript("I__create_procs.sql");
|
||||
}
|
||||
|
||||
runScript(false, "1.0__initial.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)");
|
||||
@@ -80,7 +89,7 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
createHistoryEntities();
|
||||
|
||||
// Run migration
|
||||
runScript(false, "1.1.sql");
|
||||
runScript("1.1.sql");
|
||||
List<SqlRow> result = server().sqlQuery("select * from migtest_e_basic order by id").findList();
|
||||
assertThat(result).hasSize(2);
|
||||
|
||||
@@ -104,24 +113,20 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
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");
|
||||
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()) {
|
||||
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();
|
||||
}
|
||||
result = server().sqlQuery("select * from migtest_e_basic order by id,status").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");
|
||||
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();
|
||||
// now DB structure shoud be the same as v1_0 - perform a diffent query.
|
||||
result = server().sqlQuery("select * from migtest_e_basic order by id,name").findList();
|
||||
assertThat(result).hasSize(2);
|
||||
row = result.get(0);
|
||||
assertThat(row.keySet()).contains("old_boolean", "old_boolean2");
|
||||
@@ -184,8 +189,7 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
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");
|
||||
server().script().runScript("cleanup", sb.toString(), true);
|
||||
server().script().runScript("cleanup", sb.toString(), true);
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
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ö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');");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
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,7 +51,8 @@ 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)
|
||||
|
||||
@@ -2,9 +2,11 @@ package misc.migration.v1_1;
|
||||
|
||||
|
||||
import io.ebean.annotation.DbDefault;
|
||||
import io.ebean.annotation.DbMigration;
|
||||
import io.ebean.annotation.History;
|
||||
import io.ebean.annotation.HistoryExclude;
|
||||
import io.ebean.annotation.NotNull;
|
||||
import io.ebean.annotation.Platform;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
@@ -20,6 +22,13 @@ public class EHistory2 {
|
||||
Integer id;
|
||||
|
||||
@NotNull
|
||||
// see: https://mariadb.com/de/resources/blog/use-cases-for-mariadb-data-versioning/
|
||||
@DbMigration(preAlter = {
|
||||
"SET @@system_versioning_alter_history = 1",
|
||||
"update ${table} set ${column} = 'unknown' where ${column} is null"}, platforms = Platform.MARIADB)
|
||||
|
||||
// other platforms
|
||||
@DbMigration(preAlter = "update ${table} set ${column} = 'unknown' where ${column} is null")
|
||||
@DbDefault("unknown")
|
||||
String testString;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user