mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-25 07:57:39 +00:00
#374 - Add DropIndex and CreateIndex migration
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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<Object> changeSetChildren;
|
||||
@XmlAttribute(name = "type", required = true)
|
||||
@@ -96,6 +98,8 @@ public class ChangeSet {
|
||||
* {@link DropColumn }
|
||||
* {@link AlterColumn }
|
||||
* {@link RenameColumn }
|
||||
* {@link CreateIndex }
|
||||
* {@link DropIndex }
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -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 }
|
||||
*
|
||||
|
||||
@@ -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<String> columns = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* 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<String> 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<String> 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<String> split(String columns) {
|
||||
|
||||
List<String> colList = new ArrayList<String>();
|
||||
String[] cols = columns.split(",");
|
||||
for (int i = 0; i <cols.length; i++) {
|
||||
colList.add(cols[i]);
|
||||
}
|
||||
return colList;
|
||||
}
|
||||
|
||||
private String join() {
|
||||
StringBuilder sb = new StringBuilder(50);
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
if (i > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(columns.get(i));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String> mappedColumns = new LinkedHashSet<String>();
|
||||
|
||||
@@ -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<String, MTable> tables = new LinkedHashMap<String, MTable>();
|
||||
|
||||
/**
|
||||
* All the non unique non foreign key indexes.
|
||||
*/
|
||||
private Map<String, MIndex> indexes = new LinkedHashMap<String, MIndex>();
|
||||
|
||||
public ModelContainer() {
|
||||
|
||||
}
|
||||
@@ -35,6 +43,13 @@ public class ModelContainer {
|
||||
return tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the map of all the non unique non fk indexes.
|
||||
*/
|
||||
public Map<String, MIndex> 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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String, MTable> 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<String, MIndex> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+28
-3
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -2,27 +2,6 @@
|
||||
xmlns="http://ebean-orm.github.io/xml/ns/dbmigration"
|
||||
targetNamespace="http://ebean-orm.github.io/xml/ns/dbmigration" elementFormDefault="qualified">
|
||||
|
||||
|
||||
<!-- =========================================================== -->
|
||||
<!-- APPLICATIONS -->
|
||||
<!-- =========================================================== -->
|
||||
|
||||
<!-- Root level type : applications -->
|
||||
<!--<xsd:element name="applications">-->
|
||||
<!--<xsd:complexType>-->
|
||||
<!--<xsd:sequence>-->
|
||||
<!--<xsd:element ref="application" minOccurs="0" maxOccurs="unbounded"/>-->
|
||||
<!--</xsd:sequence>-->
|
||||
<!--</xsd:complexType>-->
|
||||
|
||||
<!--<xsd:element name="application">-->
|
||||
<!--<xsd:complexType>-->
|
||||
<!--<xsd:attribute name="name" type="xsd:string" use="required"/>-->
|
||||
<!--<xsd:attribute name="resourcePath" type="xsd:string" use="required"/>-->
|
||||
<!--</xsd:complexType>-->
|
||||
<!--</xsd:element>-->
|
||||
|
||||
|
||||
<!-- =========================================================== -->
|
||||
<!-- CHANGE LOG -->
|
||||
<!-- =========================================================== -->
|
||||
@@ -245,51 +224,23 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<!-- VIEW -->
|
||||
<!-- INDEXES (Non unique) -->
|
||||
|
||||
<!--<xsd:element name="createView">-->
|
||||
<!--<xsd:complexType>-->
|
||||
<!--<xsd:simpleContent>-->
|
||||
<!--<xsd:extension base="xsd:string">-->
|
||||
<!--<xsd:attribute name="name" type="xsd:string" use="required"/>-->
|
||||
<!--<xsd:attribute name="replaceIfExists" type="xsd:boolean"/>-->
|
||||
<!--</xsd:extension>-->
|
||||
<!--</xsd:simpleContent>-->
|
||||
<!--</xsd:complexType>-->
|
||||
<!--</xsd:element>-->
|
||||
<xsd:element name="createIndex">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="indexName" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="columns" type="xsd:string" use="required"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<!--<xsd:element name="dropView">-->
|
||||
<!--<xsd:complexType>-->
|
||||
<!--<xsd:attribute name="name" type="xsd:string" use="required"/>-->
|
||||
<!--</xsd:complexType>-->
|
||||
<!--</xsd:element>-->
|
||||
<xsd:element name="dropIndex">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="indexName" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<!--<xsd:element name="renameView">-->
|
||||
<!--<xsd:complexType>-->
|
||||
<!--<xsd:attribute name="oldName" type="xsd:string" use="required"/>-->
|
||||
<!--<xsd:attribute name="newName" type="xsd:string" use="required"/>-->
|
||||
<!--</xsd:complexType>-->
|
||||
<!--</xsd:element>-->
|
||||
|
||||
<!-- FOREIGN KEY -->
|
||||
|
||||
<!--<xsd:element name="addForeignKey">-->
|
||||
<!--<xsd:complexType>-->
|
||||
<!--<xsd:simpleContent>-->
|
||||
<!--<xsd:extension base="xsd:string">-->
|
||||
<!--<xsd:attribute name="name" type="xsd:string" use="required"/>-->
|
||||
<!--<xsd:attribute name="columns" type="xsd:string" use="required"/>-->
|
||||
<!--<xsd:attribute name="references" type="xsd:string" use="required"/>-->
|
||||
<!--</xsd:extension>-->
|
||||
<!--</xsd:simpleContent>-->
|
||||
<!--</xsd:complexType>-->
|
||||
<!--</xsd:element>-->
|
||||
|
||||
<!--<xsd:element name="dropForeignKey">-->
|
||||
<!--<xsd:complexType mixed="true">-->
|
||||
<!--<xsd:attribute name="name" type="xsd:string" use="required"/>-->
|
||||
<!--</xsd:complexType>-->
|
||||
<!--</xsd:element>-->
|
||||
|
||||
<!-- ============================================ -->
|
||||
|
||||
@@ -337,25 +288,8 @@
|
||||
<xsd:element ref="alterColumn" maxOccurs="unbounded"/>
|
||||
<xsd:element ref="renameColumn" maxOccurs="unbounded"/>
|
||||
|
||||
<!--<xsd:element ref="createView" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="dropView" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="renameView" maxOccurs="unbounded"/>-->
|
||||
|
||||
<!--<xsd:element ref="addForeignKey" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="dropForeignKey" maxOccurs="unbounded"/>-->
|
||||
|
||||
<!--<xsd:element ref="createIndex" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="dropIndex" maxOccurs="unbounded"/>-->
|
||||
|
||||
<!--<xsd:element ref="createSequence" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="alterSequence" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="dropSequence" maxOccurs="unbounded"/>-->
|
||||
|
||||
<!--<xsd:element ref="addPrimaryKey" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="dropPrimaryKey" maxOccurs="unbounded"/>-->
|
||||
|
||||
<!--<xsd:element ref="addUniqueConstraint" maxOccurs="unbounded"/>-->
|
||||
<!--<xsd:element ref="alterTableDropUniqueConstraint" maxOccurs="unbounded"/>-->
|
||||
<xsd:element ref="createIndex" maxOccurs="unbounded"/>
|
||||
<xsd:element ref="dropIndex" maxOccurs="unbounded"/>
|
||||
|
||||
</xsd:choice>
|
||||
</xsd:group>
|
||||
|
||||
Reference in New Issue
Block a user