mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#2074 - Refactor extract ebean-ddlgen module. In future this module would expected to be a test only dependency
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class H2PlatformTest {
|
||||
|
||||
@Test
|
||||
public void testTypeConversion() {
|
||||
|
||||
PlatformDdl ddl = PlatformDdlBuilder.create(new H2Platform());
|
||||
|
||||
assertThat(ddl.convert("clob")).isEqualTo("clob");
|
||||
assertThat(ddl.convert("json")).isEqualTo("clob");
|
||||
assertThat(ddl.convert("jsonb")).isEqualTo("clob");
|
||||
assertThat(ddl.convert("varchar(20)")).isEqualTo("varchar(20)");
|
||||
assertThat(ddl.convert("decimal(10)")).isEqualTo("decimal(10)");
|
||||
assertThat(ddl.convert("decimal(8,4)")).isEqualTo("decimal(8,4)");
|
||||
assertThat(ddl.convert("decimal")).isEqualTo("decimal(16,3)");
|
||||
assertThat(ddl.convert("boolean")).isEqualTo("boolean");
|
||||
assertThat(ddl.convert("bit")).isEqualTo("bit");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,8 +2,6 @@ package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.hana.HanaPlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -12,37 +10,6 @@ public class HanaPlatformTest {
|
||||
|
||||
HanaPlatform platform = new HanaPlatform();
|
||||
|
||||
@Test
|
||||
public void testTypeConversion() {
|
||||
|
||||
PlatformDdl ddl = PlatformDdlBuilder.create(platform);
|
||||
|
||||
assertThat(ddl.convert("clob")).isEqualTo("nclob");
|
||||
assertThat(ddl.convert("blob")).isEqualTo("blob");
|
||||
assertThat(ddl.convert("json")).isEqualTo("nclob");
|
||||
assertThat(ddl.convert("jsonb")).isEqualTo("nclob");
|
||||
assertThat(ddl.convert("jsonvarchar")).isEqualTo("nvarchar(255)");
|
||||
|
||||
assertThat(ddl.convert("double")).isEqualTo("double");
|
||||
assertThat(ddl.convert("varchar(20)")).isEqualTo("nvarchar(20)");
|
||||
assertThat(ddl.convert("decimal(10)")).isEqualTo("decimal(10)");
|
||||
assertThat(ddl.convert("decimal(8,4)")).isEqualTo("decimal(8,4)");
|
||||
assertThat(ddl.convert("decimal")).isEqualTo("decimal(16,3)");
|
||||
assertThat(ddl.convert("boolean")).isEqualTo("boolean");
|
||||
assertThat(ddl.convert("bit")).isEqualTo("smallint");
|
||||
assertThat(ddl.convert("tinyint")).isEqualTo("smallint");
|
||||
assertThat(ddl.convert("binary")).isEqualTo("varbinary(255)");
|
||||
assertThat(ddl.convert("binary(16)")).isEqualTo("varbinary(16)");
|
||||
|
||||
assertThat(ddl.convert("point")).isEqualTo("st_point");
|
||||
|
||||
assertThat(ddl.convert("multilinestring")).isEqualTo("st_geometry");
|
||||
assertThat(ddl.convert("multipolygon")).isEqualTo("st_geometry");
|
||||
assertThat(ddl.convert("multipoint")).isEqualTo("st_geometry");
|
||||
assertThat(ddl.convert("linestring")).isEqualTo("st_geometry");
|
||||
assertThat(ddl.convert("polygon")).isEqualTo("st_geometry");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uuid_default() {
|
||||
|
||||
|
||||
@@ -2,28 +2,12 @@ package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MySqlPlatformTest {
|
||||
|
||||
MySqlPlatform mySqlPlatform = new MySqlPlatform();
|
||||
|
||||
@Test
|
||||
public void testTypeConversion() {
|
||||
PlatformDdl ddl = PlatformDdlBuilder.create(mySqlPlatform);
|
||||
assertThat(ddl.convert("clob")).isEqualTo("longtext");
|
||||
assertThat(ddl.convert("json")).isEqualTo("json");
|
||||
assertThat(ddl.convert("jsonb")).isEqualTo("json");
|
||||
assertThat(ddl.convert("varchar(20)")).isEqualTo("varchar(20)");
|
||||
assertThat(ddl.convert("boolean")).isEqualTo("tinyint(1)");
|
||||
assertThat(ddl.convert("bit")).isEqualTo("tinyint(1)");
|
||||
assertThat(ddl.convert("decimal")).isEqualTo("decimal(16,3)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uuid_default() {
|
||||
|
||||
|
||||
@@ -2,37 +2,12 @@ package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class OraclePlatformTest {
|
||||
|
||||
OraclePlatform platform = new OraclePlatform();
|
||||
|
||||
@Test
|
||||
public void testTypeConversion() {
|
||||
|
||||
PlatformDdl ddl = PlatformDdlBuilder.create(platform);
|
||||
|
||||
assertThat(ddl.convert("clob")).isEqualTo("clob");
|
||||
assertThat(ddl.convert("blob")).isEqualTo("blob");
|
||||
assertThat(ddl.convert("json")).isEqualTo("clob");
|
||||
assertThat(ddl.convert("jsonb")).isEqualTo("clob");
|
||||
|
||||
assertThat(ddl.convert("double")).isEqualTo("number(19,4)");
|
||||
assertThat(ddl.convert("varchar(20)")).isEqualTo("varchar2(20)");
|
||||
assertThat(ddl.convert("decimal(10)")).isEqualTo("number(10)");
|
||||
assertThat(ddl.convert("decimal(8,4)")).isEqualTo("number(8,4)");
|
||||
assertThat(ddl.convert("decimal")).isEqualTo("number(16,3)");
|
||||
assertThat(ddl.convert("boolean")).isEqualTo("number(1)");
|
||||
assertThat(ddl.convert("bit")).isEqualTo("bit");
|
||||
assertThat(ddl.convert("tinyint")).isEqualTo("number(3)");
|
||||
assertThat(ddl.convert("binary(16)")).isEqualTo("raw(16)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uuid_default() {
|
||||
|
||||
|
||||
@@ -2,37 +2,12 @@ package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PostgresPlatformTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testTypeConversion() {
|
||||
|
||||
PostgresPlatform platform = new PostgresPlatform();
|
||||
PlatformDdl ddl = PlatformDdlBuilder.create(platform);
|
||||
|
||||
assertThat(ddl.convert("clob")).isEqualTo("text");
|
||||
assertThat(ddl.convert("blob")).isEqualTo("bytea");
|
||||
assertThat(ddl.convert("json")).isEqualTo("json");
|
||||
assertThat(ddl.convert("jsonb")).isEqualTo("jsonb");
|
||||
assertThat(ddl.convert("hstore")).isEqualTo("hstore");
|
||||
assertThat(ddl.convert("double")).isEqualTo("float");
|
||||
assertThat(ddl.convert("tinyint")).isEqualTo("smallint");
|
||||
assertThat(ddl.convert("double")).isEqualTo("float");
|
||||
assertThat(ddl.convert("varchar(20)")).isEqualTo("varchar(20)");
|
||||
assertThat(ddl.convert("decimal")).isEqualTo("decimal(16,3)");
|
||||
assertThat(ddl.convert("decimal(10)")).isEqualTo("decimal(10)");
|
||||
assertThat(ddl.convert("decimal(8,4)")).isEqualTo("decimal(8,4)");
|
||||
assertThat(ddl.convert("boolean")).isEqualTo("boolean");
|
||||
assertThat(ddl.convert("bit")).isEqualTo("bit");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUuidType() {
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@ package io.ebean.config.dbplatform;
|
||||
|
||||
import io.ebean.config.PlatformConfig;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.PlatformDdl;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -12,27 +10,6 @@ public class SqlserverPlatformTest {
|
||||
|
||||
SqlServer17Platform platform = new SqlServer17Platform();
|
||||
|
||||
@Test
|
||||
public void testTypeConversion() {
|
||||
|
||||
PlatformDdl ddl = PlatformDdlBuilder.create(platform);
|
||||
|
||||
assertThat(ddl.convert("clob")).isEqualTo("nvarchar(max)");
|
||||
assertThat(ddl.convert("blob")).isEqualTo("image");
|
||||
assertThat(ddl.convert("json")).isEqualTo("nvarchar(max)");
|
||||
assertThat(ddl.convert("jsonb")).isEqualTo("nvarchar(max)");
|
||||
|
||||
assertThat(ddl.convert("double")).isEqualTo("float(32)");
|
||||
assertThat(ddl.convert("varchar(20)")).isEqualTo("nvarchar(20)");
|
||||
assertThat(ddl.convert("decimal(10)")).isEqualTo("numeric(10)");
|
||||
assertThat(ddl.convert("decimal(8,4)")).isEqualTo("numeric(8,4)");
|
||||
assertThat(ddl.convert("decimal")).isEqualTo("numeric(16,3)");
|
||||
assertThat(ddl.convert("boolean")).isEqualTo("bit");
|
||||
assertThat(ddl.convert("bit")).isEqualTo("bit");
|
||||
assertThat(ddl.convert("tinyint")).isEqualTo("smallint");
|
||||
assertThat(ddl.convert("binary(16)")).isEqualTo("binary(16)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uuid_default() {
|
||||
|
||||
|
||||
@@ -45,7 +45,6 @@ import io.ebean.plugin.Property;
|
||||
import io.ebean.plugin.SpiServer;
|
||||
import io.ebean.text.csv.CsvReader;
|
||||
import io.ebean.text.json.JsonContext;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
|
||||
import io.ebeaninternal.server.core.SpiResultSet;
|
||||
import io.ebeaninternal.server.core.timezone.DataTimeZone;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration;
|
||||
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebean.EbeanServerFactory;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import org.junit.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");
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
config.setName("migrationtest-history");
|
||||
config.loadFromProperties();
|
||||
config.setRegister(false);
|
||||
config.setDefaultServer(false);
|
||||
|
||||
|
||||
config.setPackages(Arrays.asList("misc.migration.history.v1_0"));
|
||||
EbeanServer server = EbeanServerFactory.create(config);
|
||||
migration.setServer(server);
|
||||
|
||||
// First, we clean up the output-directory
|
||||
assertThat(migration.getMigrationDirectory().getAbsolutePath()).contains("migrationtest-history");
|
||||
Files.walk(migration.getMigrationDirectory().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 = EbeanServerFactory.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");
|
||||
|
||||
logger.info("end");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration;
|
||||
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebean.EbeanServerFactory;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import org.junit.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);
|
||||
|
||||
@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");
|
||||
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, "db2");
|
||||
migration.addPlatform(Platform.H2, "h2");
|
||||
migration.addPlatform(Platform.HSQLDB, "hsqldb");
|
||||
migration.addPlatform(Platform.MYSQL, "mysql");
|
||||
migration.addPlatform(Platform.MYSQL55, "mysql55");
|
||||
migration.addPlatform(Platform.POSTGRES, "postgres");
|
||||
migration.addPlatform(Platform.ORACLE, "oracle");
|
||||
migration.addPlatform(Platform.SQLITE, "sqlite");
|
||||
migration.addPlatform(Platform.SQLSERVER17, "sqlserver17");
|
||||
migration.addPlatform(Platform.HANA, "hana");
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
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"));
|
||||
EbeanServer server = EbeanServerFactory.create(config);
|
||||
migration.setServer(server);
|
||||
|
||||
// First, we clean up the output-directory
|
||||
assertThat(migration.getMigrationDirectory().getAbsolutePath()).contains("migrationtest");
|
||||
Files.walk(migration.getMigrationDirectory().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 = EbeanServerFactory.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 = EbeanServerFactory.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
|
||||
|
||||
logger.info("end");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
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 io.ebeaninternal.dbmigration.ddlgeneration.Helper;
|
||||
import org.junit.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().getPluginApi().getDatabasePlatform().getName()+"/" + 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");
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LastMigrationTest {
|
||||
|
||||
@Test
|
||||
public void lastVersion() {
|
||||
File d = new File("src/test/resources/dbmigration/migrationtest/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/dbmigration/migrationtest-history");
|
||||
assertThat(LastMigration.lastVersion(d, null)).isEqualTo("1.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastVersion_test() {
|
||||
File d = new File("src/test/resources/test-dbmigration");
|
||||
assertThat(LastMigration.lastVersion(d, null)).isEqualTo("2.1");
|
||||
assertThat(LastMigration.nextVersion(d, null, false)).isEqualTo("2.2");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastVersion_app2() {
|
||||
File d = new File("src/test/resources/dbmigration/app2");
|
||||
assertThat(LastMigration.lastVersion(d, null)).isEqualTo("3.1");
|
||||
assertThat(LastMigration.nextVersion(d, null, false)).isEqualTo("3.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastVersion_app3() {
|
||||
File d = new File("src/test/resources/dbmigration/app3");
|
||||
assertThat(LastMigration.lastVersion(d, null)).isEqualTo("3.1.2");
|
||||
assertThat(LastMigration.nextVersion(d, null, false)).isEqualTo("3.1.3");
|
||||
assertThat(LastMigration.nextVersion(d, null, true)).isEqualTo("3.1.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastVersion_app3_with_model_larger() {
|
||||
File d = new File("src/test/resources/dbmigration/app3");
|
||||
File m = new File("src/test/resources/dbmigration/app3/model_larger");
|
||||
assertThat(LastMigration.lastVersion(d, m)).isEqualTo("4.1");
|
||||
assertThat(LastMigration.nextVersion(d, m, false)).isEqualTo("4.2");
|
||||
assertThat(LastMigration.nextVersion(d, m, true)).isEqualTo("4.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastVersion_app3_with_model_smaller() {
|
||||
|
||||
File d = new File("src/test/resources/dbmigration/app3");
|
||||
File m = new File("src/test/resources/dbmigration/app3/model_smaller");
|
||||
assertThat(LastMigration.lastVersion(d, m)).isEqualTo("3.1.2");
|
||||
assertThat(LastMigration.nextVersion(d, m, false)).isEqualTo("3.1.3");
|
||||
assertThat(LastMigration.nextVersion(d, m, true)).isEqualTo("3.1.2");
|
||||
}
|
||||
}
|
||||
-270
@@ -1,270 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.hana.HanaPlatform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.model.CurrentModel;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BaseDdlHandlerTest extends BaseTestCase {
|
||||
|
||||
private ServerConfig serverConfig = new ServerConfig();
|
||||
|
||||
private DdlHandler handler(DatabasePlatform platform) {
|
||||
return PlatformDdlBuilder.create(platform).createDdlHandler(serverConfig);
|
||||
}
|
||||
|
||||
private DdlHandler h2Handler() {
|
||||
return handler(new H2Platform());
|
||||
}
|
||||
|
||||
private DdlHandler postgresHandler() {
|
||||
return handler(new PostgresPlatform());
|
||||
}
|
||||
|
||||
private DdlHandler sqlserverHandler() {
|
||||
return handler(new SqlServer17Platform());
|
||||
}
|
||||
|
||||
private DdlHandler hanaHandler() {
|
||||
return handler(new HanaPlatform());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addColumn_nullable_noConstraint() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
h2Handler().generate(write, Helper.getAddColumn());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column added_to_foo varchar(20);\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
sqlserverHandler().generate(write, Helper.getAddColumn());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add added_to_foo nvarchar(20);\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
hanaHandler().generate(write, Helper.getAddColumn());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add ( added_to_foo nvarchar(20));\n\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addColumn_withCheckConstraint() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
h2Handler().generate(write, Helper.getAlterTableAddColumnWithCheckConstraint());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column status integer;\n"
|
||||
+ "alter table foo add constraint ck_ordering_status check ( status in (0,1));\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
hanaHandler().generate(write, Helper.getAlterTableAddColumnWithCheckConstraint());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add ( status integer);\n"
|
||||
+ "alter table foo add constraint ck_ordering_status check ( status in (0,1));\n\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the functionality of the Ebean {@literal @}DbArray extension during DDL
|
||||
* generation.
|
||||
*/
|
||||
@Test
|
||||
public void addColumn_dbarray() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
DdlHandler postgresHandler = postgresHandler();
|
||||
postgresHandler.generate(write, Helper.getAlterTableAddDbArrayColumn());
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column dbarray_added_to_foo varchar[];\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
|
||||
DdlHandler sqlserverHandler = sqlserverHandler();
|
||||
sqlserverHandler.generate(write, Helper.getAlterTableAddDbArrayColumn());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add dbarray_added_to_foo varchar(1000);\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
|
||||
DdlHandler hanaHandler = hanaHandler();
|
||||
hanaHandler.generate(write, Helper.getAlterTableAddDbArrayColumn());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add ( dbarray_added_to_foo nvarchar(255) array);\n\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addColumn_dbarray_withLength() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
postgresHandler().generate(write, Helper.getAlterTableAddDbArrayColumnWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column dbarray_ninety varchar[];\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
h2Handler().generate(write, Helper.getAlterTableAddDbArrayColumnWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column dbarray_ninety array;\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
sqlserverHandler().generate(write, Helper.getAlterTableAddDbArrayColumnWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add dbarray_ninety varchar(90);\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
hanaHandler().generate(write, Helper.getAlterTableAddDbArrayColumnWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add ( dbarray_ninety nvarchar(255) array(90));\n\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addColumn_dbarray_integer_withLength() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
postgresHandler().generate(write, Helper.getAlterTableAddDbArrayColumnIntegerWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column dbarray_integer integer[];\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
h2Handler().generate(write, Helper.getAlterTableAddDbArrayColumnIntegerWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column dbarray_integer array;\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
sqlserverHandler().generate(write, Helper.getAlterTableAddDbArrayColumnIntegerWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add dbarray_integer varchar(90);\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
sqlserverHandler().generate(write, Helper.getAlterTableAddDbArrayColumnInteger());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add dbarray_integer varchar(1000);\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
hanaHandler().generate(write, Helper.getAlterTableAddDbArrayColumnIntegerWithLength());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add ( dbarray_integer integer array(90));\n\n");
|
||||
|
||||
write = new DdlWrite();
|
||||
hanaHandler().generate(write, Helper.getAlterTableAddDbArrayColumnInteger());
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add ( dbarray_integer integer array);\n\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addColumn_withForeignKey() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
DdlHandler handler = h2Handler();
|
||||
handler.generate(write, Helper.getAlterTableAddColumn());
|
||||
|
||||
String buffer = write.apply().getBuffer();
|
||||
assertThat(buffer).contains("alter table foo add column some_id integer;");
|
||||
|
||||
String fkBuffer = write.applyForeignKeys().getBuffer();
|
||||
assertThat(fkBuffer).contains(
|
||||
"alter table foo add constraint fk_foo_some_id foreign key (some_id) references bar (id) on delete restrict on update restrict;");
|
||||
assertThat(fkBuffer).contains("create index idx_foo_some_id on foo (some_id);");
|
||||
assertThat(write.dropAll().getBuffer()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dropColumn() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
DdlHandler handler = h2Handler();
|
||||
|
||||
handler.generate(write, Helper.getDropColumn());
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo drop column col2;\n\n");
|
||||
assertThat(write.dropAll().getBuffer()).isEqualTo("");
|
||||
|
||||
write = new DdlWrite();
|
||||
DdlHandler hanaHandler = hanaHandler();
|
||||
|
||||
hanaHandler.generate(write, Helper.getDropColumn());
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo("CALL usp_ebean_drop_column('foo', 'col2');\n\n");
|
||||
assertThat(write.dropAll().getBuffer()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createTable() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
DdlHandler handler = h2Handler();
|
||||
|
||||
handler.generate(write, Helper.getCreateTable());
|
||||
|
||||
String createTableDDL = Helper.asText(this, "/assert/create-table.txt");
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo(createTableDDL);
|
||||
assertThat(write.dropAll().getBuffer().trim()).isEqualTo("drop table if exists foo;");
|
||||
|
||||
write = new DdlWrite();
|
||||
DdlHandler hanaHandler = hanaHandler();
|
||||
|
||||
hanaHandler.generate(write, Helper.getCreateTable());
|
||||
|
||||
String createColumnTableDDL = Helper.asText(this, "/assert/create-column-table.txt");
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo(createColumnTableDDL);
|
||||
assertThat(write.dropAll().getBuffer().trim()).isEqualTo("drop table foo cascade;");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateChangeSet() throws Exception {
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
DdlHandler handler = h2Handler();
|
||||
|
||||
handler.generate(write, Helper.getChangeSet());
|
||||
|
||||
String apply = Helper.asText(this, "/assert/BaseDdlHandlerTest/baseApply.sql");
|
||||
String rollbackLast = Helper.asText(this, "/assert/BaseDdlHandlerTest/baseDropAll.sql");
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo(apply);
|
||||
assertThat(write.dropAll().getBuffer()).isEqualTo(rollbackLast);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void generateChangeSetFromModel() throws Exception {
|
||||
|
||||
SpiEbeanServer defaultServer = (SpiEbeanServer) Ebean.getDefaultServer();
|
||||
|
||||
ChangeSet createChangeSet = new CurrentModel(defaultServer).getChangeSet();
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
DdlHandler handler = h2Handler();
|
||||
handler.generate(write, createChangeSet);
|
||||
|
||||
String apply = Helper.asText(this, "/assert/changeset-apply.txt");
|
||||
String rollbackLast = Helper.asText(this, "/assert/changeset-dropAll.txt");
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo(apply);
|
||||
assertThat(write.dropAll().getBuffer()).isEqualTo(rollbackLast);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void generateChangeSetFromModel_given_postgresTypes() throws Exception {
|
||||
|
||||
SpiEbeanServer defaultServer = (SpiEbeanServer) Ebean.getDefaultServer();
|
||||
|
||||
ChangeSet createChangeSet = new CurrentModel(defaultServer).getChangeSet();
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
DdlHandler handler = postgresHandler();
|
||||
handler.generate(write, createChangeSet);
|
||||
|
||||
String apply = Helper.asText(this, "/assert/changeset-pg-apply.sql");
|
||||
String applyLast = Helper.asText(this, "/assert/changeset-pg-applyLast.sql");
|
||||
String rollbackFirst = Helper.asText(this, "/assert/changeset-pg-rollbackFirst.sql");
|
||||
String rollbackLast = Helper.asText(this, "/assert/changeset-pg-rollbackLast.sql");
|
||||
|
||||
assertThat(write.apply().getBuffer()).isEqualTo(apply);
|
||||
assertThat(write.applyForeignKeys().getBuffer()).isEqualTo(applyLast);
|
||||
assertThat(write.dropAllForeignKeys().getBuffer()).isEqualTo(rollbackFirst);
|
||||
assertThat(write.dropAll().getBuffer()).isEqualTo(rollbackLast);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration;
|
||||
|
||||
import io.ebeaninternal.dbmigration.migration.AddColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.migration.CreateTable;
|
||||
import io.ebeaninternal.dbmigration.migration.DropColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helper for testing to return some basic migration objects.
|
||||
*/
|
||||
public class Helper {
|
||||
|
||||
static Migration migration = MigrationXmlReader.read("/container/test-create-table.xml");
|
||||
|
||||
static Migration alterTableMigration = MigrationXmlReader.read("/container/test-alter-table.xml");
|
||||
|
||||
static ChangeSet changeSet;
|
||||
|
||||
static List<Object> changeSetChildren;
|
||||
|
||||
static ChangeSet alterTableChangeSet;
|
||||
|
||||
static List<Object> alterTableChangeSetChildren;
|
||||
|
||||
static {
|
||||
List<ChangeSet> changeSets = migration.getChangeSet();
|
||||
changeSet = changeSets.get(0);
|
||||
changeSetChildren = changeSet.getChangeSetChildren();
|
||||
|
||||
alterTableChangeSet = alterTableMigration.getChangeSet().get(0);
|
||||
alterTableChangeSetChildren = alterTableChangeSet.getChangeSetChildren();
|
||||
}
|
||||
|
||||
public static ChangeSet getChangeSet() {
|
||||
return changeSet;
|
||||
}
|
||||
|
||||
public static CreateTable getCreateTable() {
|
||||
return (CreateTable) changeSetChildren.get(0);
|
||||
}
|
||||
|
||||
public static AddColumn getAddColumn() {
|
||||
return (AddColumn) changeSetChildren.get(1);
|
||||
}
|
||||
|
||||
public static AddColumn getAlterTableAddColumn() {
|
||||
return (AddColumn) alterTableChangeSetChildren.get(0);
|
||||
}
|
||||
|
||||
public static AddColumn getAlterTableAddDbArrayColumn() {
|
||||
return (AddColumn) alterTableChangeSetChildren.get(1);
|
||||
}
|
||||
|
||||
public static AddColumn getAlterTableAddDbArrayColumnWithLength() {
|
||||
return (AddColumn) alterTableChangeSetChildren.get(2);
|
||||
}
|
||||
|
||||
public static AddColumn getAlterTableAddDbArrayColumnIntegerWithLength() {
|
||||
return (AddColumn) alterTableChangeSetChildren.get(3);
|
||||
}
|
||||
|
||||
public static AddColumn getAlterTableAddDbArrayColumnInteger() {
|
||||
return (AddColumn) alterTableChangeSetChildren.get(4);
|
||||
}
|
||||
|
||||
public static AddColumn getAlterTableAddColumnWithCheckConstraint() {
|
||||
return (AddColumn) alterTableChangeSetChildren.get(5);
|
||||
}
|
||||
|
||||
public static DropColumn getDropColumn() {
|
||||
return (DropColumn) changeSetChildren.get(2);
|
||||
}
|
||||
|
||||
public static String asText(Object instance, String relativePath) throws IOException {
|
||||
InputStream is = instance.getClass().getResourceAsStream(relativePath);
|
||||
if (is == null) {
|
||||
throw new IllegalArgumentException("resource " + relativePath + " not found");
|
||||
}
|
||||
return asText(is);
|
||||
}
|
||||
|
||||
public static String asText(InputStream in) throws IOException {
|
||||
|
||||
try {
|
||||
InputStreamReader reader = new InputStreamReader(in);
|
||||
|
||||
LineNumberReader lineNumberReader = new LineNumberReader(reader);
|
||||
|
||||
StringBuilder builder = new StringBuilder(400);
|
||||
String line;
|
||||
while ((line = lineNumberReader.readLine()) != null) {
|
||||
builder.append(line).append("\n");
|
||||
}
|
||||
return builder.toString();
|
||||
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-204
@@ -1,204 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebean.config.dbplatform.clickhouse.ClickHousePlatform;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.Helper;
|
||||
import io.ebeaninternal.dbmigration.migration.AddTableComment;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
import io.ebeaninternal.dbmigration.migration.CreateTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
public class BaseTableDdlTest {
|
||||
|
||||
private ServerConfig serverConfig = new ServerConfig();
|
||||
|
||||
private PlatformDdl h2ddl = PlatformDdlBuilder.create(new H2Platform());
|
||||
|
||||
@Test
|
||||
public void testAlterColumn() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, h2ddl);
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
AlterColumn alterColumn = new AlterColumn();
|
||||
alterColumn.setTableName("mytab");
|
||||
alterColumn.setCheckConstraint("check (acol in ('A','B'))");
|
||||
alterColumn.setCheckConstraintName("ck_mytab_acol");
|
||||
|
||||
ddlGen.generate(write, alterColumn);
|
||||
|
||||
String ddl = write.apply().getBuffer();
|
||||
assertThat(ddl).contains("alter table mytab drop constraint if exists ck_mytab_acol");
|
||||
assertThat(ddl).contains("alter table mytab add constraint ck_mytab_acol check (acol in ('A','B'))");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddColumn_withTypeConversion() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, PlatformDdlBuilder.create(new OraclePlatform()));
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
Column column = new Column();
|
||||
column.setName("col_name");
|
||||
column.setType("varchar(20)");
|
||||
|
||||
ddlGen.alterTableAddColumn(write.apply(), "mytable", column, false, false);
|
||||
|
||||
String ddl = write.apply().getBuffer();
|
||||
assertThat(ddl).contains("alter table mytable add column col_name varchar2(20)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddColumn_withTypeConversion_clickHouseVarchar() throws IOException {
|
||||
|
||||
ClickHouseTableDdl ddlGen = new ClickHouseTableDdl(serverConfig, PlatformDdlBuilder.create(new ClickHousePlatform()));
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
Column column = new Column();
|
||||
column.setName("col_name");
|
||||
column.setType("varchar(20)");
|
||||
|
||||
ddlGen.alterTableAddColumn(write.apply(), "mytable", column, false, false);
|
||||
|
||||
String ddl = write.apply().getBuffer();
|
||||
assertThat(ddl).contains("alter table mytable add column col_name String");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnComment() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, h2ddl);
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
AlterColumn alterColumn = new AlterColumn();
|
||||
alterColumn.setTableName("mytab");
|
||||
alterColumn.setColumnName("acol");
|
||||
alterColumn.setComment("my comment");
|
||||
|
||||
ddlGen.generate(write, alterColumn);
|
||||
|
||||
String ddl = write.apply().getBuffer();
|
||||
assertThat(ddl).contains("comment on column mytab.acol is 'my comment'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alterTableAddColumnWithComment() throws IOException {
|
||||
BaseTableDdl ddl = new BaseTableDdl(serverConfig, h2ddl);
|
||||
DdlWrite write = new DdlWrite();
|
||||
Column column = new Column();
|
||||
column.setName("my_column");
|
||||
column.setComment("some comment");
|
||||
column.setType("int");
|
||||
|
||||
ddl.alterTableAddColumn(write.apply(), "my_table", column, false, false);
|
||||
assertEquals(
|
||||
"alter table my_table add column my_column int;\n" +
|
||||
"comment on column my_table.my_column is 'some comment';\n", write.apply().getBuffer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddTableComment() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, h2ddl);
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
AddTableComment addTableComment = new AddTableComment();
|
||||
addTableComment.setName("mytab");
|
||||
addTableComment.setComment("my comment");
|
||||
|
||||
ddlGen.generate(write, addTableComment);
|
||||
|
||||
String ddl = write.apply().getBuffer();
|
||||
assertThat(ddl).contains("comment on table mytab is 'my comment'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddTableComment_mysql() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, PlatformDdlBuilder.create(new MySqlPlatform()));
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
AddTableComment addTableComment = new AddTableComment();
|
||||
addTableComment.setName("mytab");
|
||||
addTableComment.setComment("my comment");
|
||||
|
||||
ddlGen.generate(write, addTableComment);
|
||||
|
||||
String ddl = write.apply().getBuffer();
|
||||
assertThat(ddl).contains("alter table mytab comment = 'my comment'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerate() throws Exception {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, h2ddl);
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
ddlGen.generate(write, createTable());
|
||||
String apply = write.apply().getBuffer();
|
||||
String applyLast = write.applyForeignKeys().getBuffer();
|
||||
|
||||
String rollbackFirst = write.dropAllForeignKeys().getBuffer();
|
||||
String rollbackLast = write.dropAll().getBuffer();
|
||||
|
||||
assertThat(apply).isEqualTo(Helper.asText(this, "/assert/BaseTableDdlTest/createTable-apply.txt"));
|
||||
assertThat(applyLast).isEqualTo(Helper.asText(this, "/assert/BaseTableDdlTest/createTable-applyLast.txt"));
|
||||
assertThat(rollbackFirst).isEqualTo(Helper.asText(this, "/assert/BaseTableDdlTest/createTable-rollbackFirst.txt"));
|
||||
assertThat(rollbackLast).isEqualTo(Helper.asText(this, "/assert/BaseTableDdlTest/createTable-rollback.txt"));
|
||||
}
|
||||
|
||||
private CreateTable createTable() {
|
||||
CreateTable createTable = new CreateTable();
|
||||
createTable.setName("mytable");
|
||||
createTable.setPkName("pk_mytable");
|
||||
List<Column> columns = createTable.getColumn();
|
||||
Column col = new Column();
|
||||
col.setName("id");
|
||||
col.setType("integer");
|
||||
col.setPrimaryKey(true);
|
||||
|
||||
columns.add(col);
|
||||
|
||||
Column col2 = new Column();
|
||||
col2.setName("status");
|
||||
col2.setType("varchar(1)");
|
||||
col2.setNotnull(true);
|
||||
col2.setCheckConstraint("check (status in ('A','B'))");
|
||||
col2.setCheckConstraintName("ck_mytable_status");
|
||||
|
||||
columns.add(col2);
|
||||
|
||||
Column col3 = new Column();
|
||||
col3.setName("order_id");
|
||||
col3.setType("integer");
|
||||
col3.setNotnull(true);
|
||||
col3.setReferences("orders.id");
|
||||
col3.setForeignKeyName("fk_mytable_order_id");
|
||||
col3.setForeignKeyIndex("ix_mytable_order_id");
|
||||
|
||||
columns.add(col3);
|
||||
|
||||
return createTable;
|
||||
}
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ClickHouseDbArrayTest {
|
||||
|
||||
@Test
|
||||
public void logicalToNative() {
|
||||
|
||||
assertThat(ClickHouseDbArray.logicalToNative("uuid[]")).isEqualTo("Array(UUID)");
|
||||
assertThat(ClickHouseDbArray.logicalToNative("varchar[]")).isEqualTo("Array(String)");
|
||||
assertThat(ClickHouseDbArray.logicalToNative("integer[]")).isEqualTo("Array(UInt32)");
|
||||
assertThat(ClickHouseDbArray.logicalToNative("bigint[]")).isEqualTo("Array(UInt64)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logicalToNative_withFallbackDefined() {
|
||||
|
||||
assertThat(ClickHouseDbArray.logicalToNative("uuid[]:(1000)")).isEqualTo("Array(UUID)");
|
||||
}
|
||||
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DbConstraintNormalise;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DbNameNormaliseTest {
|
||||
|
||||
DbConstraintNormalise normalise = new DbConstraintNormalise();
|
||||
|
||||
@Test
|
||||
public void testNormalise() throws Exception {
|
||||
|
||||
assertThat(normalise.normaliseTable("cat.sch.foo_bar]")).isEqualTo("foo_bar");
|
||||
assertThat(normalise.normaliseTable("sch.foo_bar]")).isEqualTo("foo_bar");
|
||||
assertThat(normalise.normaliseTable("foo_bar]")).isEqualTo("foo_bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrimQuotes() throws Exception {
|
||||
|
||||
assertThat(normalise.trimQuotes("[foo]")).isEqualTo("foo");
|
||||
assertThat(normalise.trimQuotes("'foo'")).isEqualTo("foo");
|
||||
assertThat(normalise.trimQuotes("\"foo\"")).isEqualTo("foo");
|
||||
assertThat(normalise.trimQuotes("`foo`")).isEqualTo("foo");
|
||||
|
||||
assertThat(normalise.trimQuotes("`fo_o`")).isEqualTo("fo_o");
|
||||
}
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.model.CurrentModel;
|
||||
import io.ebeaninternal.dbmigration.model.MConfiguration;
|
||||
import io.ebeaninternal.dbmigration.model.ModelContainer;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class H2HistoryDdlTest {
|
||||
|
||||
@Test
|
||||
public void testRegenerateHistoryTriggers() throws Exception {
|
||||
|
||||
SpiEbeanServer ebeanServer = (SpiEbeanServer) Ebean.getDefaultServer();
|
||||
|
||||
HistoryTableUpdate update = new HistoryTableUpdate("c_user");
|
||||
update.add(HistoryTableUpdate.Change.ADD, "one");
|
||||
update.add(HistoryTableUpdate.Change.DROP, "two");
|
||||
|
||||
|
||||
CurrentModel currentModel = new CurrentModel(ebeanServer);
|
||||
ModelContainer modelContainer = currentModel.read();
|
||||
DdlWrite write = new DdlWrite(new MConfiguration(), modelContainer, new DdlOptions());
|
||||
|
||||
H2Platform h2Platform = new H2Platform();
|
||||
PlatformDdl h2Ddl = PlatformDdlBuilder.create(h2Platform);
|
||||
h2Ddl.configure(ebeanServer.getServerConfig());
|
||||
h2Ddl.regenerateHistoryTriggers(write, update);
|
||||
|
||||
assertThat(write.applyHistoryView().isEmpty()).isFalse();
|
||||
assertThat(write.applyHistoryTrigger().isEmpty()).isFalse();
|
||||
assertThat(write.applyHistoryView().getBuffer())
|
||||
.contains("create view")
|
||||
.doesNotContain("create trigger");
|
||||
assertThat(write.applyHistoryTrigger().getBuffer())
|
||||
.contains("add one")
|
||||
.contains("create trigger")
|
||||
.doesNotContain("create view");
|
||||
assertThat(write.dropAll().isEmpty()).isTrue();
|
||||
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.dbplatform.hana.HanaPlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class HanaDdlTest {
|
||||
|
||||
@Test
|
||||
public void alterTableDropColumn() throws IOException {
|
||||
HanaColumnStoreDdl ddl = new HanaColumnStoreDdl(new HanaPlatform());
|
||||
DdlWrite write = new DdlWrite();
|
||||
ddl.alterTableDropColumn(write.apply(), "my_table", "my_column");
|
||||
assertEquals("CALL usp_ebean_drop_column('my_table', 'my_column');\n", write.apply().getBuffer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alterTableAddColumn() throws IOException {
|
||||
HanaColumnStoreDdl ddl = new HanaColumnStoreDdl(new HanaPlatform());
|
||||
DdlWrite write = new DdlWrite();
|
||||
Column column = new Column();
|
||||
column.setName("my_column");
|
||||
column.setComment("comment");
|
||||
column.setDefaultValue("1");
|
||||
column.setNotnull(Boolean.TRUE);
|
||||
column.setType("int");
|
||||
column.setUnique("unique");
|
||||
column.setPrimaryKey(Boolean.TRUE);
|
||||
column.setCheckConstraint("CHECK(my_column > 0)");
|
||||
column.setCheckConstraintName("check_constraint");
|
||||
column.setHistoryExclude(Boolean.TRUE);
|
||||
column.setIdentity(Boolean.TRUE);
|
||||
ddl.alterTableAddColumn(write.apply(), "my_table", column, false, "1");
|
||||
assertEquals("alter table my_table add ( my_column int default 1 not null);\nalter table my_table add constraint check_constraint CHECK(my_column > 0);\n", write.apply().getBuffer());
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class HistoryTableUpdateTest {
|
||||
|
||||
@Test
|
||||
public void testDescription() throws Exception {
|
||||
|
||||
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
|
||||
upd.add(HistoryTableUpdate.Change.ADD, "two");
|
||||
upd.add(HistoryTableUpdate.Change.DROP, "four");
|
||||
|
||||
assertThat(upd.description()).isEqualTo("[add two, drop four]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescription_withIncludeExclude() throws Exception {
|
||||
|
||||
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
|
||||
upd.add(HistoryTableUpdate.Change.ADD, "two");
|
||||
upd.add(HistoryTableUpdate.Change.INCLUDE, "five");
|
||||
upd.add(HistoryTableUpdate.Change.EXCLUDE, "six");
|
||||
upd.add(HistoryTableUpdate.Change.DROP, "four");
|
||||
|
||||
assertThat(upd.description()).isEqualTo("[add two, include five, exclude six, drop four]");
|
||||
}
|
||||
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.util.IndexSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class IndexSetTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
IndexSet set = new IndexSet();
|
||||
assertTrue(set.add(new String[]{"one_column"}));
|
||||
assertTrue(set.add(new String[]{"two_column"}));
|
||||
assertFalse(set.add(new String[]{"one_column"}));
|
||||
|
||||
assertTrue(set.add(new String[]{"a", "b"}));
|
||||
assertTrue(set.add(new String[]{"b", "c"}));
|
||||
assertTrue(set.add(new String[]{"a"}));
|
||||
assertFalse(set.add(new String[]{"a", "b"}));
|
||||
|
||||
}
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class NativeDbArrayTest {
|
||||
|
||||
@Test
|
||||
public void logicalToNative() {
|
||||
|
||||
assertThat(NativeDbArray.logicalToNative("uuid[]")).isEqualTo("uuid[]");
|
||||
assertThat(NativeDbArray.logicalToNative("varchar[]")).isEqualTo("varchar[]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logicalToNative_withFallbackDefined() {
|
||||
|
||||
assertThat(NativeDbArray.logicalToNative("uuid[]:(1000)")).isEqualTo("uuid[]");
|
||||
}
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.annotation.ConstraintMode;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class OracleDdlTest {
|
||||
|
||||
OracleDdl create() {
|
||||
return new OracleDdl(new OraclePlatform());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendForeignKeyOnDelete_expectEmtpy_when_nullRestrictSetDefault() {
|
||||
|
||||
OracleDdl oracle = create();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
oracle.appendForeignKeyOnDelete(sb, oracle.withDefault(null));
|
||||
assertThat(sb.toString()).isEqualTo("");
|
||||
|
||||
sb = new StringBuilder();
|
||||
oracle.appendForeignKeyOnDelete(sb, ConstraintMode.RESTRICT);
|
||||
assertThat(sb.toString()).isEqualTo("");
|
||||
|
||||
|
||||
sb = new StringBuilder();
|
||||
oracle.appendForeignKeyOnDelete(sb, ConstraintMode.SET_DEFAULT);
|
||||
assertThat(sb.toString()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendForeignKeyOnDelete_setNull() {
|
||||
|
||||
OracleDdl oracle = create();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
oracle.appendForeignKeyOnDelete(sb, ConstraintMode.SET_NULL);
|
||||
assertThat(sb.toString()).isEqualTo(" on delete set null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendForeignKeyOnDelete_cascade() {
|
||||
|
||||
OracleDdl oracle = create();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
oracle.appendForeignKeyOnDelete(sb, ConstraintMode.CASCADE);
|
||||
assertThat(sb.toString()).isEqualTo(" on delete cascade");
|
||||
}
|
||||
|
||||
}
|
||||
-288
@@ -1,288 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.IdType;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.hana.HanaPlatform;
|
||||
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PlatformDdl_AlterColumnTest {
|
||||
|
||||
private final PlatformDdl h2Ddl = PlatformDdlBuilder.create(new H2Platform());
|
||||
private final PlatformDdl pgDdl = PlatformDdlBuilder.create(new PostgresPlatform());
|
||||
private final PlatformDdl mysqlDdl = PlatformDdlBuilder.create(new MySqlPlatform());
|
||||
private final PlatformDdl oraDdl = PlatformDdlBuilder.create(new OraclePlatform());
|
||||
private final PlatformDdl sqlServerDdl = PlatformDdlBuilder.create(new SqlServer17Platform());
|
||||
private final PlatformDdl hanaDdl = PlatformDdlBuilder.create(new HanaPlatform());
|
||||
|
||||
{
|
||||
DatabaseConfig serverConfig = Ebean.getDefaultServer().getPluginApi().getServerConfig();
|
||||
sqlServerDdl.configure(serverConfig);
|
||||
}
|
||||
|
||||
AlterColumn alterNotNull() {
|
||||
AlterColumn alterColumn = new AlterColumn();
|
||||
alterColumn.setTableName("mytab");
|
||||
alterColumn.setColumnName("acol");
|
||||
alterColumn.setCurrentType("varchar(5)");
|
||||
alterColumn.setNotnull(Boolean.TRUE);
|
||||
|
||||
return alterColumn;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayType_default() {
|
||||
assertThat(mysqlDdl.convertArrayType("varchar[](90)")).isEqualTo("varchar(90)");
|
||||
assertThat(mysqlDdl.convertArrayType("integer[](60)")).isEqualTo("varchar(60)");
|
||||
assertThat(mysqlDdl.convertArrayType("varchar[]")).isEqualTo("varchar(1000)");
|
||||
assertThat(mysqlDdl.convertArrayType("integer[]")).isEqualTo("varchar(1000)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayType_h2() {
|
||||
assertThat(h2Ddl.convertArrayType("varchar[](90)")).isEqualTo("array");
|
||||
assertThat(h2Ddl.convertArrayType("integer[](60)")).isEqualTo("array");
|
||||
assertThat(h2Ddl.convertArrayType("varchar[]")).isEqualTo("array");
|
||||
assertThat(h2Ddl.convertArrayType("integer[]")).isEqualTo("array");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayType_postgres() {
|
||||
assertThat(pgDdl.convertArrayType("varchar[](90)")).isEqualTo("varchar[]");
|
||||
assertThat(pgDdl.convertArrayType("integer[](60)")).isEqualTo("integer[]");
|
||||
assertThat(pgDdl.convertArrayType("varchar[]")).isEqualTo("varchar[]");
|
||||
assertThat(pgDdl.convertArrayType("integer[]")).isEqualTo("integer[]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayType_hana() {
|
||||
assertThat(hanaDdl.convertArrayType("varchar[](90)")).isEqualTo("nvarchar(255) array(90)");
|
||||
assertThat(hanaDdl.convertArrayType("integer[](60)")).isEqualTo("integer array(60)");
|
||||
assertThat(hanaDdl.convertArrayType("varchar[]")).isEqualTo("nvarchar(255) array");
|
||||
assertThat(hanaDdl.convertArrayType("integer[]")).isEqualTo("integer array");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnBaseAttributes() {
|
||||
|
||||
AlterColumn alterColumn = alterNotNull();
|
||||
assertNull(h2Ddl.alterColumnBaseAttributes(alterColumn));
|
||||
assertNull(pgDdl.alterColumnBaseAttributes(alterColumn));
|
||||
assertNull(oraDdl.alterColumnBaseAttributes(alterColumn));
|
||||
|
||||
String sql = mysqlDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab modify acol varchar(5) not null", sql);
|
||||
|
||||
sql = sqlServerDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab alter column acol nvarchar(5) not null", sql);
|
||||
|
||||
sql = hanaDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab alter ( acol nvarchar(5) not null)", sql);
|
||||
|
||||
alterColumn.setNotnull(Boolean.FALSE);
|
||||
sql = mysqlDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab modify acol varchar(5)", sql);
|
||||
|
||||
sql = hanaDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab alter ( acol nvarchar(5))", sql);
|
||||
|
||||
alterColumn.setNotnull(null);
|
||||
alterColumn.setType("varchar(100)");
|
||||
|
||||
sql = mysqlDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab modify acol varchar(100)", sql);
|
||||
|
||||
sql = hanaDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab alter ( acol nvarchar(100))", sql);
|
||||
|
||||
alterColumn.setCurrentNotnull(Boolean.TRUE);
|
||||
sql = mysqlDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab modify acol varchar(100) not null", sql);
|
||||
|
||||
sql = hanaDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab alter ( acol nvarchar(100) not null)", sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnType() {
|
||||
|
||||
String sql = h2Ddl.alterColumnType("mytab", "acol", "varchar(20)");
|
||||
assertEquals("alter table mytab alter column acol varchar(20)", sql);
|
||||
|
||||
sql = pgDdl.alterColumnType("mytab", "acol", "varchar(20)");
|
||||
assertEquals("alter table mytab alter column acol type varchar(20) using acol::varchar(20)", sql);
|
||||
sql = pgDdl.alterColumnType("mytab", "acol", "bigint");
|
||||
assertEquals("alter table mytab alter column acol type bigint using acol::bigint", sql);
|
||||
|
||||
sql = oraDdl.alterColumnType("mytab", "acol", "varchar(20)");
|
||||
assertEquals("alter table mytab modify acol varchar2(20)", sql);
|
||||
|
||||
sql = mysqlDdl.alterColumnType("mytab", "acol", "varchar(20)");
|
||||
assertNull(sql);
|
||||
|
||||
sql = sqlServerDdl.alterColumnType("mytab", "acol", "varchar(20)");
|
||||
assertNull(sql);
|
||||
|
||||
sql = hanaDdl.alterColumnType("mytab", "acol", "varchar(20)");
|
||||
assertNull(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnNotnull() {
|
||||
|
||||
String sql = h2Ddl.alterColumnNotnull("mytab", "acol", true);
|
||||
assertEquals("alter table mytab alter column acol set not null", sql);
|
||||
|
||||
sql = pgDdl.alterColumnNotnull("mytab", "acol", true);
|
||||
assertEquals("alter table mytab alter column acol set not null", sql);
|
||||
|
||||
sql = oraDdl.alterColumnNotnull("mytab", "acol", true);
|
||||
assertEquals("alter table mytab modify acol not null", sql);
|
||||
|
||||
sql = mysqlDdl.alterColumnNotnull("mytab", "acol", true);
|
||||
assertNull(sql);
|
||||
|
||||
sql = sqlServerDdl.alterColumnNotnull("mytab", "acol", true);
|
||||
assertNull(sql);
|
||||
|
||||
sql = hanaDdl.alterColumnNotnull("mytab", "acol", true);
|
||||
assertNull(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnNull() {
|
||||
|
||||
String sql = h2Ddl.alterColumnNotnull("mytab", "acol", false);
|
||||
assertEquals("alter table mytab alter column acol set null", sql);
|
||||
|
||||
sql = pgDdl.alterColumnNotnull("mytab", "acol", false);
|
||||
assertEquals("alter table mytab alter column acol drop not null", sql);
|
||||
|
||||
sql = oraDdl.alterColumnNotnull("mytab", "acol", false);
|
||||
assertEquals("alter table mytab modify acol null", sql);
|
||||
|
||||
sql = mysqlDdl.alterColumnNotnull("mytab", "acol", false);
|
||||
assertNull(sql);
|
||||
|
||||
sql = sqlServerDdl.alterColumnNotnull("mytab", "acol", false);
|
||||
assertNull(sql);
|
||||
|
||||
sql = hanaDdl.alterColumnNotnull("mytab", "acol", false);
|
||||
assertNull(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnDefaultValue() {
|
||||
|
||||
String sql = h2Ddl.alterColumnDefaultValue("mytab", "acol", "'hi'");
|
||||
assertEquals("alter table mytab alter column acol set default 'hi'", sql);
|
||||
|
||||
sql = pgDdl.alterColumnDefaultValue("mytab", "acol", "'hi'");
|
||||
assertEquals("alter table mytab alter column acol set default 'hi'", sql);
|
||||
|
||||
sql = oraDdl.alterColumnDefaultValue("mytab", "acol", "'hi'");
|
||||
assertEquals("alter table mytab modify acol default 'hi'", sql);
|
||||
|
||||
sql = mysqlDdl.alterColumnDefaultValue("mytab", "acol", "'hi'");
|
||||
assertEquals("alter table mytab alter acol set default 'hi'", sql);
|
||||
|
||||
sql = sqlServerDdl.alterColumnDefaultValue("mytab", "acol", "'hi'");
|
||||
assertEquals("alter table mytab add default 'hi' for acol", sql);
|
||||
|
||||
boolean exceptionCaught = false;
|
||||
try {
|
||||
hanaDdl.alterColumnDefaultValue("mytab", "acol", "'hi'");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exceptionCaught = true;
|
||||
}
|
||||
assertTrue(exceptionCaught);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnDropDefault() {
|
||||
|
||||
String sql = h2Ddl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT");
|
||||
assertEquals("alter table mytab alter column acol drop default", sql);
|
||||
|
||||
sql = pgDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT");
|
||||
assertEquals("alter table mytab alter column acol drop default", sql);
|
||||
|
||||
sql = oraDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT");
|
||||
assertEquals("alter table mytab modify acol drop default", sql);
|
||||
|
||||
sql = mysqlDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT");
|
||||
assertEquals("alter table mytab alter acol drop default", sql);
|
||||
|
||||
sql = sqlServerDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT");
|
||||
assertEquals("EXEC usp_ebean_drop_default_constraint mytab, acol", sql);
|
||||
|
||||
boolean exceptionCaught = false;
|
||||
try {
|
||||
hanaDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exceptionCaught = true;
|
||||
}
|
||||
assertTrue(exceptionCaught);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useIdentityType_h2() {
|
||||
assertEquals(h2Ddl.useIdentityType(null), IdType.IDENTITY);
|
||||
assertEquals(h2Ddl.useIdentityType(IdType.SEQUENCE), IdType.SEQUENCE);
|
||||
assertEquals(h2Ddl.useIdentityType(IdType.IDENTITY), IdType.IDENTITY);
|
||||
assertEquals(h2Ddl.useIdentityType(IdType.GENERATOR), IdType.GENERATOR);
|
||||
assertEquals(h2Ddl.useIdentityType(IdType.EXTERNAL), IdType.EXTERNAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useIdentityType_postgres() {
|
||||
assertEquals(pgDdl.useIdentityType(IdType.GENERATOR), IdType.GENERATOR);
|
||||
assertEquals(pgDdl.useIdentityType(IdType.EXTERNAL), IdType.EXTERNAL);
|
||||
|
||||
assertEquals(pgDdl.useIdentityType(null), IdType.IDENTITY);
|
||||
assertEquals(pgDdl.useIdentityType(IdType.SEQUENCE), IdType.SEQUENCE);
|
||||
assertEquals(pgDdl.useIdentityType(IdType.IDENTITY), IdType.IDENTITY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useIdentityType_mysql() {
|
||||
|
||||
assertEquals(mysqlDdl.useIdentityType(null), IdType.IDENTITY);
|
||||
assertEquals(mysqlDdl.useIdentityType(IdType.SEQUENCE), IdType.IDENTITY);
|
||||
assertEquals(mysqlDdl.useIdentityType(IdType.IDENTITY), IdType.IDENTITY);
|
||||
assertEquals(mysqlDdl.useIdentityType(IdType.GENERATOR), IdType.GENERATOR);
|
||||
assertEquals(mysqlDdl.useIdentityType(IdType.EXTERNAL), IdType.EXTERNAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useIdentityType_oracle() {
|
||||
|
||||
assertEquals(oraDdl.useIdentityType(null), IdType.IDENTITY);
|
||||
assertEquals(oraDdl.useIdentityType(IdType.SEQUENCE), IdType.SEQUENCE);
|
||||
assertEquals(oraDdl.useIdentityType(IdType.IDENTITY), IdType.IDENTITY);
|
||||
assertEquals(oraDdl.useIdentityType(IdType.GENERATOR), IdType.GENERATOR);
|
||||
assertEquals(oraDdl.useIdentityType(IdType.EXTERNAL), IdType.EXTERNAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useIdentityType_hana() {
|
||||
|
||||
assertEquals(hanaDdl.useIdentityType(null), IdType.IDENTITY);
|
||||
assertEquals(hanaDdl.useIdentityType(IdType.SEQUENCE), IdType.IDENTITY);
|
||||
assertEquals(hanaDdl.useIdentityType(IdType.IDENTITY), IdType.IDENTITY);
|
||||
assertEquals(hanaDdl.useIdentityType(IdType.GENERATOR), IdType.GENERATOR);
|
||||
assertEquals(hanaDdl.useIdentityType(IdType.EXTERNAL), IdType.EXTERNAL);
|
||||
}
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.hana.HanaPlatform;
|
||||
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class PlatformDdl_dropUniqueConstraintTest {
|
||||
|
||||
private PlatformDdl h2Ddl = PlatformDdlBuilder.create(new H2Platform());
|
||||
private PlatformDdl pgDdl = PlatformDdlBuilder.create(new PostgresPlatform());
|
||||
private PlatformDdl mysqlDdl = PlatformDdlBuilder.create(new MySqlPlatform());
|
||||
private PlatformDdl oraDdl = PlatformDdlBuilder.create(new OraclePlatform());
|
||||
private PlatformDdl sqlServerDdl = PlatformDdlBuilder.create(new SqlServer17Platform());
|
||||
private PlatformDdl hanaDdl = PlatformDdlBuilder.create(new HanaPlatform());
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
String sql = h2Ddl.alterTableDropUniqueConstraint("mytab", "uq_name");
|
||||
assertEquals("alter table mytab drop constraint uq_name", sql);
|
||||
sql = pgDdl.alterTableDropUniqueConstraint("mytab", "uq_name");
|
||||
assertEquals("alter table mytab drop constraint uq_name", sql);
|
||||
sql = oraDdl.alterTableDropUniqueConstraint("mytab", "uq_name");
|
||||
assertEquals("alter table mytab drop constraint uq_name", sql);
|
||||
sql = sqlServerDdl.alterTableDropUniqueConstraint("mytab", "uq_name");
|
||||
assertEquals(
|
||||
"IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('mytab','U') AND name = 'uq_name') drop index uq_name ON mytab;\n"
|
||||
+ "IF (OBJECT_ID('uq_name', 'UQ') IS NOT NULL) alter table mytab drop constraint uq_name",
|
||||
sql);
|
||||
|
||||
sql = mysqlDdl.alterTableDropUniqueConstraint("mytab", "uq_name");
|
||||
assertEquals("alter table mytab drop index uq_name", sql);
|
||||
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
hanaDdl.configure(serverConfig);
|
||||
sql = hanaDdl.alterTableDropUniqueConstraint("mytab", "uq_name");
|
||||
assertEquals("delimiter $$\n" +
|
||||
"do\n" +
|
||||
"begin\n" +
|
||||
"declare exit handler for sql_error_code 397 begin end;\n" +
|
||||
"exec 'alter table mytab drop constraint uq_name';\n" +
|
||||
"end;\n" +
|
||||
"$$", sql);
|
||||
}
|
||||
|
||||
}
|
||||
-97
@@ -1,97 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.dbplatform.DbPlatformTypeMapping;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.util.PlatformTypeConverter;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PlatformTypeConverterTest {
|
||||
|
||||
@Test
|
||||
public void convert_withSuffix_expect_suffix() {
|
||||
|
||||
PostgresPlatform pg = new PostgresPlatform();
|
||||
DbPlatformTypeMapping dbTypeMap = pg.getDbTypeMap();
|
||||
|
||||
PlatformTypeConverter converter = new PlatformTypeConverter(dbTypeMap);
|
||||
|
||||
assertThat(converter.convert("varchar(10)")).isEqualTo("varchar(10)");
|
||||
assertThat(converter.convert("VARCHAR(10) default 'en' not null")).isEqualTo("varchar(10) default 'en' not null");
|
||||
assertThat(converter.convert("DECIMAL(10,2) DEFAULT '0.00' NOT NULL")).isEqualTo("decimal(10,2) DEFAULT '0.00' NOT NULL");
|
||||
assertThat(converter.convert("CRAZY(12,0) suffix")).isEqualTo("CRAZY(12,0) suffix");
|
||||
assertThat(converter.convert("CRAZY(12) suffix")).isEqualTo("CRAZY(12) suffix");
|
||||
assertThat(converter.convert("CRAZY suffix")).isEqualTo("CRAZY suffix");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvert_given_postgres() throws Exception {
|
||||
|
||||
PostgresPlatform pg = new PostgresPlatform();
|
||||
DbPlatformTypeMapping dbTypeMap = pg.getDbTypeMap();
|
||||
|
||||
PlatformTypeConverter converter = new PlatformTypeConverter(dbTypeMap);
|
||||
|
||||
assertThat(converter.convert("varchar(10)")).isEqualTo("varchar(10)");
|
||||
assertThat(converter.convert("decimal(10,2)")).isEqualTo("decimal(10,2)");
|
||||
assertThat(converter.convert("clob")).isEqualTo("text");
|
||||
assertThat(converter.convert("blob")).isEqualTo("bytea");
|
||||
assertThat(converter.convert("tinyint")).isEqualTo("smallint");
|
||||
assertThat(converter.convert("funky")).isEqualTo("funky"); // unknown
|
||||
assertThat(converter.convert("integer(8)")).isEqualTo("integer"); // removes scale
|
||||
|
||||
assertThat(converter.convert("funky(")).isEqualTo("funky(");
|
||||
assertThat(converter.convert("funky()")).isEqualTo("funky()");
|
||||
assertThat(converter.convert("funky)")).isEqualTo("funky)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvert_given_h2() throws Exception {
|
||||
|
||||
|
||||
H2Platform platform = new H2Platform();
|
||||
DbPlatformTypeMapping dbTypeMap = platform.getDbTypeMap();
|
||||
|
||||
PlatformTypeConverter converter = new PlatformTypeConverter(dbTypeMap);
|
||||
|
||||
assertThat(converter.convert("varchar(10)")).isEqualTo("varchar(10)");
|
||||
assertThat(converter.convert("decimal(10,2)")).isEqualTo("decimal(10,2)");
|
||||
assertThat(converter.convert("clob")).isEqualTo("clob");
|
||||
assertThat(converter.convert("blob")).isEqualTo("blob");
|
||||
assertThat(converter.convert("tinyint")).isEqualTo("tinyint");
|
||||
assertThat(converter.convert("integer(8)")).isEqualTo("integer(8)");
|
||||
assertThat(converter.convert("funky")).isEqualTo("funky"); // unknown
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertJsonTypes_given_postgres() {
|
||||
|
||||
PostgresPlatform platform = new PostgresPlatform();
|
||||
DbPlatformTypeMapping dbTypeMap = platform.getDbTypeMap();
|
||||
|
||||
PlatformTypeConverter converter = new PlatformTypeConverter(dbTypeMap);
|
||||
|
||||
assertThat(converter.convert("jsonblob")).isEqualTo("bytea");
|
||||
assertThat(converter.convert("jsonclob")).isEqualTo("text");
|
||||
assertThat(converter.convert("jsonvarchar(200)")).isEqualTo("varchar(200)");
|
||||
assertThat(converter.convert("json")).isEqualTo("json");
|
||||
assertThat(converter.convert("jsonb")).isEqualTo("jsonb");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertJsonTypes_given_h2() {
|
||||
|
||||
H2Platform platform = new H2Platform();
|
||||
DbPlatformTypeMapping dbTypeMap = platform.getDbTypeMap();
|
||||
|
||||
PlatformTypeConverter converter = new PlatformTypeConverter(dbTypeMap);
|
||||
|
||||
assertThat(converter.convert("jsonblob")).isEqualTo("blob");
|
||||
assertThat(converter.convert("jsonclob")).isEqualTo("clob");
|
||||
assertThat(converter.convert("jsonvarchar(200)")).isEqualTo("varchar(200)");
|
||||
assertThat(converter.convert("json")).isEqualTo("clob");
|
||||
assertThat(converter.convert("jsonb")).isEqualTo("clob");
|
||||
}
|
||||
}
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PostgresDdlTest {
|
||||
|
||||
private PostgresDdl postgresDdl = new PostgresDdl(new PostgresPlatform());
|
||||
|
||||
@Test
|
||||
public void setLockTimeout() {
|
||||
|
||||
final String sql = postgresDdl.setLockTimeout(5);
|
||||
assertThat(sql).isEqualTo("set lock_timeout = 5000");
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SqlServerHistoryDdlTest {
|
||||
|
||||
@Test
|
||||
public void getHistoryTable() {
|
||||
|
||||
SqlServerHistoryDdl ddl = new SqlServerHistoryDdl();
|
||||
assertThat(ddl.getHistoryTable("foo")).isEqualTo("dbo.foo_history");
|
||||
assertThat(ddl.getHistoryTable("bar.foo")).isEqualTo("bar.foo_history");
|
||||
assertThat(ddl.getHistoryTable("[Foo]")).isEqualTo("dbo.[Foo_history]");
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class VowelRemoverTest {
|
||||
|
||||
@Test
|
||||
public void testTrim() throws Exception {
|
||||
|
||||
|
||||
assertEquals("fk_abcd", VowelRemover.trim("fk_abcde", 4));
|
||||
assertEquals("fk_a", VowelRemover.trim("fk_aaaaaa", 4));
|
||||
assertEquals("ab_avrylngtblnm", VowelRemover.trim("ab_averylongtablename", 4));
|
||||
}
|
||||
}
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.migrationreader;
|
||||
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MigrationXmlWriterTest {
|
||||
|
||||
@Test
|
||||
public void testReadWrite() throws Exception {
|
||||
|
||||
Migration migration = MigrationXmlReader.read("/container/test-create-table.xml");
|
||||
assertThat(migration.getChangeSet()).hasSize(1);
|
||||
assertThat(migration.getChangeSet().get(0).getChangeSetChildren()).hasSize(3);
|
||||
|
||||
File temp = File.createTempFile("migrationWrite", ".xml");
|
||||
new MigrationXmlWriter("THIS IS A GENERATED FILE - DO NOT MODIFY").write(migration, temp);
|
||||
|
||||
Migration migrationRead = MigrationXmlReader.read(temp);
|
||||
assertThat(migrationRead.getChangeSet()).hasSize(1);
|
||||
assertThat(migrationRead.getChangeSet().get(0).getChangeSetChildren()).hasSize(3);
|
||||
|
||||
temp = File.createTempFile("migrationWrite", ".xml");
|
||||
new MigrationXmlWriter(null).write(migration, temp);
|
||||
|
||||
Migration migrationRead2 = MigrationXmlReader.read(temp);
|
||||
|
||||
assertThat(migrationRead2.getChangeSet()).hasSize(1);
|
||||
assertThat(migrationRead.getChangeSet().get(0).getChangeSetChildren()).hasSize(3);
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebean.migration.MigrationVersion;
|
||||
import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.migration.DropColumn;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class EntryTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void test_when_empty() throws Exception {
|
||||
|
||||
PendingDrops.Entry entry = createEntry();
|
||||
assertThat(entry.hasPendingDrops()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_when_normal() throws Exception {
|
||||
|
||||
PendingDrops.Entry entry = createEntry(new ChangeSet());
|
||||
|
||||
assertThat(entry.hasPendingDrops()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_when_suppressOnly() throws Exception {
|
||||
|
||||
ChangeSet cs = new ChangeSet();
|
||||
cs.setSuppressDropsForever(Boolean.TRUE);
|
||||
|
||||
PendingDrops.Entry entry = createEntry(cs);
|
||||
|
||||
assertThat(entry.hasPendingDrops()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_when_both() throws Exception {
|
||||
|
||||
ChangeSet cs = new ChangeSet();
|
||||
cs.setSuppressDropsForever(Boolean.TRUE);
|
||||
|
||||
PendingDrops.Entry entry = createEntry(cs, new ChangeSet());
|
||||
|
||||
assertThat(entry.hasPendingDrops()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_containsSuppressForever_when_empty() {
|
||||
|
||||
PendingDrops.Entry entry = createEntry();
|
||||
|
||||
assertThat(entry.containsSuppressForever()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_containsSuppressForever_when_notSuppress() {
|
||||
|
||||
PendingDrops.Entry entry = createEntry(new ChangeSet());
|
||||
|
||||
assertThat(entry.containsSuppressForever()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_containsSuppressForever_when_suppress() {
|
||||
|
||||
ChangeSet cs = new ChangeSet();
|
||||
cs.setSuppressDropsForever(Boolean.TRUE);
|
||||
PendingDrops.Entry entry = createEntry(cs);
|
||||
|
||||
assertThat(entry.containsSuppressForever()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_containsSuppressForever_when_mixed() {
|
||||
|
||||
ChangeSet cs = new ChangeSet();
|
||||
cs.setSuppressDropsForever(Boolean.TRUE);
|
||||
|
||||
PendingDrops.Entry entry = createEntry(cs, new ChangeSet());
|
||||
|
||||
assertThat(entry.containsSuppressForever()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeDrops_when_columnsMatch() {
|
||||
|
||||
ChangeSet pending = changeSet("one", "two");
|
||||
|
||||
PendingDrops.Entry entry = createEntry(pending);
|
||||
|
||||
assertThat(entry.removeDrops(changeSet("one", "two"))).isTrue();
|
||||
assertThat(entry.list).asList().doesNotContain(pending);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeDrops_when_subset() {
|
||||
|
||||
DropColumn dropColumnTwo = col("two");
|
||||
ChangeSet pending = changeSet("one");
|
||||
pending.getChangeSetChildren().add(dropColumnTwo);
|
||||
|
||||
PendingDrops.Entry entry = createEntry(pending);
|
||||
|
||||
assertThat(entry.removeDrops(changeSet("one"))).isFalse();
|
||||
assertThat(entry.list).asList().containsExactly(pending);
|
||||
assertThat(pending.getChangeSetChildren()).asList().containsExactly(dropColumnTwo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeDrops_when_columnsMatch_butSuppressed() {
|
||||
|
||||
ChangeSet pending = changeSet("one", "two");
|
||||
pending.setSuppressDropsForever(Boolean.TRUE);
|
||||
|
||||
PendingDrops.Entry entry = createEntry(pending);
|
||||
|
||||
assertThat(entry.removeDrops(changeSet("one", "two"))).isFalse();
|
||||
assertThat(entry.list).asList().contains(pending);
|
||||
assertThat(pending.getChangeSetChildren()).asList().hasSize(2);
|
||||
|
||||
}
|
||||
|
||||
static ChangeSet changeSet(String... colName) {
|
||||
|
||||
ChangeSet cs = new ChangeSet();
|
||||
for (String col : colName) {
|
||||
cs.getChangeSetChildren().add(col(col));
|
||||
}
|
||||
return cs;
|
||||
}
|
||||
|
||||
static DropColumn col(String colName) {
|
||||
DropColumn drop = new DropColumn();
|
||||
drop.setColumnName(colName);
|
||||
drop.setTableName("tab");
|
||||
return drop;
|
||||
}
|
||||
|
||||
static PendingDrops.Entry createEntry(ChangeSet... pending) {
|
||||
|
||||
PendingDrops.Entry entry = new PendingDrops.Entry(MigrationVersion.parse("1.1"));
|
||||
for (ChangeSet changeSet : pending) {
|
||||
entry.add(changeSet);
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
@@ -1,320 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class MColumnTest {
|
||||
|
||||
MTable table = new MTable("tab");
|
||||
|
||||
MColumn basic() {
|
||||
return new MColumn("col", "integer");
|
||||
}
|
||||
|
||||
ModelDiff diff() {
|
||||
return new ModelDiff();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noDiff() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
basic().compare(diff, table, basic());
|
||||
|
||||
assertThat(diff.getApplyChanges()).isEmpty();
|
||||
assertThat(diff.getDropChanges()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffType() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
basic().compare(diff, table, new MColumn("col", "integer(8)"));
|
||||
|
||||
assertChanges(diff);
|
||||
AlterColumn alterColumn = getAlterColumn(diff);
|
||||
assertThat(alterColumn.getType()).isEqualTo("integer(8)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffNotNull() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setNotnull(true);
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
AlterColumn alterColumn = getAlterColumn(diff);
|
||||
assertThat(alterColumn.isNotnull()).isEqualTo(true);
|
||||
|
||||
assertThat(alterColumn.getType()).isNull();
|
||||
assertThat(alterColumn.getUnique()).isNull();
|
||||
assertThat(alterColumn.getUniqueOneToOne()).isNull();
|
||||
assertThat(alterColumn.getDefaultValue()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffNull() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
|
||||
MColumn newCol = basic();
|
||||
newCol.setNotnull(false);
|
||||
|
||||
MColumn baseCol = basic();
|
||||
baseCol.setNotnull(true);
|
||||
|
||||
baseCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
AlterColumn alterColumn = getAlterColumn(diff);
|
||||
assertThat(alterColumn.isNotnull()).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyNotNull_expect_notNull() throws Exception {
|
||||
|
||||
MColumn newCol = basic();
|
||||
newCol.setNotnull(true);
|
||||
|
||||
AlterColumn alterColumn = new AlterColumn();
|
||||
alterColumn.setNotnull(Boolean.FALSE);
|
||||
newCol.apply(alterColumn);
|
||||
|
||||
assertThat(newCol.isNotnull()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffCheckAdd() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setCheckConstraint("abc");
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getCheckConstraint()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getDropCheckConstraint()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffCheckRemove() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setCheckConstraint("z");
|
||||
oldCol.setCheckConstraintName("abc");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getCheckConstraint()).isNull();
|
||||
assertThat(getAlterColumn(diff).getDropCheckConstraint()).isEqualTo("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffCheckChange() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setCheckConstraint("abc");
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setCheckConstraint("z");
|
||||
oldCol.setCheckConstraintName("d");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getCheckConstraint()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getDropCheckConstraint()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffDefaultValueAdd() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setDefaultValue("abc");
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getDefaultValue()).isEqualTo("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffDefaultValueRemove() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setDefaultValue("abc");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getDefaultValue()).isEqualTo("DROP DEFAULT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffDefaultValueChange() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setDefaultValue("abc");
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setDefaultValue("d");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getDefaultValue()).isEqualTo("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffReferencesAdd() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setReferences("abc");
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getReferences()).isEqualTo("abc");
|
||||
assertThat(getAlterColumn(diff).getDropForeignKey()).isNull();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void diffReferencesRemove() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setReferences("abc");
|
||||
oldCol.setForeignKeyName("fk_ab");
|
||||
oldCol.setForeignKeyIndex("ix_ab");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getReferences()).isNull();
|
||||
assertThat(getAlterColumn(diff).getForeignKeyName()).isNull();
|
||||
assertThat(getAlterColumn(diff).getForeignKeyIndex()).isNull();
|
||||
|
||||
assertThat(getAlterColumn(diff).getDropForeignKey()).isEqualTo("fk_ab");
|
||||
assertThat(getAlterColumn(diff).getDropForeignKeyIndex()).isEqualTo("ix_ab");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffReferencesChange() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setReferences("ab");
|
||||
newCol.setForeignKeyName("fk_ab");
|
||||
newCol.setForeignKeyIndex("ix_ab");
|
||||
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setReferences("d");
|
||||
oldCol.setForeignKeyName("fk_d");
|
||||
oldCol.setForeignKeyIndex("ix_d");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
|
||||
assertThat(getAlterColumn(diff).getReferences()).isEqualTo("ab");
|
||||
assertThat(getAlterColumn(diff).getForeignKeyName()).isEqualTo("fk_ab");
|
||||
assertThat(getAlterColumn(diff).getForeignKeyIndex()).isEqualTo("ix_ab");
|
||||
|
||||
assertThat(getAlterColumn(diff).getDropForeignKey()).isEqualTo("fk_d");
|
||||
assertThat(getAlterColumn(diff).getDropForeignKeyIndex()).isEqualTo("ix_d");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffUniqueAdd() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setUnique("uq_one");
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getUnique()).isEqualTo("uq_one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffUniqueRemove() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setUnique("uq_one");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getDropUnique()).isEqualTo("uq_one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffUniqueOneToOneAdd() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setUniqueOneToOne("uq_new");
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setUniqueOneToOne("uq_old");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getUniqueOneToOne()).isEqualTo("uq_new");
|
||||
assertThat(getAlterColumn(diff).getDropUnique()).isEqualTo("uq_old");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffUniqueOneToOneRemove() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setUniqueOneToOne("uq_new");
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).getDropUnique()).isEqualTo("uq_new");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffHistoryExcludeAdd() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
newCol.setHistoryExclude(true);
|
||||
basic().compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).isHistoryExclude()).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffHistoryExcludeRemove() throws Exception {
|
||||
|
||||
ModelDiff diff = diff();
|
||||
MColumn newCol = basic();
|
||||
MColumn oldCol = basic();
|
||||
oldCol.setHistoryExclude(true);
|
||||
oldCol.compare(diff, table, newCol);
|
||||
|
||||
assertChanges(diff);
|
||||
assertThat(getAlterColumn(diff).isHistoryExclude()).isEqualTo(false);
|
||||
}
|
||||
|
||||
private AlterColumn getAlterColumn(ModelDiff diff) {
|
||||
return (AlterColumn) diff.getApplyChanges().get(0);
|
||||
}
|
||||
|
||||
private void assertChanges(ModelDiff diff) {
|
||||
assertThat(diff.getDropChanges()).isEmpty();
|
||||
assertThat(diff.getApplyChanges()).hasSize(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebean.annotation.IdentityGenerated;
|
||||
import io.ebean.config.dbplatform.IdType;
|
||||
import io.ebeaninternal.dbmigration.migration.CreateTable;
|
||||
import io.ebeaninternal.dbmigration.migration.IdentityType;
|
||||
import io.ebeaninternal.server.deploy.IdentityMode;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployIdentityMode;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MTableIdentityTest {
|
||||
|
||||
@Test
|
||||
public void toCreateTable() {
|
||||
|
||||
IdentityMode mode = new IdentityMode(IdType.SEQUENCE, IdentityGenerated.BY_DEFAULT, 11, 12, 13, "foo_seq");
|
||||
CreateTable tab = new CreateTable();
|
||||
MTableIdentity.toCreateTable(mode, tab);
|
||||
|
||||
assertEquals(BigInteger.valueOf(11), tab.getIdentityStart());
|
||||
assertEquals(BigInteger.valueOf(12), tab.getIdentityIncrement());
|
||||
assertEquals(BigInteger.valueOf(13), tab.getIdentityCache());
|
||||
assertEquals("foo_seq", tab.getSequenceName());
|
||||
assertEquals("by_default", tab.getIdentityGenerated());
|
||||
assertEquals(IdentityType.SEQUENCE, tab.getIdentityType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCreateTable_when_auto() {
|
||||
|
||||
IdentityMode mode = new IdentityMode(IdType.AUTO, IdentityGenerated.AUTO, 11, 12, 13, "foo_seq");
|
||||
CreateTable tab = new CreateTable();
|
||||
MTableIdentity.toCreateTable(mode, tab);
|
||||
|
||||
assertNull(tab.getIdentityGenerated());
|
||||
assertNull(tab.getIdentityType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCreateTable_when_PlatformDefault_expect_typeNull() {
|
||||
|
||||
DeployIdentityMode deploy = DeployIdentityMode.auto();
|
||||
deploy.setPlatformType(IdType.SEQUENCE);
|
||||
|
||||
CreateTable tab = new CreateTable();
|
||||
MTableIdentity.toCreateTable(new IdentityMode(deploy), tab);
|
||||
|
||||
assertNull(tab.getIdentityType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCreateTable_when_notPlatformDefault_expectType() {
|
||||
|
||||
DeployIdentityMode deploy = DeployIdentityMode.auto();
|
||||
deploy.setIdType(IdType.SEQUENCE);
|
||||
|
||||
CreateTable tab = new CreateTable();
|
||||
MTableIdentity.toCreateTable(new IdentityMode(deploy), tab);
|
||||
|
||||
assertEquals(IdentityType.SEQUENCE, tab.getIdentityType());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void fromCreateTable_when_empty() {
|
||||
|
||||
CreateTable tab = new CreateTable();
|
||||
final IdentityMode mode = MTableIdentity.fromCreateTable(tab);
|
||||
assertEquals(IdType.AUTO, mode.getIdType());
|
||||
assertEquals(IdentityGenerated.AUTO, mode.getGenerated());
|
||||
assertEquals(0, mode.getStart());
|
||||
assertEquals(0, mode.getIncrement());
|
||||
assertEquals(0, mode.getCache());
|
||||
assertNull(mode.getSequenceName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromCreateTable_when_allSet() {
|
||||
|
||||
CreateTable tab = new CreateTable();
|
||||
tab.setIdentityType(IdentityType.SEQUENCE);
|
||||
tab.setIdentityGenerated("always");
|
||||
tab.setSequenceName("foo_seq");
|
||||
tab.setIdentityStart(BigInteger.valueOf(5));
|
||||
tab.setIdentityIncrement(BigInteger.valueOf(7));
|
||||
tab.setIdentityCache(BigInteger.valueOf(9));
|
||||
|
||||
final IdentityMode mode = MTableIdentity.fromCreateTable(tab);
|
||||
assertEquals(IdType.SEQUENCE, mode.getIdType());
|
||||
assertEquals(IdentityGenerated.ALWAYS, mode.getGenerated());
|
||||
assertEquals(5, mode.getStart());
|
||||
assertEquals(7, mode.getIncrement());
|
||||
assertEquals(9, mode.getCache());
|
||||
assertEquals("foo_seq", mode.getSequenceName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromCreateTable_expect_useNewPropertiesWin() {
|
||||
|
||||
CreateTable tab = new CreateTable();
|
||||
// old properties lose
|
||||
tab.setSequenceInitial(BigInteger.valueOf(42));
|
||||
tab.setSequenceAllocate(BigInteger.valueOf(43));
|
||||
|
||||
// new properties win
|
||||
tab.setIdentityStart(BigInteger.valueOf(5));
|
||||
tab.setIdentityIncrement(BigInteger.valueOf(7));
|
||||
|
||||
final IdentityMode mode = MTableIdentity.fromCreateTable(tab);
|
||||
assertEquals(5, mode.getStart());
|
||||
assertEquals(7, mode.getIncrement());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromCreateTable_when_seqInitAndSeqAll() {
|
||||
|
||||
CreateTable tab = new CreateTable();
|
||||
tab.setSequenceInitial(BigInteger.valueOf(42));
|
||||
tab.setSequenceAllocate(BigInteger.valueOf(43));
|
||||
|
||||
final IdentityMode mode = MTableIdentity.fromCreateTable(tab);
|
||||
assertEquals(42, mode.getStart());
|
||||
assertEquals(43, mode.getIncrement());
|
||||
}
|
||||
}
|
||||
@@ -1,239 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebeaninternal.dbmigration.migration.AddColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.DropColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.migration.DropTable;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class MTableTest {
|
||||
|
||||
static MTable base() {
|
||||
MTable table = new MTable("tab");
|
||||
table.addColumn(new MColumn("id", "bigint"));
|
||||
table.addColumn(new MColumn("name", "varchar(20)"));
|
||||
table.addColumn(new MColumn("status", "varchar(3)"));
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
static MTable newTable() {
|
||||
MTable table = new MTable("tab");
|
||||
table.addColumn(new MColumn("id", "bigint"));
|
||||
table.addColumn(new MColumn("name", "varchar(20)"));
|
||||
table.addColumn(new MColumn("comment", "varchar(1000)"));
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
static MTable newTableAdd2Columns() {
|
||||
MTable table = new MTable("tab");
|
||||
table.addColumn(new MColumn("id", "bigint"));
|
||||
table.addColumn(new MColumn("name", "varchar(20)"));
|
||||
table.addColumn(new MColumn("status", "varchar(3)"));
|
||||
table.addColumn(new MColumn("comment", "varchar(1000)"));
|
||||
table.addColumn(new MColumn("note", "varchar(2000)"));
|
||||
return table;
|
||||
}
|
||||
|
||||
static MTable newTableModifiedColumn() {
|
||||
MColumn modCol = new MColumn("name", "varchar(30)");// modified type
|
||||
modCol.setNotnull(true);
|
||||
|
||||
MTable table = new MTable("tab");
|
||||
table.addColumn(modCol);
|
||||
table.addColumn(new MColumn("id", "bigint"));
|
||||
table.addColumn(new MColumn("status", "varchar(3)"));
|
||||
return table;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void schema() {
|
||||
MTable table = new MTable("tab");
|
||||
assertNull(table.getSchema());
|
||||
|
||||
table = new MTable("foo.tab");
|
||||
assertEquals("foo", table.getSchema());
|
||||
assertEquals("foo.tab", table.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_allHistoryColumns() throws Exception {
|
||||
|
||||
MTable base = base();
|
||||
base.registerPendingDropColumn("fullName");
|
||||
base.registerPendingDropColumn("last");
|
||||
|
||||
assertThat(base.allHistoryColumns(false)).containsExactly("id", "name", "status");
|
||||
assertThat(base.allHistoryColumns(true)).containsExactly("id", "name", "status", "fullName", "last");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_dropTable() {
|
||||
|
||||
MTable base = base();
|
||||
DropTable dropTable = base.dropTable();
|
||||
assertThat(dropTable.getName()).isEqualTo(base.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compare_addColumnDropColumn() throws Exception {
|
||||
|
||||
ModelDiff diff = new ModelDiff();
|
||||
diff.compareTables(base(), newTable());
|
||||
|
||||
List<Object> createChanges = diff.getApplyChanges();
|
||||
assertThat(createChanges).hasSize(1);
|
||||
AddColumn addColumn = (AddColumn) createChanges.get(0);
|
||||
assertThat(addColumn.getColumn()).extracting("name").contains("comment");
|
||||
assertThat(addColumn.getColumn()).extracting("type").contains("varchar(1000)");
|
||||
|
||||
List<Object> dropChanges = diff.getDropChanges();
|
||||
assertThat(dropChanges).hasSize(1);
|
||||
|
||||
DropColumn dropColumn = (DropColumn) dropChanges.get(0);
|
||||
assertThat(dropColumn.getColumnName()).isEqualTo("status");
|
||||
assertThat(dropColumn.getTableName()).isEqualTo("tab");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compare_addTwoColumnsToSameTable() throws Exception {
|
||||
|
||||
ModelDiff diff = new ModelDiff();
|
||||
diff.compareTables(base(), newTableAdd2Columns());
|
||||
|
||||
List<Object> createChanges = diff.getApplyChanges();
|
||||
assertThat(createChanges).hasSize(1);
|
||||
|
||||
AddColumn addColumn = (AddColumn) createChanges.get(0);
|
||||
assertThat(addColumn.getColumn()).extracting("name").contains("comment", "note");
|
||||
assertThat(addColumn.getColumn()).extracting("type").contains("varchar(1000)", "varchar(2000)");
|
||||
|
||||
assertThat(diff.getDropChanges()).hasSize(0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compare_modifyColumn() throws Exception {
|
||||
|
||||
ModelDiff diff = new ModelDiff();
|
||||
diff.compareTables(base(), newTableModifiedColumn());
|
||||
|
||||
List<Object> createChanges = diff.getApplyChanges();
|
||||
assertThat(createChanges).hasSize(1);
|
||||
|
||||
AlterColumn alterColumn = (AlterColumn) createChanges.get(0);
|
||||
assertThat(alterColumn.getColumnName()).isEqualTo("name");
|
||||
assertThat(alterColumn.getType()).isEqualTo("varchar(30)");
|
||||
assertThat(alterColumn.isNotnull()).isEqualTo(true);
|
||||
assertThat(alterColumn.getUnique()).isNull();
|
||||
assertThat(alterColumn.getCheckConstraint()).isNull();
|
||||
assertThat(alterColumn.getReferences()).isNull();
|
||||
|
||||
assertThat(diff.getDropChanges()).hasSize(0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_apply_dropColumn() {
|
||||
|
||||
MTable base = base();
|
||||
|
||||
DropColumn dropColumn = new DropColumn();
|
||||
dropColumn.setTableName("tab");
|
||||
dropColumn.setColumnName("name");
|
||||
|
||||
base.apply(dropColumn);
|
||||
assertThat(base.getColumn("name")).isNull();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void test_apply_dropColumn_doesNotExist() {
|
||||
|
||||
MTable base = base();
|
||||
|
||||
DropColumn dropColumn = new DropColumn();
|
||||
dropColumn.setTableName(base.getName());
|
||||
dropColumn.setColumnName("DoesNotExist");
|
||||
base.apply(dropColumn);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void test_apply_alterColumn_doesNotExist() {
|
||||
|
||||
MTable base = base();
|
||||
|
||||
AlterColumn alterColumn = new AlterColumn();
|
||||
alterColumn.setTableName(base.getName());
|
||||
alterColumn.setColumnName("DoesNotExist");
|
||||
alterColumn.setType("integer");
|
||||
base.apply(alterColumn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_apply_alterColumn_type() {
|
||||
|
||||
MTable base = base();
|
||||
|
||||
AlterColumn alterColumn = new AlterColumn();
|
||||
alterColumn.setTableName(base.getName());
|
||||
alterColumn.setColumnName("id");
|
||||
alterColumn.setType("uuid");
|
||||
base.apply(alterColumn);
|
||||
|
||||
assertThat(base.getColumn("id").getType()).isEqualTo("uuid");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compare_addAndDropColumn() throws Exception {
|
||||
|
||||
MTable base = base();
|
||||
MTable newTable = newTable();
|
||||
|
||||
ModelDiff diff = new ModelDiff();
|
||||
base.compare(diff, newTable);
|
||||
|
||||
assertThat(diff.getApplyChanges()).hasSize(1);
|
||||
assertThat(diff.getDropChanges()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compare_addHistoryToTable() {
|
||||
|
||||
MTable base = base();
|
||||
MTable withHistory = base();
|
||||
withHistory.setWithHistory(true);
|
||||
|
||||
ModelDiff diff = new ModelDiff();
|
||||
base.compare(diff, withHistory);
|
||||
|
||||
assertThat(diff.getDropChanges()).isEmpty();
|
||||
assertThat(diff.getApplyChanges()).hasSize(1);
|
||||
assertThat(diff.getApplyChanges().get(0)).isInstanceOf(AddHistoryTable.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compare_removeHistoryFromTable() throws Exception {
|
||||
|
||||
MTable withHistory = base();
|
||||
withHistory.setWithHistory(true);
|
||||
|
||||
MTable noHistory = base();
|
||||
|
||||
ModelDiff diff = new ModelDiff();
|
||||
withHistory.compare(diff, noHistory);
|
||||
|
||||
assertThat(diff.getApplyChanges()).isEmpty();
|
||||
assertThat(diff.getDropChanges()).hasSize(1);
|
||||
assertThat(diff.getDropChanges().get(0)).isInstanceOf(DropHistoryTable.class);
|
||||
}
|
||||
|
||||
}
|
||||
-180
@@ -1,180 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import io.ebean.migration.MigrationVersion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
// FIXME: this test should be moved to ebean-migration artifact
|
||||
public class MigrationVersionTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void sort() {
|
||||
|
||||
List<MigrationVersion> list = new ArrayList<>();
|
||||
list.add(MigrationVersion.parse("1.1__point"));
|
||||
list.add(MigrationVersion.parse("3.0__three"));
|
||||
list.add(MigrationVersion.parse("1.0__init"));
|
||||
list.add(MigrationVersion.parse("R__beta"));
|
||||
list.add(MigrationVersion.parse("R__alpha"));
|
||||
|
||||
Collections.sort(list);
|
||||
|
||||
assertThat(list.get(0).getComment()).isEqualTo("init");
|
||||
assertThat(list.get(1).getComment()).isEqualTo("point");
|
||||
assertThat(list.get(2).getComment()).isEqualTo("three");
|
||||
assertThat(list.get(3).getComment()).isEqualTo("alpha");
|
||||
assertThat(list.get(4).getComment()).isEqualTo("beta");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_parse_hyphenSnapshot() {
|
||||
|
||||
MigrationVersion version = MigrationVersion.parse("0.1.1-SNAPSHOT");
|
||||
assertThat(version.normalised()).isEqualTo("0.1.1");
|
||||
assertThat(version.getComment()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_parse_hyphenSnapshot_when_underscores() {
|
||||
|
||||
MigrationVersion version = MigrationVersion.parse("0_1_1-SNAPSHOT__Foo");
|
||||
assertThat(version.normalised()).isEqualTo("0.1.1");
|
||||
assertThat(version.getComment()).isEqualTo("Foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_parse_when_repeatable() throws Exception {
|
||||
|
||||
MigrationVersion version = MigrationVersion.parse("R__Foo");
|
||||
assertThat(version.getComment()).isEqualTo("Foo");
|
||||
assertThat(version.normalised()).isEqualTo("R");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_parse_when_repeatable_case() throws Exception {
|
||||
|
||||
MigrationVersion version = MigrationVersion.parse("r__Foo");
|
||||
assertThat(version.isRepeatable()).isTrue();
|
||||
assertThat(version.getComment()).isEqualTo("Foo");
|
||||
assertThat(version.normalised()).isEqualTo("R");
|
||||
assertThat(version.normalised()).isEqualTo("R");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_parse_when_v_prefix() throws Exception {
|
||||
|
||||
MigrationVersion version = MigrationVersion.parse("v1_0__Foo");
|
||||
assertThat(version.isRepeatable()).isFalse();
|
||||
assertThat(version.getComment()).isEqualTo("Foo");
|
||||
assertThat(version.normalised()).isEqualTo("1.0");
|
||||
assertThat(version.asString()).isEqualTo("1_0");
|
||||
assertThat(version.getRaw()).isEqualTo("1_0__Foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void repeatable_compareTo() throws Exception {
|
||||
|
||||
MigrationVersion foo = MigrationVersion.parse("R__Foo");
|
||||
MigrationVersion bar = MigrationVersion.parse("R__Bar");
|
||||
assertThat(foo.compareTo(bar)).isGreaterThan(0);
|
||||
assertThat(bar.compareTo(foo)).isLessThan(0);
|
||||
|
||||
MigrationVersion bar2 = MigrationVersion.parse("R__Bar");
|
||||
assertThat(bar.compareTo(bar2)).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void repeatable_compareTo_when_caseDifferent() throws Exception {
|
||||
|
||||
MigrationVersion none = MigrationVersion.parse("R__");
|
||||
MigrationVersion bar = MigrationVersion.parse("R__Bar");
|
||||
MigrationVersion bar2 = MigrationVersion.parse("R__bar");
|
||||
assertThat(none.compareTo(bar)).isLessThan(0);
|
||||
assertThat(bar.compareTo(bar2)).isLessThan(0);
|
||||
assertThat(none.compareTo(bar2)).isLessThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_parse_getComment() throws Exception {
|
||||
|
||||
assertThat(MigrationVersion.parse("1.1.1_2__Foo").getComment()).isEqualTo("Foo");
|
||||
assertThat(MigrationVersion.parse("1.1.1.2__junk").getComment()).isEqualTo("junk");
|
||||
assertThat(MigrationVersion.parse("1.1_1.2_foo").getComment()).isEqualTo("");
|
||||
assertThat(MigrationVersion.parse("1.1_1.2_d").getComment()).isEqualTo("");
|
||||
assertThat(MigrationVersion.parse("1.1_1.2_").getComment()).isEqualTo("");
|
||||
assertThat(MigrationVersion.parse("1.1_1.2").getComment()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_nextVersion_expect_preserveUnderscores() {
|
||||
|
||||
assertThat(MigrationVersion.parse("2").nextVersion()).isEqualTo("3");
|
||||
assertThat(MigrationVersion.parse("1.0").nextVersion()).isEqualTo("1.1");
|
||||
assertThat(MigrationVersion.parse("2.0.b34").nextVersion()).isEqualTo("2.1");
|
||||
assertThat(MigrationVersion.parse("1.1.1_2__Foo").nextVersion()).isEqualTo("1.1.1_3");
|
||||
assertThat(MigrationVersion.parse("1.1.1.2_junk").nextVersion()).isEqualTo("1.1.1.3");
|
||||
assertThat(MigrationVersion.parse("1_2.3_4__Foo").nextVersion()).isEqualTo("1_2.3_5");
|
||||
assertThat(MigrationVersion.parse("1_2.3_4_").nextVersion()).isEqualTo("1_2.3_5");
|
||||
assertThat(MigrationVersion.parse("1_2_3_4__Foo").nextVersion()).isEqualTo("1_2_3_5");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_normalised_expect_periods() {
|
||||
|
||||
assertThat(MigrationVersion.parse("2").normalised()).isEqualTo("2");
|
||||
assertThat(MigrationVersion.parse("1.0").normalised()).isEqualTo("1.0");
|
||||
assertThat(MigrationVersion.parse("2.0.b34").normalised()).isEqualTo("2.0");
|
||||
assertThat(MigrationVersion.parse("1.1.1_2__Foo").normalised()).isEqualTo("1.1.1.2");
|
||||
assertThat(MigrationVersion.parse("1.1.1.2_junk").normalised()).isEqualTo("1.1.1.2");
|
||||
assertThat(MigrationVersion.parse("1_2.3_4__Foo").normalised()).isEqualTo("1.2.3.4");
|
||||
assertThat(MigrationVersion.parse("1_2.3_4_").normalised()).isEqualTo("1.2.3.4");
|
||||
assertThat(MigrationVersion.parse("1_2_3_4__Foo").normalised()).isEqualTo("1.2.3.4");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compareTo_isEqual() throws Exception {
|
||||
|
||||
MigrationVersion v0 = MigrationVersion.parse("1.1.1_2__Foo");
|
||||
MigrationVersion v1 = MigrationVersion.parse("1.1.1.2_junk");
|
||||
MigrationVersion v2 = MigrationVersion.parse("1.1_1.2_foo");
|
||||
MigrationVersion v3 = MigrationVersion.parse("1.1_1.2__foo");
|
||||
|
||||
// same version number, but not equal. It prints out the warning:
|
||||
// The migrationscript '1.1.1.1_junk' contains non numeric version part.
|
||||
// This may lead to misordered version scripts.
|
||||
assertThat(v0.compareTo(v1)).isEqualTo(0);
|
||||
assertThat(v0).isNotEqualTo(v1);
|
||||
assertThat(v1.compareTo(v0)).isEqualTo(0);
|
||||
assertThat(v1).isNotEqualTo(v0);
|
||||
assertThat(v1.compareTo(v2)).isEqualTo(0);
|
||||
assertThat(v1).isNotEqualTo(v2);
|
||||
|
||||
assertThat(v0.compareTo(v3)).isEqualTo(0);
|
||||
assertThat(v0).isNotEqualTo(v3);
|
||||
assertThat(v3.compareTo(v0)).isEqualTo(0);
|
||||
assertThat(v3).isNotEqualTo(v0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_compareTo() throws Exception {
|
||||
|
||||
MigrationVersion v0 = MigrationVersion.parse("1.1.1.1_junk");
|
||||
MigrationVersion v1 = MigrationVersion.parse("1.1.1.2_junk");
|
||||
MigrationVersion v2 = MigrationVersion.parse("1.1_1.3_junk");
|
||||
MigrationVersion v3 = MigrationVersion.parse("1.2_1.2_junk");
|
||||
MigrationVersion v4 = MigrationVersion.parse("2.1_1.2_junk");
|
||||
|
||||
assertThat(v1.compareTo(v0)).isEqualTo(1);
|
||||
|
||||
assertThat(v1.compareTo(v2)).isEqualTo(-1);
|
||||
assertThat(v1.compareTo(v3)).isEqualTo(-1);
|
||||
assertThat(v1.compareTo(v4)).isEqualTo(-1);
|
||||
}
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebean.migration.MigrationVersion;
|
||||
import io.ebeaninternal.dbmigration.migration.AddColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.migration.CreateTable;
|
||||
import io.ebeaninternal.dbmigration.migration.DropColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModelContainerApplyTest {
|
||||
|
||||
@Test
|
||||
public void testApply() {
|
||||
|
||||
Migration migration = MigrationXmlReader.read("/container/test-create-table.xml");
|
||||
|
||||
List<ChangeSet> changeSets = migration.getChangeSet();
|
||||
ChangeSet changeSet = changeSets.get(0);
|
||||
|
||||
List<Object> changeSetChildren = changeSet.getChangeSetChildren();
|
||||
assertThat(changeSetChildren).hasSize(3);
|
||||
assertThat(changeSetChildren.get(0)).isInstanceOf(CreateTable.class);
|
||||
assertThat(changeSetChildren.get(1)).isInstanceOf(AddColumn.class);
|
||||
assertThat(changeSetChildren.get(2)).isInstanceOf(DropColumn.class);
|
||||
|
||||
ModelContainer model = new ModelContainer();
|
||||
model.apply(migration, MigrationVersion.parse("1.1"));
|
||||
|
||||
MTable foo = model.getTable("foo");
|
||||
assertThat(foo.getComment()).isEqualTo("comment");
|
||||
assertThat(foo.getTablespace()).isEqualTo("fooSpace");
|
||||
assertThat(foo.getIndexTablespace()).isEqualTo("fooIndexSpace");
|
||||
assertThat(foo.isWithHistory()).isEqualTo(false);
|
||||
assertThat(foo.allColumns()).extracting("name").contains("col1", "col3", "added_to_foo");
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
|
||||
import io.ebean.migration.MigrationVersion;
|
||||
import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.migration.DropIndex;
|
||||
import io.ebeaninternal.dbmigration.migration.DropTable;
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModelContainerTest {
|
||||
|
||||
@Test
|
||||
public void elementCollectionTable_single_expect_foreignKeys() {
|
||||
|
||||
ModelContainer container = new ModelContainer();
|
||||
|
||||
container.addTableElementCollection(createElementCollectionTable("ec_bean.id", "fk_ec_bean_ec_table"));
|
||||
|
||||
final MTable ecTable = container.getTable("ec_table");
|
||||
final MColumn fkCol = ecTable.getColumn("fk_col");
|
||||
assertThat(fkCol.getReferences()).isEqualTo("ec_bean.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void elementCollectionTable_reused_expect_noForeignKeys() {
|
||||
|
||||
ModelContainer container = new ModelContainer();
|
||||
|
||||
container.addTableElementCollection(createElementCollectionTable("ec_bean.id", "fk_ec_bean_ec_table"));
|
||||
container.addTableElementCollection(createElementCollectionTable("ec_otherBean.id", "fk_ec_otherBean_ec_table"));
|
||||
|
||||
final MTable ecTable = container.getTable("ec_table");
|
||||
final MColumn fkCol = ecTable.getColumn("fk_col");
|
||||
assertThat(fkCol.getReferences()).isNull();
|
||||
assertThat(fkCol.getForeignKeyName()).isNull();
|
||||
|
||||
final MIndex index = container.getIndex(new MIndex("ix_ec_table", "ec_table", "foo"));
|
||||
assertThat(index.getTableName()).isEqualTo("ec_table");
|
||||
assertThat(index.getColumns()).containsOnly("fk_col");
|
||||
}
|
||||
|
||||
private MTable createElementCollectionTable(String references, String fkName) {
|
||||
|
||||
MTable ecTable = new MTable("ec_table");
|
||||
final MColumn colFk = new MColumn("fk_col", "varchar", true);
|
||||
colFk.setReferences(references);
|
||||
colFk.setForeignKeyName(fkName);
|
||||
colFk.setForeignKeyIndex("ix_ec_table");
|
||||
|
||||
ecTable.addColumn(colFk);
|
||||
ecTable.addColumn(new MColumn("key", "varchar", true));
|
||||
ecTable.addColumn(new MColumn("val", "varchar", true));
|
||||
|
||||
return ecTable;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_when_noPendingDrops_then_emptyPending() {
|
||||
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.apply(mig("1.0.model.xml"), ver("1.1"));
|
||||
assertThat(container.getPendingDrops()).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void apply_when_pendingDrops_then_registeredHistoryTable() {
|
||||
|
||||
ModelContainer base = container_1_1();
|
||||
|
||||
MTable table = base.getTable("document");
|
||||
assertThat(table.allHistoryColumns(true)).doesNotContain("zing");
|
||||
|
||||
container_1_1().registerPendingHistoryDropColumns(base);
|
||||
assertThat(table.allHistoryColumns(true)).contains("zing");
|
||||
assertThat(table.allHistoryColumns(false)).doesNotContain("zing");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void apply_when_pendingDropsApplied_then_droppedTableNotInHistory() {
|
||||
|
||||
ModelContainer container = container_1_1();
|
||||
container.apply(mig("1.1_2__drops.model.xml"), ver("1.1_2"));
|
||||
|
||||
ModelContainer base = container_1_1();
|
||||
|
||||
container.registerPendingHistoryDropColumns(base);
|
||||
|
||||
assertThat(base.getTable("document").allHistoryColumns(false)).doesNotContain("zing");
|
||||
assertThat(base.getTable("document").allHistoryColumns(true)).doesNotContain("zing");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_when_apply_partial_pendingDrops_then_some_remainder() {
|
||||
|
||||
ModelContainer container = container_2_1();
|
||||
container.apply(mig("2.2__drops.model.xml"), ver("2.2"));
|
||||
|
||||
ModelContainer base = container_2_1();
|
||||
|
||||
container.registerPendingHistoryDropColumns(base);
|
||||
|
||||
List<String> normalColumns = base.getTable("document").allHistoryColumns(false);
|
||||
List<String> historyColumns = base.getTable("document").allHistoryColumns(true);
|
||||
|
||||
assertThat(historyColumns).contains("zong", "boom", "baz", "bar");
|
||||
assertThat(normalColumns).doesNotContain("zing", "zong", "boom", "baz", "bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_sql() {
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.apply(mig("3.0__rawSql.model.xml"), ver("3.0"));
|
||||
assertThat(container.getPendingDrops()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_alterForeignKey() {
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.apply(mig("4.0__alterForeignKey.model.xml"), ver("4.0"));
|
||||
assertThat(container.getPendingDrops()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_dropTable_when_notInModel_then_ok() {
|
||||
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.apply(mig("5.0__dropTable.model.xml"), ver("5.0"));
|
||||
assertThat(container.getTables()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_drop_when_notInModel_then_ok() {
|
||||
|
||||
ModelContainer container = new ModelContainer();
|
||||
DropTable dropTable = new DropTable();
|
||||
dropTable.setName("DoesNotExist");
|
||||
container.applyChange(dropTable);
|
||||
|
||||
DropIndex dropIndex = new DropIndex();
|
||||
dropIndex.setIndexName("DoesNotExist");
|
||||
container.applyChange(dropIndex);
|
||||
|
||||
DropHistoryTable dropHistoryTable = new DropHistoryTable();
|
||||
dropHistoryTable.setBaseTable("DoesNotExist");
|
||||
container.applyChange(dropHistoryTable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_renameColumn() {
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.apply(mig("6.0__renameColumn.model.xml"), ver("6.0"));
|
||||
|
||||
final MTable table = container.getTable("document");
|
||||
assertThat(table.getColumn("title")).isNotNull();
|
||||
assertThat(table.getColumn("short_title")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSchemas() {
|
||||
|
||||
MTable t0 = new MTable("foo.one");
|
||||
MTable t1 = new MTable("foo.two");
|
||||
MTable t2 = new MTable("bar.three");
|
||||
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.addTable(t0);
|
||||
container.addTable(t1);
|
||||
container.addTable(t2);
|
||||
|
||||
final Set<String> schemas = container.getSchemas();
|
||||
assertThat(schemas).containsExactly("bar","foo");
|
||||
}
|
||||
|
||||
private ModelContainer container_2_1() {
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.apply(mig("2.0.model.xml"), ver("2.0"));
|
||||
container.apply(mig("2.1.model.xml"), ver("2.1"));
|
||||
return container;
|
||||
}
|
||||
|
||||
private ModelContainer container_1_1() {
|
||||
ModelContainer container = new ModelContainer();
|
||||
container.apply(mig("1.0.model.xml"), ver("1.0"));
|
||||
container.apply(mig("1.1.model.xml"), ver("1.1"));
|
||||
return container;
|
||||
}
|
||||
|
||||
private MigrationVersion ver(String version) {
|
||||
return MigrationVersion.parse(version);
|
||||
}
|
||||
|
||||
private Migration mig(String path) {
|
||||
return MigrationXmlReader.read(ModelContainerTest.class.getResourceAsStream(path));
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class ModelDiffTest {
|
||||
|
||||
@Test
|
||||
public void test_compareTo_with_dropColumnOnHistoryTable_then_historyColumnRegistered() throws Exception {
|
||||
|
||||
ModelContainer base = new ModelContainer();
|
||||
base.addTable(MTableTest.base().setWithHistory(true));
|
||||
|
||||
ModelContainer newModel = new ModelContainer();
|
||||
newModel.addTable(MTableTest.newTable().setWithHistory(true));
|
||||
|
||||
ModelDiff diff = new ModelDiff(base);
|
||||
diff.compareTo(newModel);
|
||||
|
||||
MTable tab = newModel.getTable("tab");
|
||||
|
||||
assertThat(tab.allHistoryColumns(true)).contains("status");
|
||||
assertThat(tab.allColumns()).extracting("name").doesNotContain("status");
|
||||
}
|
||||
}
|
||||
@@ -1,247 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebean.migration.MigrationVersion;
|
||||
import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.migration.DropColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class PendingDropsTest {
|
||||
|
||||
static final MigrationVersion V1_1 = MigrationVersion.parse("1.1");
|
||||
|
||||
static final MigrationVersion V1_2 = MigrationVersion.parse("1.2");
|
||||
|
||||
@Test
|
||||
public void test_add() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
pendingDrops.add(V1_1, new ChangeSet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_appliedDropsFor_when_matchesSome_then_removesMatched() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
|
||||
DropColumn one = col("one");
|
||||
DropColumn two = col("two");
|
||||
pendingDrops.add(V1_1, changeSet(one, two));
|
||||
pendingDrops.add(V1_1, changeSet("three", "four"));
|
||||
assertThat(pendingDrops.testGetEntryFor(V1_1).list).asList().hasSize(2);
|
||||
|
||||
ChangeSet applied = changeSet("two");
|
||||
applied.setDropsFor("1.1");
|
||||
|
||||
assertThat(pendingDrops.appliedDropsFor(applied)).isFalse();
|
||||
assertThat(pendingDrops.testGetEntryFor(V1_1).list).asList().hasSize(2);
|
||||
assertThat(pendingDrops.testGetEntryFor(V1_1).list.get(0).getChangeSetChildren()).asList().containsExactly(one);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_appliedDropsFor_when_matchesAll_then_removesChangeSet() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
|
||||
DropColumn one = col("one");
|
||||
DropColumn two = col("two");
|
||||
pendingDrops.add(V1_1, changeSet(one, two));
|
||||
pendingDrops.add(V1_1, changeSet("three", "four"));
|
||||
assertThat(pendingDrops.testGetEntryFor(V1_1).list).asList().hasSize(2);
|
||||
|
||||
ChangeSet applied = changeSet("two", "one");
|
||||
applied.setDropsFor("1.1");
|
||||
|
||||
assertThat(pendingDrops.appliedDropsFor(applied)).isFalse();
|
||||
assertThat(pendingDrops.testGetEntryFor(V1_1).list).asList().hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_appliedDropsFor_when_changeSetSuppressed_isIgnored() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
|
||||
DropColumn one = col("one");
|
||||
ChangeSet changeSet = changeSet(one);
|
||||
changeSet.setSuppressDropsForever(true);
|
||||
pendingDrops.add(V1_1, changeSet);
|
||||
|
||||
ChangeSet applied = changeSet("one");
|
||||
applied.setDropsFor("1.1");
|
||||
|
||||
assertThat(pendingDrops.appliedDropsFor(applied)).isFalse();
|
||||
assertThat(pendingDrops.testGetEntryFor(V1_1).list).asList().hasSize(1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test_pendingDrops() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
assertThat(pendingDrops.pendingDrops()).isEmpty();
|
||||
|
||||
pendingDrops.add(V1_1, new ChangeSet());
|
||||
pendingDrops.add(V1_2, new ChangeSet());
|
||||
assertThat(pendingDrops.pendingDrops()).containsExactly("1.1", "1.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_pendingDrops_when_suppressForever() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
assertThat(pendingDrops.pendingDrops()).isEmpty();
|
||||
|
||||
pendingDrops.add(V1_1, newSuppressForeverChangeSet());
|
||||
assertThat(pendingDrops.pendingDrops()).isEmpty();
|
||||
|
||||
pendingDrops.add(V1_2, new ChangeSet());
|
||||
assertThat(pendingDrops.pendingDrops()).containsExactly("1.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_pendingDrops_when_both() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
assertThat(pendingDrops.pendingDrops()).isEmpty();
|
||||
|
||||
pendingDrops.add(V1_1, newSuppressForeverChangeSet());
|
||||
pendingDrops.add(V1_1, new ChangeSet());
|
||||
assertThat(pendingDrops.pendingDrops()).containsExactly("1.1");
|
||||
|
||||
pendingDrops.add(V1_2, new ChangeSet());
|
||||
assertThat(pendingDrops.pendingDrops()).containsExactly("1.1", "1.2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_migrationForVersion() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
|
||||
ChangeSet applyDropChangeSet1 = new ChangeSet();
|
||||
ChangeSet applyDropChangeSet2 = new ChangeSet();
|
||||
|
||||
MigrationVersion version = V1_1;
|
||||
pendingDrops.add(version, applyDropChangeSet1);
|
||||
pendingDrops.add(version, applyDropChangeSet2);
|
||||
|
||||
Migration migration = pendingDrops.migrationForVersion("1_1");
|
||||
assertThat(migration.getChangeSet()).containsExactly(applyDropChangeSet1, applyDropChangeSet2);
|
||||
|
||||
assertThat(pendingDrops.testContainsEntryFor(version)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_migrationForVersion_when_both() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
|
||||
ChangeSet applyDropChangeSet = new ChangeSet();
|
||||
MigrationVersion version = V1_1;
|
||||
pendingDrops.add(version, newSuppressForeverChangeSet());
|
||||
pendingDrops.add(version, applyDropChangeSet);
|
||||
|
||||
Migration migration = pendingDrops.migrationForVersion("1_1");
|
||||
assertThat(migration.getChangeSet()).containsExactly(applyDropChangeSet);
|
||||
|
||||
assertThat(pendingDrops.testContainsEntryFor(version)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_migrationForVersion_when_next() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
MigrationVersion version = V1_1;
|
||||
|
||||
ChangeSet applyDropChangeSet = new ChangeSet();
|
||||
pendingDrops.add(version, newSuppressForeverChangeSet());
|
||||
pendingDrops.add(version, applyDropChangeSet);
|
||||
|
||||
|
||||
Migration migration = pendingDrops.migrationForVersion("next");
|
||||
assertThat(migration.getChangeSet()).containsExactly(applyDropChangeSet);
|
||||
assertThat(pendingDrops.testContainsEntryFor(version)).isTrue();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_migrationForVersion_when_next_isSuppressForever() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
pendingDrops.add(V1_1, newSuppressForeverChangeSet());
|
||||
pendingDrops.migrationForVersion("next");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_migrationForVersion_when_doesNotExist() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
pendingDrops.migrationForVersion("1_1");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_migrationForVersion_when_next_doesNotExist() throws Exception {
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
pendingDrops.migrationForVersion("next");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test_registerPendingHistoryDropColumns() throws Exception {
|
||||
|
||||
TDModelContainer modelContainer = new TDModelContainer();
|
||||
|
||||
DropColumn drop1 = col("one");
|
||||
drop1.setWithHistory(Boolean.TRUE);
|
||||
|
||||
DropColumn drop2 = col("two");
|
||||
|
||||
ChangeSet changeSet = changeSet(drop1, drop2);
|
||||
|
||||
PendingDrops pendingDrops = new PendingDrops();
|
||||
pendingDrops.add(V1_1, changeSet);
|
||||
pendingDrops.registerPendingHistoryDropColumns(modelContainer);
|
||||
|
||||
assertThat(modelContainer.drops).containsExactly(changeSet);
|
||||
}
|
||||
|
||||
class TDModelContainer extends ModelContainer {
|
||||
|
||||
List<ChangeSet> drops = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void registerPendingHistoryDropColumns(ChangeSet changeSet) {
|
||||
drops.add(changeSet);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ChangeSet newSuppressForeverChangeSet() {
|
||||
ChangeSet changeSet = new ChangeSet();
|
||||
changeSet.setSuppressDropsForever(Boolean.TRUE);
|
||||
return changeSet;
|
||||
}
|
||||
|
||||
static ChangeSet changeSet(String... colNames) {
|
||||
return EntryTest.changeSet(colNames);
|
||||
}
|
||||
|
||||
static ChangeSet changeSet(DropColumn... drops) {
|
||||
ChangeSet changeSet = new ChangeSet();
|
||||
for (DropColumn dropColumn : drops) {
|
||||
changeSet.getChangeSetChildren().add(dropColumn);
|
||||
}
|
||||
|
||||
return changeSet;
|
||||
}
|
||||
|
||||
static DropColumn col(String colName) {
|
||||
return EntryTest.col(colName);
|
||||
}
|
||||
|
||||
}
|
||||
-129
@@ -1,129 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model.build;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DatabaseFactory;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.DbConstraintNaming;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.DefaultConstraintMaxLength;
|
||||
import io.ebeaninternal.dbmigration.model.MColumn;
|
||||
import io.ebeaninternal.dbmigration.model.MCompoundForeignKey;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
import io.ebeaninternal.dbmigration.model.ModelContainer;
|
||||
import io.ebeaninternal.dbmigration.model.visitor.VisitAllUsing;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.CKeyAssoc;
|
||||
import org.tests.model.basic.CKeyDetail;
|
||||
import org.tests.model.basic.CKeyParent;
|
||||
import org.tests.model.basic.CKeyParentId;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModelBuildBeanVisitorTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer defaultServer = (SpiEbeanServer) DB.getDefault();
|
||||
|
||||
ModelContainer model = new ModelContainer();
|
||||
|
||||
DbConstraintNaming constraintNaming = defaultServer.getServerConfig().getConstraintNaming();
|
||||
constraintNaming.setMaxLength(new DefaultConstraintMaxLength(60));
|
||||
ModelBuildContext ctx = new ModelBuildContext(model, new H2Platform(), constraintNaming, true);
|
||||
|
||||
ModelBuildBeanVisitor addTable = new ModelBuildBeanVisitor(ctx);
|
||||
|
||||
new VisitAllUsing(addTable, defaultServer).visitAllBeans();
|
||||
|
||||
assert_compound_pk(model);
|
||||
|
||||
assert_discriminatorColumn_explicit(model);
|
||||
assert_discriminatorColumn_implied(model);
|
||||
assert_discriminatorColumn_length(model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_allQuoted() {
|
||||
|
||||
ModelContainer model = new ModelContainer();
|
||||
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setName("h2");
|
||||
config.loadFromProperties();
|
||||
config.setName("h2other");
|
||||
config.setAllQuotedIdentifiers(true);
|
||||
config.setDdlGenerate(false);
|
||||
config.setDdlRun(false);
|
||||
config.setDdlExtra(false);
|
||||
config.setDefaultServer(false);
|
||||
config.setRegister(false);
|
||||
|
||||
config.addClass(CKeyDetail.class);
|
||||
config.addClass(CKeyParent.class);
|
||||
config.addClass(CKeyAssoc.class);
|
||||
config.addClass(CKeyParentId.class);
|
||||
config.setDbOffline(true);
|
||||
config.setDatabasePlatform(new SqlServer17Platform());
|
||||
|
||||
final SpiEbeanServer database = (SpiEbeanServer)DatabaseFactory.create(config);
|
||||
|
||||
|
||||
ModelBuildContext ctx = new ModelBuildContext(model, config.getDatabasePlatform(), config.getConstraintNaming(), true);
|
||||
|
||||
ModelBuildBeanVisitor addTable = new ModelBuildBeanVisitor(ctx);
|
||||
|
||||
new VisitAllUsing(addTable, database).visitAllBeans();
|
||||
|
||||
MTable parent = model.getTable("[CKeyParent]");
|
||||
assertThat(parent.getPkName()).isEqualTo("[pk_CKeyParent]");
|
||||
|
||||
MTable detail = model.getTable("[CKeyDetail]");
|
||||
assertThat(detail.getPkName()).isEqualTo("[pk_CKeyDetail]");
|
||||
|
||||
final List<MCompoundForeignKey> compoundKeys = detail.getCompoundKeys();
|
||||
assertThat(compoundKeys).hasSize(1);
|
||||
final MCompoundForeignKey fkey = compoundKeys.get(0);
|
||||
assertThat(fkey.getName()).isEqualTo("[fk_CKeyDetail_parent]");
|
||||
assertThat(fkey.getIndexName()).isEqualTo("[ix_CKeyDetail_parent]");
|
||||
|
||||
database.shutdown(true, false);
|
||||
}
|
||||
|
||||
private void assert_compound_pk(ModelContainer model) {
|
||||
MTable item = model.getTable("item");
|
||||
|
||||
assertThat(item).isNotNull();
|
||||
assertThat(item.primaryKeyColumns()).hasSize(2);
|
||||
}
|
||||
|
||||
private void assert_discriminatorColumn_explicit(ModelContainer model) {
|
||||
|
||||
MTable configuration = model.getTable("configuration");
|
||||
MColumn discTypeColumn = configuration.getColumn("type");
|
||||
assertThat(discTypeColumn.getType()).isEqualTo("varchar(21)");
|
||||
assertThat(discTypeColumn.isNotnull()).isTrue();
|
||||
}
|
||||
|
||||
private void assert_discriminatorColumn_implied(ModelContainer model) {
|
||||
|
||||
MTable configuration = model.getTable("bar");
|
||||
MColumn discTypeColumn = configuration.getColumn("bar_type");
|
||||
assertThat(discTypeColumn.getType()).isEqualTo("varchar(31)");
|
||||
assertThat(discTypeColumn.isNotnull()).isTrue();
|
||||
}
|
||||
|
||||
private void assert_discriminatorColumn_length(ModelContainer model) {
|
||||
|
||||
MTable configuration = model.getTable("vehicle");
|
||||
MColumn discTypeColumn = configuration.getColumn("dtype");
|
||||
assertThat(discTypeColumn.getType()).isEqualTo("varchar(3)");
|
||||
assertThat(discTypeColumn.isNotnull()).isTrue();
|
||||
}
|
||||
}
|
||||
-85
@@ -1,85 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model.build;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.EbeanServerFactory;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.Helper;
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader;
|
||||
import io.ebeaninternal.dbmigration.model.CurrentModel;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
import io.ebeaninternal.dbmigration.model.ModelContainer;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.CKeyAssoc;
|
||||
import org.tests.model.basic.CKeyDetail;
|
||||
import org.tests.model.basic.CKeyParent;
|
||||
import org.tests.model.basic.CKeyParentId;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModelBuild_compoundKeyTest extends BaseTestCase {
|
||||
|
||||
private SpiEbeanServer getServer() {
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
config.setName("h2");
|
||||
config.loadFromProperties();
|
||||
config.setName("h2other");
|
||||
config.setDdlGenerate(false);
|
||||
config.setDdlRun(false);
|
||||
config.setDdlExtra(false);
|
||||
config.setDefaultServer(false);
|
||||
config.setRegister(false);
|
||||
|
||||
config.addClass(CKeyDetail.class);
|
||||
config.addClass(CKeyParent.class);
|
||||
config.addClass(CKeyAssoc.class);
|
||||
config.addClass(CKeyParentId.class);
|
||||
|
||||
return (SpiEbeanServer) EbeanServerFactory.create(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
|
||||
SpiEbeanServer ebeanServer = getServer();
|
||||
|
||||
CurrentModel currentModel = new CurrentModel(ebeanServer);
|
||||
ModelContainer model = currentModel.read();
|
||||
|
||||
MTable parent = model.getTable("ckey_parent");
|
||||
MTable detail = model.getTable("ckey_detail");
|
||||
|
||||
assertThat(parent).isNotNull();
|
||||
assertThat(detail).isNotNull();
|
||||
assertThat(parent.primaryKeyColumns()).hasSize(2);
|
||||
assertThat(detail.getCompoundKeys()).hasSize(1);
|
||||
|
||||
String apply = Helper.asText(this, "/assert/ModelBuild_compoundKeyTest/apply.sql");
|
||||
|
||||
String createDdl = currentModel.getCreateDdl();
|
||||
assertThat(createDdl).startsWith("-- Generated by ebean").endsWith(apply);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFromMigration() throws IOException {
|
||||
|
||||
|
||||
Migration migration = MigrationXmlReader.read("/container/test-compoundkey.xml");
|
||||
|
||||
SpiEbeanServer ebeanServer = getServer();
|
||||
CurrentModel currentModel = new CurrentModel(ebeanServer);
|
||||
currentModel.setChangeSet(migration.getChangeSet().get(0));
|
||||
|
||||
String createDdl = currentModel.getCreateDdl();
|
||||
String apply = Helper.asText(this, "/assert/ModelBuild_compoundKeyTest/apply.sql");
|
||||
|
||||
assertThat(createDdl).startsWith("-- Generated by ebean").endsWith(apply);
|
||||
}
|
||||
}
|
||||
-68
@@ -1,68 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.model.build;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.EbeanServerFactory;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.Helper;
|
||||
import io.ebeaninternal.dbmigration.model.CurrentModel;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.Person;
|
||||
import org.tests.model.basic.Phone;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModelBuild_explicitSequencesTest extends BaseTestCase {
|
||||
|
||||
private SpiEbeanServer getServer(boolean postgres) {
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
config.setName("h2");
|
||||
config.loadFromProperties();
|
||||
config.setName("h2other");
|
||||
config.setDdlGenerate(false);
|
||||
config.setDdlRun(false);
|
||||
config.setDdlExtra(false);
|
||||
config.setDefaultServer(false);
|
||||
config.setRegister(false);
|
||||
|
||||
config.setDatabasePlatformName(postgres ? "postgres" : "h2");
|
||||
|
||||
config.addClass(Person.class);
|
||||
config.addClass(Phone.class);
|
||||
|
||||
return (SpiEbeanServer) EbeanServerFactory.create(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
|
||||
SpiEbeanServer ebeanServer = getServer(false);
|
||||
CurrentModel currentModel = new CurrentModel(ebeanServer);
|
||||
|
||||
String apply = currentModel.getCreateDdl();
|
||||
assertThat(apply)
|
||||
.startsWith("-- Generated by ebean")
|
||||
.endsWith(Helper.asText(this, "/assert/ModelBuild_explicitSequencesTest/apply.sql"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_asPostgres() throws IOException {
|
||||
|
||||
SpiEbeanServer ebeanServer = getServer(true);
|
||||
CurrentModel currentModel = new CurrentModel(ebeanServer);
|
||||
final DdlOptions ddlOptions = currentModel.getDdlOptions();
|
||||
ddlOptions.setForeignKeySkipCheck(true);
|
||||
|
||||
String apply = currentModel.getCreateDdl();
|
||||
assertThat(apply)
|
||||
.startsWith("-- Generated by ebean")
|
||||
.endsWith(Helper.asText(this, "/assert/ModelBuild_explicitSequencesTest/pg-apply.sql"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user