diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java index 57e05c723..bb3d8ed5b 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java @@ -7,9 +7,12 @@ import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.ChangeSet; +import com.avaje.ebean.dbmigration.migration.CreateIndex; import com.avaje.ebean.dbmigration.migration.CreateTable; import com.avaje.ebean.dbmigration.migration.DropColumn; import com.avaje.ebean.dbmigration.migration.DropHistoryTable; +import com.avaje.ebean.dbmigration.migration.DropIndex; +import com.avaje.ebean.dbmigration.migration.DropTable; import java.io.IOException; import java.util.List; @@ -32,6 +35,12 @@ public class BaseDdlHandler implements DdlHandler { for (Object change : changeSetChildren) { if (change instanceof CreateTable) { generate(writer, (CreateTable) change); + } else if (change instanceof DropTable) { + generate(writer, (DropTable) change); + } else if (change instanceof CreateIndex) { + generate(writer, (CreateIndex) change); + } else if (change instanceof DropIndex) { + generate(writer, (DropIndex) change); } else if (change instanceof AddColumn) { generate(writer, (AddColumn) change); } else if (change instanceof DropColumn) { @@ -56,6 +65,11 @@ public class BaseDdlHandler implements DdlHandler { tableDdl.generate(writer, createTable); } + @Override + public void generate(DdlWrite writer, DropTable dropTable) throws IOException { + tableDdl.generate(writer, dropTable); + } + @Override public void generate(DdlWrite writer, AddColumn addColumn) throws IOException { tableDdl.generate(writer, addColumn); @@ -80,4 +94,15 @@ public class BaseDdlHandler implements DdlHandler { public void generate(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException { tableDdl.generate(writer, dropHistoryTable); } + + @Override + public void generate(DdlWrite writer, CreateIndex createIndex) throws IOException { + tableDdl.generate(writer, createIndex); + } + + @Override + public void generate(DdlWrite writer, DropIndex dropIndex) throws IOException { + tableDdl.generate(writer, dropIndex); + } + } diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/DdlHandler.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/DdlHandler.java index 5506611d0..2f929122c 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/DdlHandler.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/DdlHandler.java @@ -4,9 +4,12 @@ import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.ChangeSet; +import com.avaje.ebean.dbmigration.migration.CreateIndex; import com.avaje.ebean.dbmigration.migration.CreateTable; import com.avaje.ebean.dbmigration.migration.DropColumn; import com.avaje.ebean.dbmigration.migration.DropHistoryTable; +import com.avaje.ebean.dbmigration.migration.DropIndex; +import com.avaje.ebean.dbmigration.migration.DropTable; import java.io.IOException; @@ -18,6 +21,8 @@ public interface DdlHandler { void generate(DdlWrite writer, CreateTable createTable) throws IOException; + void generate(DdlWrite writer, DropTable dropTable) throws IOException; + void generate(DdlWrite writer, AddColumn addColumn) throws IOException; void generate(DdlWrite writer, DropColumn dropColumn) throws IOException; @@ -28,5 +33,9 @@ public interface DdlHandler { void generate(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException; + void generate(DdlWrite writer, CreateIndex createIndex) throws IOException; + + void generate(DdlWrite writer, DropIndex dropIndex) throws IOException; + void generateExtra(DdlWrite write) throws IOException; } \ No newline at end of file diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java index 45597f740..1fa2b2e9f 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java @@ -3,9 +3,11 @@ package com.avaje.ebean.dbmigration.ddlgeneration; import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; import com.avaje.ebean.dbmigration.migration.AlterColumn; +import com.avaje.ebean.dbmigration.migration.CreateIndex; import com.avaje.ebean.dbmigration.migration.CreateTable; import com.avaje.ebean.dbmigration.migration.DropColumn; import com.avaje.ebean.dbmigration.migration.DropHistoryTable; +import com.avaje.ebean.dbmigration.migration.DropIndex; import com.avaje.ebean.dbmigration.migration.DropTable; import java.io.IOException; @@ -16,10 +18,15 @@ import java.io.IOException; public interface TableDdl { /** - * Generate the create table DDL. + * Generate the create table change. */ void generate(DdlWrite writer, CreateTable createTable) throws IOException; + /** + * Write the drop column change. + */ + void generate(DdlWrite writer, DropTable dropTable) throws IOException; + /** * Write the add column change. */ @@ -30,11 +37,6 @@ public interface TableDdl { */ void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException; - /** - * Write the drop column change. - */ - void generate(DdlWrite writer, DropTable dropTable) throws IOException; - /** * Write the drop column change. */ @@ -50,6 +52,16 @@ public interface TableDdl { */ void generate(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException; + /** + * Generate the create index change. + */ + void generate(DdlWrite writer, CreateIndex createIndex) throws IOException; + + /** + * Write the drop index change. + */ + void generate(DdlWrite writer, DropIndex dropIndex) throws IOException; + /** * Generate any extra DDL such as regeneration of history triggers. */ diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java index 1a3762dde..04fedc4b1 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java @@ -12,9 +12,11 @@ import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.Column; +import com.avaje.ebean.dbmigration.migration.CreateIndex; import com.avaje.ebean.dbmigration.migration.CreateTable; import com.avaje.ebean.dbmigration.migration.DropColumn; import com.avaje.ebean.dbmigration.migration.DropHistoryTable; +import com.avaje.ebean.dbmigration.migration.DropIndex; import com.avaje.ebean.dbmigration.migration.DropTable; import com.avaje.ebean.dbmigration.migration.ForeignKey; import com.avaje.ebean.dbmigration.migration.UniqueConstraint; @@ -455,6 +457,23 @@ public class BaseTableDdl implements TableDdl { return pk; } + @Override + public void generate(DdlWrite writer, CreateIndex createIndex) throws IOException { + + String[] cols = toColumnNamesSplit(createIndex.getColumns()); + writer.apply() + .append(platformDdl.createIndex(createIndex.getIndexName(), createIndex.getTableName(), cols)) + .endOfStatement(); + } + + @Override + public void generate(DdlWrite writer, DropIndex dropIndex) throws IOException { + + writer.apply() + .append(platformDdl.dropIndex(dropIndex.getIndexName(), dropIndex.getTableName())) + .endOfStatement(); + } + /** * Add add history table DDL. */ diff --git a/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java b/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java index 801769bb2..9cefa6407 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java @@ -55,7 +55,9 @@ public class ChangeSet { @XmlElement(name = "addColumn", type = AddColumn.class), @XmlElement(name = "dropColumn", type = DropColumn.class), @XmlElement(name = "alterColumn", type = AlterColumn.class), - @XmlElement(name = "renameColumn", type = RenameColumn.class) + @XmlElement(name = "renameColumn", type = RenameColumn.class), + @XmlElement(name = "createIndex", type = CreateIndex.class), + @XmlElement(name = "dropIndex", type = DropIndex.class) }) protected List changeSetChildren; @XmlAttribute(name = "type", required = true) @@ -96,6 +98,8 @@ public class ChangeSet { * {@link DropColumn } * {@link AlterColumn } * {@link RenameColumn } + * {@link CreateIndex } + * {@link DropIndex } * * */ diff --git a/src/main/java/com/avaje/ebean/dbmigration/migration/ObjectFactory.java b/src/main/java/com/avaje/ebean/dbmigration/migration/ObjectFactory.java index af430d4fb..c285edd1d 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/ObjectFactory.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/ObjectFactory.java @@ -133,6 +133,14 @@ public class ObjectFactory { return new DropColumn(); } + /** + * Create an instance of {@link CreateIndex } + * + */ + public CreateIndex createCreateIndex() { + return new CreateIndex(); + } + /** * Create an instance of {@link ChangeSet } * @@ -173,6 +181,14 @@ public class ObjectFactory { return new RenameColumn(); } + /** + * Create an instance of {@link DropIndex } + * + */ + public DropIndex createDropIndex() { + return new DropIndex(); + } + /** * Create an instance of {@link AlterHistoryTable } * diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/MIndex.java b/src/main/java/com/avaje/ebean/dbmigration/model/MIndex.java index 5b3810771..0384e6278 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MIndex.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MIndex.java @@ -1,7 +1,131 @@ package com.avaje.ebean.dbmigration.model; +import com.avaje.ebean.dbmigration.migration.CreateIndex; +import com.avaje.ebean.dbmigration.migration.DropIndex; + +import java.util.ArrayList; +import java.util.List; + /** * Index as part of the logical model. */ public class MIndex { + + private String tableName; + + private String indexName; + + private List columns = new ArrayList(); + + /** + * Create a single column non unique index. + */ + public MIndex(String indexName, String tableName, String columnName) { + this.tableName = tableName; + this.indexName = indexName; + this.columns.add(columnName); + } + + public MIndex(CreateIndex createIndex) { + this.indexName = createIndex.getIndexName(); + this.tableName = createIndex.getTableName(); + this.columns = split(createIndex.getColumns()); + } + + /** + * Return the index name. + */ + public String getIndexName() { + return indexName; + } + + /** + * Return the table this index is on. + */ + public String getTableName() { + return tableName; + } + + /** + * Return the columns in the index (in order). + */ + public List getColumns() { + return columns; + } + + /** + * Return a CreateIndex migration for this index. + */ + public CreateIndex createIndex() { + CreateIndex create = new CreateIndex(); + create.setIndexName(indexName); + create.setTableName(tableName); + create.setColumns(join()); + return create; + } + + /** + * Create a DropIndex migration for this index. + */ + public DropIndex dropIndex() { + DropIndex dropIndex = new DropIndex(); + dropIndex.setIndexName(indexName); + dropIndex.setTableName(tableName); + return dropIndex; + } + + /** + * Compare with an index of the same name. + */ + public void compare(ModelDiff modelDiff, MIndex newIndex) { + + if (changed(newIndex)) { + // drop and recreate the index + modelDiff.addDropIndex(dropIndex()); + modelDiff.addCreateIndex(newIndex.createIndex()); + } + } + + /** + * Return true if the index has changed. + */ + private boolean changed(MIndex newIndex) { + + if (!tableName.equals(newIndex.getTableName())) { + return true; + } + List newColumns = newIndex.getColumns(); + if (columns.size() != newColumns.size()) { + return true; + } + for (int i = 0; i < columns.size(); i++) { + if (!columns.get(i).equals(newColumns.get(i))) { + return true; + } + } + return false; + } + + + private List split(String columns) { + + List colList = new ArrayList(); + String[] cols = columns.split(","); + for (int i = 0; i 0) { + sb.append(","); + } + sb.append(columns.get(i)); + } + return sb.toString(); + } + } diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java index 8d576280a..8707ae650 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java @@ -7,6 +7,7 @@ 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.DropHistoryTable; +import com.avaje.ebean.dbmigration.migration.DropTable; import com.avaje.ebean.dbmigration.migration.IdentityType; import java.math.BigInteger; @@ -132,6 +133,15 @@ public class MTable { this.name = name; } + /** + * Return the DropTable migration for this table. + */ + public DropTable dropTable() { + DropTable dropTable = new DropTable(); + dropTable.setName(name); + return dropTable; + } + /** * Return the CreateTable migration for this table. */ @@ -176,14 +186,10 @@ public class MTable { } else { AddHistoryTable addHistoryTable = new AddHistoryTable(); addHistoryTable.setBaseTable(name); - //addHistoryTable.setWhenCreatedColumn(); modelDiff.addAddHistoryTable(addHistoryTable); } } - // TODO: compare indexes? - // TODO: compare primary key - addColumn = null; Set mappedColumns = new LinkedHashSet(); diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java b/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java index 5ce9a7b81..5c8f5a85f 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java @@ -3,8 +3,11 @@ package com.avaje.ebean.dbmigration.model; import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.ChangeSet; +import com.avaje.ebean.dbmigration.migration.CreateIndex; import com.avaje.ebean.dbmigration.migration.CreateTable; import com.avaje.ebean.dbmigration.migration.DropColumn; +import com.avaje.ebean.dbmigration.migration.DropIndex; +import com.avaje.ebean.dbmigration.migration.DropTable; import com.avaje.ebean.dbmigration.migration.Migration; import java.util.LinkedHashMap; @@ -24,6 +27,11 @@ public class ModelContainer { */ private Map tables = new LinkedHashMap(); + /** + * All the non unique non foreign key indexes. + */ + private Map indexes = new LinkedHashMap(); + public ModelContainer() { } @@ -35,6 +43,13 @@ public class ModelContainer { return tables; } + /** + * Return the map of all the non unique non fk indexes. + */ + public Map getIndexes() { + return indexes; + } + /** * Return the table by name. */ @@ -42,6 +57,12 @@ public class ModelContainer { return tables.get(tableName); } + /** + * Return the index by name. + */ + public MIndex getIndex(String indexName) { + return indexes.get(indexName); + } /** * Apply a migration with associated changeSets to the model. @@ -63,12 +84,18 @@ public class ModelContainer { for (Object change : changeSetChildren) { if (change instanceof CreateTable) { applyChange((CreateTable) change); + } else if (change instanceof DropTable) { + applyChange((DropTable) change); } else if (change instanceof AlterColumn) { applyChange((AlterColumn) change); } else if (change instanceof AddColumn) { applyChange((AddColumn) change); } else if (change instanceof DropColumn) { applyChange((DropColumn) change); + } else if (change instanceof CreateIndex) { + applyChange((CreateIndex) change); + } else if (change instanceof DropIndex) { + applyChange((DropIndex) change); } } } @@ -85,6 +112,41 @@ public class ModelContainer { tables.put(tableName, table); } + /** + * Apply a DropTable change to the model. + */ + protected void applyChange(DropTable dropTable) { + String tableName = dropTable.getName(); + if (!tables.containsKey(tableName)) { + throw new IllegalStateException("Table [" + tableName + "] does not exists in model?"); + } + tables.remove(tableName); + } + + /** + * Apply a CreateTable change to the model. + */ + protected void applyChange(CreateIndex createIndex) { + String indexName = createIndex.getIndexName(); + if (indexes.containsKey(indexName)) { + throw new IllegalStateException("Index [" + indexName + "] already exists in model?"); + } + MIndex index = new MIndex(createIndex); + indexes.put(createIndex.getIndexName(), index); + } + + /** + * Apply a DropTable change to the model. + */ + protected void applyChange(DropIndex dropIndex) { + String name = dropIndex.getIndexName(); + if (!indexes.containsKey(name)) { + throw new IllegalStateException("Index [" + name + "] does not exist in model?"); + } + indexes.remove(name); + } + + /** * Apply a AddColumn change to the model. */ @@ -124,4 +186,12 @@ public class ModelContainer { public void addTable(MTable table) { tables.put(table.getName(), table); } + + /** + * Add a single column index. + */ + public void addIndex(String indexName, String tableName, String columnName) { + + indexes.put(indexName, new MIndex(indexName, tableName, columnName)); + } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/ModelDiff.java b/src/main/java/com/avaje/ebean/dbmigration/model/ModelDiff.java index b9d928f3b..0f2c014b3 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/ModelDiff.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/ModelDiff.java @@ -5,12 +5,15 @@ import com.avaje.ebean.dbmigration.migration.AddHistoryTable; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.ChangeSet; import com.avaje.ebean.dbmigration.migration.ChangeSetType; +import com.avaje.ebean.dbmigration.migration.CreateIndex; import com.avaje.ebean.dbmigration.migration.DropColumn; import com.avaje.ebean.dbmigration.migration.DropHistoryTable; +import com.avaje.ebean.dbmigration.migration.DropIndex; import com.avaje.ebean.dbmigration.migration.Migration; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * Used to prepare a diff in terms of changes required to migrate from @@ -114,7 +117,8 @@ public class ModelDiff { */ public void compareTo(ModelContainer newModel) { - for (MTable newTable : newModel.getTables().values()) { + Map newTables = newModel.getTables(); + for (MTable newTable : newTables.values()) { MTable currentTable = baseModel.getTable(newTable.getName()); if (currentTable == null) { @@ -124,15 +128,40 @@ public class ModelDiff { } } - //TODO: other parts of the model? views, indexes etc + // search for tables that are no longer used + for (MTable existingTable : baseModel.getTables().values()) { + if (!newTables.containsKey(existingTable.getName())) { + addDropTable(existingTable); + } + } + + Map newIndexes = newModel.getIndexes(); + for (MIndex newIndex : newIndexes.values()) { + MIndex currentIndex = baseModel.getIndex(newIndex.getIndexName()); + if (currentIndex == null) { + addCreateIndex(newIndex.createIndex()); + } else { + compareIndexes(currentIndex, newIndex); + } + } + + // search for indexes that are no longer used + for (MIndex existingIndex : baseModel.getIndexes().values()) { + if (!newIndexes.containsKey(existingIndex.getIndexName())) { + addDropIndex(existingIndex.dropIndex()); + } + } } + protected void addDropTable(MTable existingTable) { + dropChanges.add(existingTable.dropTable()); + } + /** - * Add CreateTable to the 'creation' changes. + * Add CreateTable to the 'apply' changes. */ protected void addNewTable(MTable newTable) { - createChanges.add(newTable.createTable()); } @@ -144,6 +173,14 @@ public class ModelDiff { currentTable.compare(this, newTable); } + /** + * Compare tables looking for add/drop/modify columns etc. + */ + protected void compareIndexes(MIndex currentIndex, MIndex newIndex) { + + currentIndex.compare(this, newIndex); + } + /** * Add the AlterColumn to the 'apply' changes. */ @@ -179,4 +216,17 @@ public class ModelDiff { dropHistoryChanges.add(dropHistoryTable); } + /** + * Add the DropIndex to the 'apply' changes. + */ + public void addDropIndex(DropIndex dropIndex) { + createChanges.add(dropIndex); + } + + /** + * Add the CreateIndex to the 'apply' changes. + */ + public void addCreateIndex(CreateIndex createIndex) { + createChanges.add(createIndex); + } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java index 3dd9e9c4d..aa27311c7 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java @@ -47,6 +47,14 @@ public class ModelBuildContext { return maxLength(constraintNaming.foreignKeyIndexName(tableName, column), indexCount); } + public String indexName(String tableName, String column, int indexCount) { + return maxLength(constraintNaming.indexName(tableName, column), indexCount); + } + + public String indexName(String tableName, String[] columns, int indexCount) { + return maxLength(constraintNaming.indexName(tableName, columns), indexCount); + } + public String uniqueConstraintName(String tableName, String columnName, int indexCount) { return maxLength(constraintNaming.uniqueConstraintName(tableName, columnName), indexCount); } @@ -63,6 +71,10 @@ public class ModelBuildContext { model.addTable(table); } + public void addIndex(String indexName, String tableName, String columnName) { + model.addIndex(indexName, tableName, columnName); + } + private String maxLength(String constraintName, int indexCount) { return maxLength.maxLength(constraintName, indexCount); } diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java index 0ee8e0d8d..b3817d13e 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java @@ -199,14 +199,14 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { MColumn col = new MColumn(p.getDbColumn(), ctx.getColumnDefn(p)); - if (p.isId()){ + if (p.isId()) { col.setPrimaryKey(true); if (p.getBeanDescriptor().isUseIdGenerator()) { col.setIdentity(true); } - } else if (!p.isNullable() || p.isDDLNotNull()) { + } else if (!p.isNullable() || p.isDDLNotNull()) { col.setNotnull(true); - } + } if (p.isUnique() && !p.isId()) { col.setUnique(determineUniqueConstraintName(col.getName())); @@ -218,6 +218,15 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { col.setCheckConstraintName(determineCheckConstraintName(col.getName())); } + String indexName = p.getIndexName(); + if (indexName != null) { + // single column non-unique index + if (indexName.trim().isEmpty()) { + indexName = determineIndexName(col.getName()); + } + ctx.addIndex(indexName, table.getName(), p.getDbColumn()); + } + lastColumn = col; table.addColumn(col); } @@ -265,6 +274,22 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { return ctx.foreignKeyIndexName(table.getName(), columns, ++countIndex); } + /** + * Return the index name given a single column foreign key. + */ + protected String determineIndexName(String column) { + + return ctx.indexName(table.getName(), column, ++countIndex); + } + + /** + * Return the index name given multiple columns. + */ + protected String determineIndexName(String[] columns) { + + return ctx.indexName(table.getName(), columns, ++countIndex); + } + /** * Return the unique constraint name. */ diff --git a/src/main/resources/ebean-dbmigration-1.0.xsd b/src/main/resources/ebean-dbmigration-1.0.xsd index 61328889e..b0a9e8275 100644 --- a/src/main/resources/ebean-dbmigration-1.0.xsd +++ b/src/main/resources/ebean-dbmigration-1.0.xsd @@ -2,27 +2,6 @@ xmlns="http://ebean-orm.github.io/xml/ns/dbmigration" targetNamespace="http://ebean-orm.github.io/xml/ns/dbmigration" elementFormDefault="qualified"> - - - - - - - - - - - - - - - - - - - - - @@ -245,51 +224,23 @@ - + - - - - - - - - - - + + + + + + + - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -337,25 +288,8 @@ - - - - - - - - - - - - - - - - - - - + +