From b8ecbd507e7217dd5b2bf7ce90054e35336df8ce Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Thu, 12 May 2016 16:52:22 +1200 Subject: [PATCH] #522 - DDL - DB migration diff does not include DDL to add comment --- .../ddlgeneration/BaseDdlHandler.java | 8 ++ .../dbmigration/ddlgeneration/DdlHandler.java | 4 + .../dbmigration/ddlgeneration/TableDdl.java | 6 ++ .../ddlgeneration/platform/BaseTableDdl.java | 16 +++- .../migration/AddTableComment.java | 87 +++++++++++++++++++ .../dbmigration/migration/AlterColumn.java | 27 ++++++ .../dbmigration/migration/ChangeSet.java | 2 + .../dbmigration/migration/ObjectFactory.java | 8 ++ .../ebean/dbmigration/model/MColumn.java | 7 +- .../avaje/ebean/dbmigration/model/MTable.java | 8 ++ .../ebean/dbmigration/model/ModelDiff.java | 8 ++ src/main/resources/ebean-dbmigration-1.0.xsd | 9 ++ .../platform/BaseTableDdlTest.java | 36 ++++++++ 13 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/avaje/ebean/dbmigration/migration/AddTableComment.java 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 bb3d8ed5b..c1e27faf0 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java @@ -5,6 +5,7 @@ import com.avaje.ebean.dbmigration.ddlgeneration.platform.BaseTableDdl; import com.avaje.ebean.dbmigration.ddlgeneration.platform.PlatformDdl; import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; +import com.avaje.ebean.dbmigration.migration.AddTableComment; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.ChangeSet; import com.avaje.ebean.dbmigration.migration.CreateIndex; @@ -37,6 +38,8 @@ public class BaseDdlHandler implements DdlHandler { generate(writer, (CreateTable) change); } else if (change instanceof DropTable) { generate(writer, (DropTable) change); + } else if (change instanceof AddTableComment) { + generate(writer, (AddTableComment) change); } else if (change instanceof CreateIndex) { generate(writer, (CreateIndex) change); } else if (change instanceof DropIndex) { @@ -70,6 +73,11 @@ public class BaseDdlHandler implements DdlHandler { tableDdl.generate(writer, dropTable); } + @Override + public void generate(DdlWrite writer, AddTableComment addTableComment) throws IOException { + tableDdl.generate(writer, addTableComment); + } + @Override public void generate(DdlWrite writer, AddColumn addColumn) throws IOException { tableDdl.generate(writer, addColumn); 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 2f929122c..0285fa47e 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/DdlHandler.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/DdlHandler.java @@ -2,6 +2,7 @@ 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.AddTableComment; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.ChangeSet; import com.avaje.ebean.dbmigration.migration.CreateIndex; @@ -14,6 +15,7 @@ import com.avaje.ebean.dbmigration.migration.DropTable; import java.io.IOException; /** + * DDL generation interface. */ public interface DdlHandler { @@ -23,6 +25,8 @@ public interface DdlHandler { void generate(DdlWrite writer, DropTable dropTable) throws IOException; + void generate(DdlWrite writer, AddTableComment addTableComment) throws IOException; + void generate(DdlWrite writer, AddColumn addColumn) throws IOException; void generate(DdlWrite writer, DropColumn dropColumn) throws IOException; 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 1fa2b2e9f..ba080cae2 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java @@ -2,6 +2,7 @@ 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.AddTableComment; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.CreateIndex; import com.avaje.ebean.dbmigration.migration.CreateTable; @@ -42,6 +43,11 @@ public interface TableDdl { */ void generate(DdlWrite writer, DropColumn dropColumn) throws IOException; + /** + * Write the AddTableComment change. + */ + void generate(DdlWrite writer, AddTableComment addTableComment) throws IOException; + /** * Write the AddHistoryTable change. */ 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 cb26d5433..c20d09fe5 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 @@ -10,6 +10,7 @@ import com.avaje.ebean.dbmigration.ddlgeneration.TableDdl; import com.avaje.ebean.dbmigration.ddlgeneration.platform.util.IndexSet; import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; +import com.avaje.ebean.dbmigration.migration.AddTableComment; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.Column; import com.avaje.ebean.dbmigration.migration.CreateIndex; @@ -554,6 +555,13 @@ public class BaseTableDdl implements TableDdl { } } + @Override + public void generate(DdlWrite writer, AddTableComment addTableComment) throws IOException { + if (hasValue(addTableComment.getComment())) { + platformDdl.addTableComment(writer.apply(), addTableComment.getName(), addTableComment.getComment()); + } + } + /** * Add add column DDL. */ @@ -634,7 +642,9 @@ public class BaseTableDdl implements TableDdl { if (hasValue(alterColumn.getUniqueOneToOne())) { alterColumnAddUniqueOneToOneConstraint(writer, alterColumn); } - + if (hasValue(alterColumn.getComment())) { + alterColumnComment(writer, alterColumn); + } boolean alterCheckConstraint = hasValue(alterColumn.getCheckConstraint()); @@ -664,6 +674,10 @@ public class BaseTableDdl implements TableDdl { } } + private void alterColumnComment(DdlWrite writer, AlterColumn alterColumn) throws IOException { + platformDdl.addColumnComment(writer.apply(), alterColumn.getTableName(), alterColumn.getColumnName(), alterColumn.getComment()); + } + /** * Return the name of the history table given the base table name. */ diff --git a/src/main/java/com/avaje/ebean/dbmigration/migration/AddTableComment.java b/src/main/java/com/avaje/ebean/dbmigration/migration/AddTableComment.java new file mode 100644 index 000000000..40d50c625 --- /dev/null +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/AddTableComment.java @@ -0,0 +1,87 @@ + +package com.avaje.ebean.dbmigration.migration; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType>
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
+ *       <attribute name="comment" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "") +@XmlRootElement(name = "addTableComment") +public class AddTableComment { + + @XmlAttribute(name = "name", required = true) + protected String name; + @XmlAttribute(name = "comment", required = true) + protected String comment; + + /** + * 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 comment property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getComment() { + return comment; + } + + /** + * Sets the value of the comment property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setComment(String value) { + this.comment = value; + } + +} diff --git a/src/main/java/com/avaje/ebean/dbmigration/migration/AlterColumn.java b/src/main/java/com/avaje/ebean/dbmigration/migration/AlterColumn.java index 90c86842a..7f8c683ca 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/AlterColumn.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/AlterColumn.java @@ -26,6 +26,7 @@ import javax.xml.bind.annotation.XmlType; * <attribute name="currentDefaultValue" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="notnull" type="{http://www.w3.org/2001/XMLSchema}boolean" /> * <attribute name="currentNotnull" type="{http://www.w3.org/2001/XMLSchema}boolean" /> + * <attribute name="comment" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="historyExclude" type="{http://www.w3.org/2001/XMLSchema}boolean" /> * <attribute name="checkConstraint" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="checkConstraintName" type="{http://www.w3.org/2001/XMLSchema}string" /> @@ -68,6 +69,8 @@ public class AlterColumn { protected Boolean notnull; @XmlAttribute(name = "currentNotnull") protected Boolean currentNotnull; + @XmlAttribute(name = "comment") + protected String comment; @XmlAttribute(name = "historyExclude") protected Boolean historyExclude; @XmlAttribute(name = "checkConstraint") @@ -309,6 +312,30 @@ public class AlterColumn { this.currentNotnull = value; } + /** + * Gets the value of the comment property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getComment() { + return comment; + } + + /** + * Sets the value of the comment property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setComment(String value) { + this.comment = value; + } + /** * Gets the value of the historyExclude property. * 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 4db9e88c2..8341525c5 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java @@ -52,6 +52,7 @@ public class ChangeSet { @XmlElement(name = "createTable", type = CreateTable.class), @XmlElement(name = "dropTable", type = DropTable.class), @XmlElement(name = "renameTable", type = RenameTable.class), + @XmlElement(name = "addTableComment", type = AddTableComment.class), @XmlElement(name = "addHistoryTable", type = AddHistoryTable.class), @XmlElement(name = "dropHistoryTable", type = DropHistoryTable.class), @XmlElement(name = "addColumn", type = AddColumn.class), @@ -98,6 +99,7 @@ public class ChangeSet { * {@link CreateTable } * {@link DropTable } * {@link RenameTable } + * {@link AddTableComment } * {@link AddHistoryTable } * {@link DropHistoryTable } * {@link AddColumn } 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 c285edd1d..632e62006 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/ObjectFactory.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/ObjectFactory.java @@ -101,6 +101,14 @@ public class ObjectFactory { return new DefaultTablespace(); } + /** + * Create an instance of {@link AddTableComment } + * + */ + public AddTableComment createAddTableComment() { + return new AddTableComment(); + } + /** * Create an instance of {@link RenameTable } * diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/MColumn.java b/src/main/java/com/avaje/ebean/dbmigration/model/MColumn.java index 20e5eb3a0..286261ca7 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MColumn.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MColumn.java @@ -260,7 +260,7 @@ public class MColumn { return c; } - private boolean different(String val1, String val2) { + protected static boolean different(String val1, String val2) { return (val1 == null) ? val2 != null : !val1.equals(val2); } @@ -314,7 +314,10 @@ public class MColumn { alter.setDefaultValue(newColumn.defaultValue); } } - + if (different(comment, newColumn.comment)) { + AlterColumn alter = getAlterColumn(tableName, tableWithHistory); + alter.setComment(newColumn.comment); + } if (different(checkConstraint, newColumn.checkConstraint)) { AlterColumn alter = getAlterColumn(tableName, tableWithHistory); if (hasValue(checkConstraint) && !hasValue(newColumn.checkConstraint)) { 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 343d8f65b..d1753f6a5 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java @@ -2,6 +2,7 @@ package com.avaje.ebean.dbmigration.model; import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; +import com.avaje.ebean.dbmigration.migration.AddTableComment; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.Column; import com.avaje.ebean.dbmigration.migration.CreateTable; @@ -285,6 +286,13 @@ public class MTable { if (addColumn != null) { modelDiff.addAddColumn(addColumn); } + + if (MColumn.different(comment, newTable.comment)) { + AddTableComment addTableComment = new AddTableComment(); + addTableComment.setName(name); + addTableComment.setComment(newTable.comment); + modelDiff.addTableComment(addTableComment); + } } /** 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 f851143b2..da50becb7 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/ModelDiff.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/ModelDiff.java @@ -2,6 +2,7 @@ package com.avaje.ebean.dbmigration.model; import com.avaje.ebean.dbmigration.migration.AddColumn; import com.avaje.ebean.dbmigration.migration.AddHistoryTable; +import com.avaje.ebean.dbmigration.migration.AddTableComment; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.ChangeSet; import com.avaje.ebean.dbmigration.migration.ChangeSetType; @@ -237,4 +238,11 @@ public class ModelDiff { public void addCreateIndex(CreateIndex createIndex) { applyChanges.add(createIndex); } + + /** + * Add a table comment to the 'apply' changes. + */ + public void addTableComment(AddTableComment addTableComment) { + applyChanges.add(addTableComment); + } } diff --git a/src/main/resources/ebean-dbmigration-1.0.xsd b/src/main/resources/ebean-dbmigration-1.0.xsd index 2ada00820..4233f28ef 100644 --- a/src/main/resources/ebean-dbmigration-1.0.xsd +++ b/src/main/resources/ebean-dbmigration-1.0.xsd @@ -152,6 +152,13 @@ + + + + + + + @@ -203,6 +210,7 @@ + @@ -282,6 +290,7 @@ + diff --git a/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdlTest.java b/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdlTest.java index 4857c0b13..3573dcfbb 100644 --- a/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdlTest.java +++ b/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdlTest.java @@ -5,6 +5,7 @@ import com.avaje.ebean.config.ServerConfig; import com.avaje.ebean.config.dbplatform.H2Platform; import com.avaje.ebean.dbmigration.ddlgeneration.DdlWrite; import com.avaje.ebean.dbmigration.ddlgeneration.Helper; +import com.avaje.ebean.dbmigration.migration.AddTableComment; import com.avaje.ebean.dbmigration.migration.AlterColumn; import com.avaje.ebean.dbmigration.migration.Column; import com.avaje.ebean.dbmigration.migration.CreateTable; @@ -39,6 +40,41 @@ public class BaseTableDdlTest { assertThat(ddl).contains("alter table mytab add constraint ck_mytab_acol check (acol in ('A','B'))"); } + @Test + public void testAlterColumnComment() throws IOException { + + BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, new H2Platform().getPlatformDdl()); + + DdlWrite write = new DdlWrite(); + + AlterColumn alterColumn = new AlterColumn(); + alterColumn.setTableName("mytab"); + alterColumn.setColumnName("acol"); + alterColumn.setComment("my comment"); + + ddlGen.generate(write, alterColumn); + + String ddl = write.apply().getBuffer(); + assertThat(ddl).contains("comment on column mytab.acol is 'my comment'"); + } + + @Test + public void testAddTableComment() throws IOException { + + BaseTableDdl ddlGen = new BaseTableDdl(serverConfig, new H2Platform().getPlatformDdl()); + + DdlWrite write = new DdlWrite(); + + AddTableComment addTableComment = new AddTableComment(); + addTableComment.setName("mytab"); + addTableComment.setComment("my comment"); + + ddlGen.generate(write, addTableComment); + + String ddl = write.apply().getBuffer(); + assertThat(ddl).contains("comment on table mytab is 'my comment'"); + } + @Test public void testGenerate() throws Exception {