#784 - DDL DB Migration - Add @ManyToOne does not generate Foreign key and index ddl statements in migration script

This commit is contained in:
Robin Bygrave
2016-07-25 20:44:15 +12:00
parent ba7e729ae1
commit 6828fa1786
4 changed files with 49 additions and 0 deletions
@@ -583,6 +583,12 @@ public class BaseTableDdl implements TableDdl {
}
}
for (Column column : columns) {
if (hasValue(column.getReferences())) {
writeForeignKey(writer, tableName, column);
}
}
// add a bit of whitespace
writer.apply().end();
}
@@ -35,6 +35,23 @@ public class BaseDdlHandlerTest extends BaseTestCase {
handler.generate(write, Helper.getAddColumn());
assertThat(write.apply().getBuffer()).isEqualTo("alter table foo add column added_to_foo varchar(20);\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("");
}
@@ -20,14 +20,23 @@ 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() {
@@ -42,6 +51,10 @@ public class Helper {
return (AddColumn) changeSetChildren.get(1);
}
public static AddColumn getAlterTableAddColumn() {
return (AddColumn) alterTableChangeSetChildren.get(0);
}
public static DropColumn getDropColumn() {
return (DropColumn) changeSetChildren.get(2);
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ebean-orm.github.io/xml/ns/dbmigration http://ebean-orm.github.io/xml/ns/dbmigration-1.0.xsd">
<changeSet type="apply">
<addColumn tableName="foo">
<column name="some_id" type="integer" references="bar.id" foreignKeyName="fk_foo_some_id" foreignKeyIndex="idx_foo_some_id"/>
</addColumn>
</changeSet>
</migration>