mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-12-01 15:37:14 +00:00
#1147 - Refactor move DbMigration internals into io.ebeaninternal - change to use DbMigration.create() - initial move
This commit is contained in:
1 parent
764f5ce4d4
commit
10faa1fae6
146 files changed
+1087
-936
No files matched your search
@@ -0,0 +1,108 @@
|
||||
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.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* 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.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.POSTGRES, "postgres");
|
||||
migration.addPlatform(Platform.ORACLE, "oracle");
|
||||
migration.addPlatform(Platform.SQLITE, "sqlite");
|
||||
migration.addPlatform(Platform.SQLSERVER, "sqlserver");
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
config.setName("migrationtest");
|
||||
config.loadFromProperties();
|
||||
config.setRegister(false);
|
||||
config.setDefaultServer(false);
|
||||
|
||||
|
||||
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");
|
||||
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");
|
||||
System.clearProperty("ddl.migration.pendingDropsFor");
|
||||
assertThat(migration.generateMigration()).isNull(); // subsequent call
|
||||
|
||||
logger.info("end");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package io.ebeaninternal.dbmigration;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.SqlQuery;
|
||||
import io.ebean.SqlRow;
|
||||
import io.ebean.SqlUpdate;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.migration.ddl.DdlRunner;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
public class DbMigrationTest extends BaseTestCase {
|
||||
|
||||
private int runScript(boolean expectErrors, String scriptName) throws IOException {
|
||||
try (InputStream stream = getClass().getResourceAsStream("/dbmigration/migrationtest/" + server().getPluginApi().getDatabasePlatform().getName()+"/" + scriptName);
|
||||
java.util.Scanner s = new java.util.Scanner(stream)) {
|
||||
s.useDelimiter("\\A");
|
||||
if (s.hasNext()) {
|
||||
return runScript(expectErrors, s.next(), scriptName);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int runScript(boolean expectErrors, String content, String scriptName) {
|
||||
|
||||
DdlRunner runner = new DdlRunner(expectErrors, scriptName);
|
||||
|
||||
Transaction transaction = server().createTransaction();
|
||||
Connection connection = transaction.getConnection();
|
||||
try {
|
||||
if (expectErrors) {
|
||||
connection.setAutoCommit(true);
|
||||
}
|
||||
int count = runner.runAll(content, connection);
|
||||
if (expectErrors) {
|
||||
connection.setAutoCommit(false);
|
||||
}
|
||||
transaction.commit();
|
||||
return count;
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new PersistenceException("Failed to run script", e);
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testRunMigration() throws IOException {
|
||||
// first clean up previously created objects
|
||||
runScript(true, "drop table migtest_e_ref;\n","test");
|
||||
runScript(true, "drop table migtest_e_basic;\n"
|
||||
+ "drop table migtest_e_history;\n"
|
||||
+ "drop table migtest_e_ref;\n"
|
||||
+ "drop table migtest_e_ref cascade;\n"
|
||||
+ "drop table migtest_e_user;\n"
|
||||
+ "drop table migtest_e_history;\n"
|
||||
+ "drop table migtest_e_history cascade;\n" // pg
|
||||
+ "drop table migtest_e_history_history cascade;\n" // pg
|
||||
+ "drop sequence migtest_e_basic_seq;\n"
|
||||
+ "drop sequence migtest_e_history_seq;\n"
|
||||
+ "drop sequence migtest_e_ref_seq;\n"
|
||||
+ "drop sequence migtest_e_user;\n"
|
||||
+ "drop sequence migtest_e_history;\n"
|
||||
, "cleanup");
|
||||
|
||||
|
||||
runScript(false, "1.0__initial.sql");
|
||||
|
||||
SqlUpdate update = server().createSqlUpdate("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);
|
||||
|
||||
|
||||
// Run migration
|
||||
runScript(false, "1.1.sql");
|
||||
SqlQuery select = server().createSqlQuery("select * from migtest_e_basic order by id");
|
||||
List<SqlRow> result = select.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")).isEqualTo(new Timestamp(100, 0, 1, 0, 0, 0, 0)); // = 2000-01-01T00:00:00
|
||||
|
||||
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")).isEqualTo(new Timestamp(100, 0, 1, 0, 0, 0, 0)); // = 2000-01-01T00:00:00
|
||||
|
||||
// Run migration & drops
|
||||
runScript(false, "1.2__dropsFor_1.1.sql");
|
||||
|
||||
|
||||
select = server().createSqlQuery("select * from migtest_e_basic order by id");
|
||||
result = select.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
|
||||
select = server().createSqlQuery("select * from migtest_e_basic order by id");
|
||||
result = select.findList();
|
||||
assertThat(result).hasSize(2);
|
||||
row = result.get(0);
|
||||
assertThat(row.keySet()).contains("old_boolean", "old_boolean2");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServerPlatform;
|
||||
import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.model.CurrentModel;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class BaseDdlHandlerTest extends BaseTestCase {
|
||||
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
|
||||
private DdlHandler h2Handler() {
|
||||
return new H2Platform().createDdlHandler(serverConfig);
|
||||
}
|
||||
|
||||
private DdlHandler postgresHandler() {
|
||||
return new PostgresPlatform().createDdlHandler(serverConfig);
|
||||
}
|
||||
|
||||
private DdlHandler sqlserverHandler() {
|
||||
return new SqlServerPlatform().createDdlHandler(serverConfig);
|
||||
}
|
||||
|
||||
@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 varchar(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");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
@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("");
|
||||
}
|
||||
|
||||
|
||||
@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;");
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
|
||||
import io.ebean.config.ServerConfig;
|
||||
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 org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class BaseTableDdlTest {
|
||||
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
|
||||
@Test
|
||||
public void testAlterColumn() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, new H2Platform().getPlatformDdl());
|
||||
|
||||
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 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, new OraclePlatform().getPlatformDdl());
|
||||
|
||||
DdlWrite write = new DdlWrite();
|
||||
|
||||
Column column = new Column();
|
||||
column.setName("col_name");
|
||||
column.setType("varchar(20)");
|
||||
|
||||
ddlGen.alterTableAddColumn(write.apply(), "mytable", column, false);
|
||||
|
||||
String ddl = write.apply().getBuffer();
|
||||
assertThat(ddl).contains("alter table mytable add column col_name varchar2(20)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnComment() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, new H2Platform().getPlatformDdl());
|
||||
|
||||
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 testAddTableComment() throws IOException {
|
||||
|
||||
BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, new H2Platform().getPlatformDdl());
|
||||
|
||||
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, new MySqlPlatform().getPlatformDdl());
|
||||
|
||||
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, new H2Platform().getPlatformDdl());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DbConstraintNormalise;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.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");
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
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 org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.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);
|
||||
|
||||
H2Platform h2Platform = new H2Platform();
|
||||
PlatformDdl h2Ddl = h2Platform.getPlatformDdl();
|
||||
h2Ddl.configure(ebeanServer.getServerConfig());
|
||||
h2Ddl.regenerateHistoryTriggers(write, update);
|
||||
|
||||
assertThat(write.applyHistory().isEmpty()).isFalse();
|
||||
assertThat(write.applyHistory().getBuffer()).contains("add one");
|
||||
assertThat(write.dropAll().isEmpty()).isTrue();
|
||||
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
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]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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"}));
|
||||
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServerPlatform;
|
||||
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class PlatformDdl_AlterColumnTest {
|
||||
|
||||
PlatformDdl h2Ddl = new H2Platform().getPlatformDdl();
|
||||
PlatformDdl pgDdl = new PostgresPlatform().getPlatformDdl();
|
||||
PlatformDdl mysqlDdl = new MySqlPlatform().getPlatformDdl();
|
||||
PlatformDdl oraDdl = new OraclePlatform().getPlatformDdl();
|
||||
PlatformDdl sqlServerDdl = new SqlServerPlatform().getPlatformDdl();
|
||||
|
||||
{
|
||||
ServerConfig 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 testAlterColumnBaseAttributes() throws Exception {
|
||||
|
||||
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 varchar(5) not null", sql);
|
||||
|
||||
alterColumn.setNotnull(Boolean.FALSE);
|
||||
sql = mysqlDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab modify acol varchar(5)", sql);
|
||||
|
||||
alterColumn.setNotnull(null);
|
||||
alterColumn.setType("varchar(100)");
|
||||
|
||||
sql = mysqlDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab modify acol varchar(100)", sql);
|
||||
|
||||
alterColumn.setCurrentNotnull(Boolean.TRUE);
|
||||
sql = mysqlDdl.alterColumnBaseAttributes(alterColumn);
|
||||
assertEquals("alter table mytab modify acol varchar(100) not null", sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnType() throws Exception {
|
||||
|
||||
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)", 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnNotnull() throws Exception {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnNull() throws Exception {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnDefaultValue() throws Exception {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlterColumnDropDefault() throws Exception {
|
||||
|
||||
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");
|
||||
assertThat(sql).startsWith("delimiter $$").endsWith("$$");
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServerPlatform;
|
||||
import io.ebean.config.dbplatform.mysql.MySqlPlatform;
|
||||
import io.ebean.config.dbplatform.oracle.OraclePlatform;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class PlatformDdl_dropUniqueConstraintTest {
|
||||
|
||||
|
||||
PlatformDdl h2Ddl = new H2Platform().getPlatformDdl();
|
||||
PlatformDdl pgDdl = new PostgresPlatform().getPlatformDdl();
|
||||
PlatformDdl mysqlDdl = new MySqlPlatform().getPlatformDdl();
|
||||
PlatformDdl oraDdl = new OraclePlatform().getPlatformDdl();
|
||||
PlatformDdl sqlServerDdl = new SqlServerPlatform().getPlatformDdl();
|
||||
|
||||
@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 (OBJECT_ID('uq_name', 'UQ') IS NOT NULL) alter table mytab drop constraint uq_name;\n"
|
||||
+ "IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('mytab','U') AND name = 'uq_name') drop index uq_name ON mytab", sql);
|
||||
|
||||
|
||||
sql = mysqlDdl.alterTableDropUniqueConstraint("mytab", "uq_name");
|
||||
assertEquals("alter table mytab drop index uq_name", sql);
|
||||
}
|
||||
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
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
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.migration.DropColumn;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.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;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
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;
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
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");
|
||||
|
||||
assertThat(v0.compareTo(v1)).isGreaterThan(0);
|
||||
assertThat(v1.compareTo(v0)).isLessThan(0);
|
||||
assertThat(v1.compareTo(v2)).isEqualTo(0);
|
||||
|
||||
assertThat(v0.compareTo(v3)).isLessThan(0);
|
||||
assertThat(v3.compareTo(v0)).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
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() throws Exception {
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModelContainerTest {
|
||||
|
||||
@Test
|
||||
public void apply_when_noPendingDrops_then_emptyPending() throws Exception {
|
||||
|
||||
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() throws Exception {
|
||||
|
||||
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() throws Exception {
|
||||
|
||||
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() throws Exception {
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
package io.ebeaninternal.dbmigration.model;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package io.ebeaninternal.dbmigration.model.build;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.DbConstraintNaming;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.DefaultConstraintMaxLength;
|
||||
import io.ebeaninternal.dbmigration.model.MColumn;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
import io.ebeaninternal.dbmigration.model.ModelContainer;
|
||||
import io.ebeaninternal.dbmigration.model.visitor.VisitAllUsing;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ModelBuildBeanVisitorTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer defaultServer = (SpiEbeanServer) Ebean.getDefaultServer();
|
||||
|
||||
ModelContainer model = new ModelContainer();
|
||||
|
||||
DbConstraintNaming constraintNaming = defaultServer.getServerConfig().getConstraintNaming();
|
||||
|
||||
DefaultConstraintMaxLength maxLength = new DefaultConstraintMaxLength(60);
|
||||
ModelBuildContext ctx = new ModelBuildContext(model, constraintNaming, maxLength, 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);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
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() {
|
||||
|
||||
System.setProperty("ebean.ignoreExtraDdl", "true");
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
config.setName("h2");
|
||||
config.loadFromProperties();
|
||||
config.setName("h2other");
|
||||
config.setDdlGenerate(false);
|
||||
config.setDdlRun(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).isEqualTo(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).isEqualTo(apply);
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
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.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) {
|
||||
|
||||
System.setProperty("ebean.ignoreExtraDdl", "true");
|
||||
|
||||
ServerConfig config = new ServerConfig();
|
||||
config.setName("h2");
|
||||
config.loadFromProperties();
|
||||
config.setName("h2other");
|
||||
config.setDdlGenerate(false);
|
||||
config.setDdlRun(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).isEqualTo(Helper.asText(this, "/assert/ModelBuild_explicitSequencesTest/apply.sql"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_asPostgres() throws IOException {
|
||||
|
||||
SpiEbeanServer ebeanServer = getServer(true);
|
||||
CurrentModel currentModel = new CurrentModel(ebeanServer);
|
||||
|
||||
String apply = currentModel.getCreateDdl();
|
||||
assertThat(apply).isEqualTo(Helper.asText(this, "/assert/ModelBuild_explicitSequencesTest/pg-apply.sql"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user