WIP Db Migration compound keys

This commit is contained in:
Robin Bygrave
2015-08-04 19:48:47 +12:00
parent 3bd281bea0
commit 009f91b949
19 changed files with 1250 additions and 1110 deletions
@@ -5,6 +5,8 @@ import com.avaje.ebean.dbmigration.ddlgeneration.DdlWrite;
import com.avaje.ebean.dbmigration.ddlgeneration.TableDdl;
import com.avaje.ebean.dbmigration.migration.Column;
import com.avaje.ebean.dbmigration.migration.CreateTable;
import com.avaje.ebean.dbmigration.migration.ForeignKey;
import com.avaje.ebean.dbmigration.migration.PrimaryKey;
import com.avaje.ebean.dbmigration.model.MTable;
import java.io.IOException;
@@ -50,7 +52,15 @@ public class BaseTableDdl implements TableDdl {
writeUniqueConstraints(apply, createTable);
writeCompoundUniqueConstraints(apply, createTable);
if (!pk.isEmpty()) {
writePrimaryKeyConstraint(apply, tableName, pk);
// defined on the columns
writePrimaryKeyConstraint(apply, tableName, toColumnNames(pk));
} else {
// defined on the table
if (createTable.getPrimaryKey() == null) {
System.out.print("asd");
} else {
writePrimaryKeyConstraint(apply, tableName, createTable.getPrimaryKey());
}
}
apply.newLine().append(")").endOfStatement();
@@ -75,14 +85,32 @@ public class BaseTableDdl implements TableDdl {
protected void writeAddForeignKeys(DdlWrite write, CreateTable createTable) throws IOException {
String tableName = createTable.getName();
List<Column> columns = createTable.getColumn();
for (Column column : columns) {
String references = column.getReferences();
if (hasValue(references)) {
writeForeignKey(write, createTable.getName(), column.getName(), references);
writeForeignKey(write, tableName, column.getName(), references);
}
}
//createTable.
writeAddCompoundForeignKeys(write, createTable);
}
protected void writeAddCompoundForeignKeys(DdlWrite write, CreateTable createTable) throws IOException {
String tableName = createTable.getName();
List<ForeignKey> foreignKey = createTable.getForeignKey();
for (ForeignKey key : foreignKey) {
String refTableName = key.getRefTableName();
String fkName = determineForeignKeyConstraintName(tableName, refTableName);
String[] cols = toColumnNamesSplit(key.getColumnNames());
String[] refColumns = toColumnNamesSplit(key.getRefColumnNames());
writeForeignKey(write, fkName, tableName, cols, refTableName, refColumns);
}
}
@@ -109,10 +137,10 @@ public class BaseTableDdl implements TableDdl {
fkeyBuffer
.append("alter table ").append(tableName)
.append(" add constraint ").append(fkName)
.append(" foreign key (");
.append(" foreign key");
appendColumns(columns, fkeyBuffer);
fkeyBuffer
.append(") references ")
.append(" references ")
.append(refTable);
appendColumns(refColumns, fkeyBuffer);
fkeyBuffer.appendWithSpace(platformDdl.getForeignKeyRestrict())
@@ -144,7 +172,7 @@ public class BaseTableDdl implements TableDdl {
if (i > 0) {
buffer.append(",");
}
buffer.append(columns[i]);
buffer.append(columns[i].trim());
}
buffer.append(")");
}
@@ -215,23 +243,23 @@ public class BaseTableDdl implements TableDdl {
buffer.append(")");
}
protected void writePrimaryKeyConstraint(DdlBuffer buffer, String tableName, PrimaryKey pk) throws IOException {
String columnNames = pk.getColumnNames();
String[] cols = columnNames.split(",");
writePrimaryKeyConstraint(buffer, tableName, cols);
}
/**
* Write the primary key constraint inline with the create table statement.
*/
protected void writePrimaryKeyConstraint(DdlBuffer buffer, String tableName, List<Column> pk) throws IOException {
protected void writePrimaryKeyConstraint(DdlBuffer buffer, String tableName, String[] pkColumns) throws IOException {
String pkName = determinePrimaryKeyName(tableName, pk);
String pkName = determinePrimaryKeyName(tableName, pkColumns);
buffer.append(",").newLine();
buffer.append(" constraint ").append(pkName).append(" primary key ");
buffer.append("(");
for (int i = 0; i < pk.size(); i++) {
if (i > 0) {
buffer.append(",");
}
buffer.append(pk.get(i).getName());
}
buffer.append(")");
buffer.append(" constraint ").append(pkName).append(" primary key");
appendColumns(pkColumns, buffer);
}
/**
@@ -239,19 +267,34 @@ public class BaseTableDdl implements TableDdl {
*/
public void alterTableAddPrimaryKey(DdlBuffer buffer, String tableName, List<Column> pk) throws IOException {
String pkName = determinePrimaryKeyName(tableName, pk);
String[] pkColumns = toColumnNames(pk);
String pkName = determinePrimaryKeyName(tableName, pkColumns);
buffer.append("alter table ").append(tableName);
buffer.append(" add primary key ").append(pkName).append(" (");
for (int i = 0; i < pk.size(); i++) {
if (i > 0) {
buffer.append(",");
}
buffer.append(pk.get(i).getName());
}
buffer.append(" add primary key ").append(pkName);
appendColumns(pkColumns, buffer);
buffer.append(")").endOfStatement();
}
/**
* Return as an array of string column names.
*/
private String[] toColumnNames(List<Column> columns) {
String[] cols = new String[columns.size()];
for (int i = 0; i < cols.length; i++) {
cols[i] = columns.get(i).getName();
}
return cols;
}
/**
* Return as an array of string column names.
*/
private String[] toColumnNamesSplit(String columns) {
return columns.split(",");
}
/**
* Write the column definition to the create table statement.
*/
@@ -283,14 +326,9 @@ public class BaseTableDdl implements TableDdl {
/**
* Return the primary key constraint name.
*/
protected String determinePrimaryKeyName(String tableName, List<Column> pkColumns) {
protected String determinePrimaryKeyName(String tableName, String[] pkColumns) {
// collect the primary key column names
List<String> pkColumnNames = new ArrayList<String>(pkColumns.size());
for (int i = 0; i < pkColumns.size(); i++) {
pkColumnNames.add(pkColumns.get(i).getName());
}
return namingConvention.primaryKeyName(tableName, pkColumnNames);
return namingConvention.primaryKeyName(tableName, pkColumns);
}
/**
@@ -35,7 +35,7 @@ public class DdlNamingConvention {
/**
* Return the primary key constraint name.
*/
public String primaryKeyName(String tableName, List<String> pkColumns) {
public String primaryKeyName(String tableName, String[] pkColumns) {
return pkPrefix + normalise(tableName) + pkSuffix;
}
@@ -62,7 +62,7 @@ public class DdlNamingConvention {
StringBuilder sb = new StringBuilder(30);
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
sb.append("-");
sb.append("_");
}
sb.append(columns[i]);
}
@@ -1,14 +1,14 @@
package com.avaje.ebean.dbmigration.migration;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;
/**
@@ -1,12 +1,12 @@
package com.avaje.ebean.dbmigration.migration;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;
/**
@@ -1,6 +1,9 @@
package com.avaje.ebean.dbmigration.migration;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@@ -9,9 +12,6 @@ import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
/**
@@ -50,7 +50,7 @@ public class ChangeSet {
@XmlElement(name = "createTable", type = CreateTable.class),
@XmlElement(name = "dropTable", type = DropTable.class),
@XmlElement(name = "renameTable", type = RenameTable.class),
@XmlElement(name = "addHistoryTable", type = CreateHistoryTable.class),
@XmlElement(name = "createHistoryTable", type = CreateHistoryTable.class),
@XmlElement(name = "createView", type = CreateView.class),
@XmlElement(name = "dropView", type = DropView.class),
@XmlElement(name = "renameView", type = RenameView.class),
@@ -18,8 +18,8 @@ import javax.xml.bind.annotation.XmlValue;
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;attGroup ref="{http://ebean-orm.github.io/xml/ns/dbmigration}column"/>
* &lt;attGroup ref="{http://ebean-orm.github.io/xml/ns/dbmigration}columnAttributes"/>
* &lt;attGroup ref="{http://ebean-orm.github.io/xml/ns/dbmigration}column"/>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
@@ -36,14 +36,6 @@ public class Column {
@XmlValue
protected String content;
@XmlAttribute(name = "name", required = true)
protected String name;
@XmlAttribute(name = "type", required = true)
protected String type;
@XmlAttribute(name = "defaultValue")
protected String defaultValue;
@XmlAttribute(name = "remarks")
protected String remarks;
@XmlAttribute(name = "notnull")
protected Boolean notnull;
@XmlAttribute(name = "checkConstraint")
@@ -56,12 +48,14 @@ public class Column {
protected Boolean identity;
@XmlAttribute(name = "references")
protected String references;
@XmlAttribute(name = "deleteCascade")
protected Boolean deleteCascade;
@XmlAttribute(name = "deferrable")
protected Boolean deferrable;
@XmlAttribute(name = "initiallyDeferred")
protected Boolean initiallyDeferred;
@XmlAttribute(name = "name", required = true)
protected String name;
@XmlAttribute(name = "type", required = true)
protected String type;
@XmlAttribute(name = "defaultValue")
protected String defaultValue;
@XmlAttribute(name = "remarks")
protected String remarks;
/**
* Gets the value of the content property.
@@ -87,102 +81,6 @@ public class Column {
this.content = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the type property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getType() {
return type;
}
/**
* Sets the value of the type property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setType(String value) {
this.type = value;
}
/**
* Gets the value of the defaultValue property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getDefaultValue() {
return defaultValue;
}
/**
* Sets the value of the defaultValue property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setDefaultValue(String value) {
this.defaultValue = value;
}
/**
* Gets the value of the remarks property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getRemarks() {
return remarks;
}
/**
* Sets the value of the remarks property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setRemarks(String value) {
this.remarks = value;
}
/**
* Gets the value of the notnull property.
*
@@ -328,75 +226,99 @@ public class Column {
}
/**
* Gets the value of the deleteCascade property.
* Gets the value of the name property.
*
* @return
* possible object is
* {@link Boolean }
* {@link String }
*
*/
public Boolean isDeleteCascade() {
return deleteCascade;
public String getName() {
return name;
}
/**
* Sets the value of the deleteCascade property.
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link Boolean }
* {@link String }
*
*/
public void setDeleteCascade(Boolean value) {
this.deleteCascade = value;
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the deferrable property.
* Gets the value of the type property.
*
* @return
* possible object is
* {@link Boolean }
* {@link String }
*
*/
public Boolean isDeferrable() {
return deferrable;
public String getType() {
return type;
}
/**
* Sets the value of the deferrable property.
* Sets the value of the type property.
*
* @param value
* allowed object is
* {@link Boolean }
* {@link String }
*
*/
public void setDeferrable(Boolean value) {
this.deferrable = value;
public void setType(String value) {
this.type = value;
}
/**
* Gets the value of the initiallyDeferred property.
* Gets the value of the defaultValue property.
*
* @return
* possible object is
* {@link Boolean }
* {@link String }
*
*/
public Boolean isInitiallyDeferred() {
return initiallyDeferred;
public String getDefaultValue() {
return defaultValue;
}
/**
* Sets the value of the initiallyDeferred property.
* Sets the value of the defaultValue property.
*
* @param value
* allowed object is
* {@link Boolean }
* {@link String }
*
*/
public void setInitiallyDeferred(Boolean value) {
this.initiallyDeferred = value;
public void setDefaultValue(String value) {
this.defaultValue = value;
}
/**
* Gets the value of the remarks property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getRemarks() {
return remarks;
}
/**
* Sets the value of the remarks property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setRemarks(String value) {
this.remarks = value;
}
}
@@ -27,7 +27,7 @@ import javax.xml.bind.annotation.XmlType;
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "addHistoryTable")
@XmlRootElement(name = "createHistoryTable")
public class CreateHistoryTable {
@XmlAttribute(name = "baseTable", required = true)
@@ -1,14 +1,14 @@
package com.avaje.ebean.dbmigration.migration;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;
/**
@@ -23,6 +23,8 @@ import java.util.List;
* &lt;sequence>
* &lt;element ref="{http://ebean-orm.github.io/xml/ns/dbmigration}column" maxOccurs="unbounded"/>
* &lt;element ref="{http://ebean-orm.github.io/xml/ns/dbmigration}uniqueConstraint" maxOccurs="unbounded" minOccurs="0"/>
* &lt;element ref="{http://ebean-orm.github.io/xml/ns/dbmigration}foreignKey" maxOccurs="unbounded" minOccurs="0"/>
* &lt;element ref="{http://ebean-orm.github.io/xml/ns/dbmigration}primaryKey" minOccurs="0"/>
* &lt;/sequence>
* &lt;attGroup ref="{http://ebean-orm.github.io/xml/ns/dbmigration}tablespaceAttributes"/>
* &lt;attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
@@ -37,7 +39,9 @@ import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"column",
"uniqueConstraint"
"uniqueConstraint",
"foreignKey",
"primaryKey"
})
@XmlRootElement(name = "createTable")
public class CreateTable {
@@ -45,6 +49,8 @@ public class CreateTable {
@XmlElement(required = true)
protected List<Column> column;
protected List<UniqueConstraint> uniqueConstraint;
protected List<ForeignKey> foreignKey;
protected PrimaryKey primaryKey;
@XmlAttribute(name = "name", required = true)
protected String name;
@XmlAttribute(name = "withHistory")
@@ -114,6 +120,59 @@ public class CreateTable {
return this.uniqueConstraint;
}
/**
* Gets the value of the foreignKey property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the foreignKey property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getForeignKey().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link ForeignKey }
*
*
*/
public List<ForeignKey> getForeignKey() {
if (foreignKey == null) {
foreignKey = new ArrayList<ForeignKey>();
}
return this.foreignKey;
}
/**
* Gets the value of the primaryKey property.
*
* @return
* possible object is
* {@link PrimaryKey }
*
*/
public PrimaryKey getPrimaryKey() {
return primaryKey;
}
/**
* Sets the value of the primaryKey property.
*
* @param value
* allowed object is
* {@link PrimaryKey }
*
*/
public void setPrimaryKey(PrimaryKey value) {
this.primaryKey = value;
}
/**
* Gets the value of the name property.
*
@@ -1,13 +1,13 @@
package com.avaje.ebean.dbmigration.migration;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;
/**
@@ -7,7 +7,7 @@ import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the org.avaje.ebean.dbmigration.migration package.
* generated in the com.avaje.ebean.dbmigration.migration package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
@@ -23,36 +23,12 @@ public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.avaje.ebean.dbmigration.migration
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.avaje.ebean.dbmigration.migration
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Rollback }
*
*/
public Rollback createRollback() {
return new Rollback();
}
/**
* Create an instance of {@link AddColumn }
*
*/
public AddColumn createAddColumn() {
return new AddColumn();
}
/**
* Create an instance of {@link Column }
*
*/
public Column createColumn() {
return new Column();
}
/**
* Create an instance of {@link CreateTable }
*
@@ -61,6 +37,14 @@ public class ObjectFactory {
return new CreateTable();
}
/**
* Create an instance of {@link Column }
*
*/
public Column createColumn() {
return new Column();
}
/**
* Create an instance of {@link UniqueConstraint }
*
@@ -69,6 +53,22 @@ public class ObjectFactory {
return new UniqueConstraint();
}
/**
* Create an instance of {@link ForeignKey }
*
*/
public ForeignKey createForeignKey() {
return new ForeignKey();
}
/**
* Create an instance of {@link PrimaryKey }
*
*/
public PrimaryKey createPrimaryKey() {
return new PrimaryKey();
}
/**
* Create an instance of {@link DropForeignKey }
*
@@ -77,14 +77,6 @@ public class ObjectFactory {
return new DropForeignKey();
}
/**
* Create an instance of {@link Apply }
*
*/
public Apply createApply() {
return new Apply();
}
/**
* Create an instance of {@link Configuration }
*
@@ -117,22 +109,6 @@ public class ObjectFactory {
return new DropHistoryTable();
}
/**
* Create an instance of {@link RenameView }
*
*/
public RenameView createRenameView() {
return new RenameView();
}
/**
* Create an instance of {@link AddForeignKey }
*
*/
public AddForeignKey createAddForeignKey() {
return new AddForeignKey();
}
/**
* Create an instance of {@link DropColumn }
*
@@ -149,14 +125,6 @@ public class ObjectFactory {
return new DropView();
}
/**
* Create an instance of {@link ChangeSet }
*
*/
public ChangeSet createChangeSet() {
return new ChangeSet();
}
/**
* Create an instance of {@link Sql }
*
@@ -166,11 +134,19 @@ public class ObjectFactory {
}
/**
* Create an instance of {@link DropTable }
* Create an instance of {@link Apply }
*
*/
public DropTable createDropTable() {
return new DropTable();
public Apply createApply() {
return new Apply();
}
/**
* Create an instance of {@link Rollback }
*
*/
public Rollback createRollback() {
return new Rollback();
}
/**
@@ -181,6 +157,14 @@ public class ObjectFactory {
return new CreateHistoryTable();
}
/**
* Create an instance of {@link RenameColumn }
*
*/
public RenameColumn createRenameColumn() {
return new RenameColumn();
}
/**
* Create an instance of {@link CreateView }
*
@@ -190,11 +174,43 @@ public class ObjectFactory {
}
/**
* Create an instance of {@link RenameColumn }
* Create an instance of {@link DropTable }
*
*/
public RenameColumn createRenameColumn() {
return new RenameColumn();
public DropTable createDropTable() {
return new DropTable();
}
/**
* Create an instance of {@link AddColumn }
*
*/
public AddColumn createAddColumn() {
return new AddColumn();
}
/**
* Create an instance of {@link RenameView }
*
*/
public RenameView createRenameView() {
return new RenameView();
}
/**
* Create an instance of {@link AddForeignKey }
*
*/
public AddForeignKey createAddForeignKey() {
return new AddForeignKey();
}
/**
* Create an instance of {@link ChangeSet }
*
*/
public ChangeSet createChangeSet() {
return new ChangeSet();
}
/**
@@ -49,6 +49,10 @@ public class CurrentModel {
return model;
}
public void setChangeSet(ChangeSet changeSet) {
this.changeSet = changeSet;
}
public ChangeSet getChangeSet() {
read();
if (changeSet == null) {
@@ -90,6 +94,16 @@ public class CurrentModel {
return ddl.toString();
}
public DdlWrite generateDdl(ChangeSet changeSet) throws IOException {
DdlWrite write = new DdlWrite();
BaseDdlHandler handler = handler();
handler.generate(write, changeSet);
return write;
}
private void createDdl() throws IOException {
if (write == null) {
@@ -109,17 +109,15 @@ public class MColumn {
Column c = new Column();
c.setName(name);
c.setType(type);
c.setNotnull(notnull);
if (notnull) c.setNotnull(notnull);
if (unique) c.setUnique(unique);
if (primaryKey) c.setPrimaryKey(primaryKey);
if (identity) c.setIdentity(identity);
c.setCheckConstraint(checkConstraint);
c.setUnique(unique);
c.setPrimaryKey(primaryKey);
c.setIdentity(identity);
c.setReferences(references);
c.setDefaultValue(defaultValue);
//c.setDeleteCascade();
//c.setDeferrable(deferrable);
return c;
}
}
@@ -1,5 +1,8 @@
package com.avaje.ebean.dbmigration.model;
import com.avaje.ebean.dbmigration.migration.Column;
import com.avaje.ebean.dbmigration.migration.ForeignKey;
import java.util.ArrayList;
import java.util.List;
@@ -24,4 +27,28 @@ public class MCompoundForeignKey {
columns.add(dbCol);
referenceColumns.add(refColumn);
}
public ForeignKey createForeignKey() {
ForeignKey fk = new ForeignKey();
fk.setColumnNames(toColumnNames(columns));
fk.setRefColumnNames(toColumnNames(referenceColumns));
fk.setRefTableName(referenceTable);
return fk;
}
/**
* Return as an array of string column names.
*/
private String toColumnNames(List<String> columns) {
StringBuilder sb = new StringBuilder(40);
for (int i = 0; i < columns.size(); i++) {
if (i > 0) {
sb.append(",");
}
sb.append(columns.get(i));
}
return sb.toString();
}
}
@@ -4,6 +4,7 @@ import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.Column;
import com.avaje.ebean.dbmigration.migration.CreateTable;
import com.avaje.ebean.dbmigration.migration.DropColumn;
import com.avaje.ebean.dbmigration.migration.ForeignKey;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -81,6 +82,11 @@ public class MTable {
for (MColumn column : this.columns.values()) {
createTable.getColumn().add(column.createColumn());
}
for (MCompoundForeignKey compoundKey : compoundKeys) {
createTable.getForeignKey().add(compoundKey.createForeignKey());
}
return createTable;
}
@@ -142,6 +148,19 @@ public class MTable {
return compoundKeys;
}
/**
* Return the list of columns that make the primary key.
*/
public List<MColumn> primaryKeyColumns() {
List<MColumn> pk = new ArrayList<MColumn>(3);
for (MColumn column : columns.values()) {
if (column.isPrimaryKey()) {
pk.add(column);
}
}
return pk;
}
private void checkTableName(String tableName) {
if (!name.equals(tableName)) {
throw new IllegalArgumentException("addColumn tableName ["+tableName+"] does not match ["+name+"]");
@@ -183,4 +202,5 @@ public class MTable {
public void addForeignKey(MCompoundForeignKey compoundKey) {
compoundKeys.add(compoundKey);
}
}
@@ -24,6 +24,8 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor {
private final MTable table;
private MColumn lastColumn;
public ModelBuildPropertyVisitor(ModelBuildContext ctx, MTable table) {
this.ctx = ctx;
this.table = table;
@@ -59,8 +61,11 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor {
@Override
public void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne<?> embedded) {
//this.embedded = embedded;
visitScalar(p);
if (embedded.isId()) {
// compound primary key
lastColumn.setPrimaryKey(true);
}
}
@Override
@@ -141,6 +146,7 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor {
}
col.setCheckConstraint(p.getDbConstraintExpression());
lastColumn = col;
table.addColumn(col);
}