diff --git a/src/main/java/com/avaje/ebean/dbmigration/migration/CreateTable.java b/src/main/java/com/avaje/ebean/dbmigration/migration/CreateTable.java index 421e5fb90..dac8198b0 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/CreateTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/CreateTable.java @@ -30,6 +30,7 @@ import javax.xml.bind.annotation.XmlType; * <attGroup ref="{http://ebean-orm.github.io/xml/ns/dbmigration}tablespaceAttributes"/> * <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="withHistory" type="{http://www.w3.org/2001/XMLSchema}boolean" /> + * <attribute name="draft" type="{http://www.w3.org/2001/XMLSchema}boolean" /> * <attribute name="identityType" type="{http://ebean-orm.github.io/xml/ns/dbmigration}identityType" /> * <attribute name="sequenceName" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="sequenceInitial" type="{http://www.w3.org/2001/XMLSchema}positiveInteger" /> @@ -59,6 +60,8 @@ public class CreateTable { protected String name; @XmlAttribute(name = "withHistory") protected Boolean withHistory; + @XmlAttribute(name = "draft") + protected Boolean draft; @XmlAttribute(name = "identityType") protected IdentityType identityType; @XmlAttribute(name = "sequenceName") @@ -213,6 +216,30 @@ public class CreateTable { this.withHistory = value; } + /** + * Gets the value of the draft property. + * + * @return + * possible object is + * {@link Boolean } + * + */ + public Boolean isDraft() { + return draft; + } + + /** + * Sets the value of the draft property. + * + * @param value + * allowed object is + * {@link Boolean } + * + */ + public void setDraft(Boolean value) { + this.draft = value; + } + /** * Gets the value of the identityType property. * 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 e608a5b5e..4c412d4cc 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java @@ -10,13 +10,14 @@ import com.avaje.ebean.dbmigration.migration.DropHistoryTable; import com.avaje.ebean.dbmigration.migration.DropTable; import com.avaje.ebean.dbmigration.migration.IdentityType; import com.avaje.ebean.dbmigration.migration.UniqueConstraint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -38,6 +39,8 @@ import java.util.Set; */ public class MTable { + private static final Logger logger = LoggerFactory.getLogger(MTable.class); + /** * Table name. */ @@ -150,6 +153,7 @@ public class MTable { this.tablespace = createTable.getTablespace(); this.indexTablespace = createTable.getIndexTablespace(); this.withHistory = Boolean.TRUE.equals(createTable.isWithHistory()); + this.draft = Boolean.TRUE.equals(createTable.isDraft()); this.sequenceName = createTable.getSequenceName(); this.sequenceInitial = toInt(createTable.getSequenceInitial()); this.sequenceAllocate = toInt(createTable.getSequenceAllocate()); @@ -194,6 +198,9 @@ public class MTable { if (withHistory) { createTable.setWithHistory(Boolean.TRUE); } + if (draft) { + createTable.setDraft(Boolean.TRUE); + } for (MColumn column : this.columns.values()) { // filter out draftOnly columns from the base table @@ -244,24 +251,29 @@ public class MTable { addColumn = null; - Set mappedColumns = new LinkedHashSet(); + Map newColumnMap = newTable.getColumns(); - Collection newColumns = newTable.getColumns().values(); - for (MColumn newColumn : newColumns) { - MColumn localColumn = columns.get(newColumn.getName()); + // compare newColumns to existing columns (look for new and diff columns) + for (MColumn newColumn : newColumnMap.values()) { + MColumn localColumn = this.columns.get(newColumn.getName()); if (localColumn == null) { - diffNewColumn(newColumn); + // can ignore if draftOnly column and non-draft table + if (!newColumn.isDraftOnly() || draft) { + diffNewColumn(newColumn); + } } else { - // note that if there are alter column changes in here then - // the table withHistory is taken into account localColumn.compare(modelDiff, this, newColumn); - mappedColumns.add(newColumn.getName()); } } - Collection existingColumns = columns.values(); - for (MColumn existingColumn : existingColumns) { - if (!mappedColumns.contains(existingColumn.getName())) { + // compare existing columns (look for dropped columns) + for (MColumn existingColumn : columns.values()) { + MColumn newColumn = newColumnMap.get(existingColumn.getName()); + if (newColumn == null) { + diffDropColumn(modelDiff, existingColumn); + } else if (newColumn.isDraftOnly() && !draft) { + // effectively a drop column (draft only column on a non-draft table) + logger.trace("... drop column {} from table {} as now draftOnly", newColumn.getName(), name); diffDropColumn(modelDiff, existingColumn); } } diff --git a/src/main/resources/ebean-dbmigration-1.0.xsd b/src/main/resources/ebean-dbmigration-1.0.xsd index b0a9e8275..d33e3570b 100644 --- a/src/main/resources/ebean-dbmigration-1.0.xsd +++ b/src/main/resources/ebean-dbmigration-1.0.xsd @@ -98,6 +98,7 @@ +