From f29ba8383e59056a8c958d91aca7039eeac91514 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Sun, 16 Aug 2015 20:19:52 +1200 Subject: [PATCH] #374 - Find whenCreatedColumn and use as default effective start when adding history --- .../platform/PostgresHistoryDdl.java | 12 ++++++--- .../migration/AddHistoryTable.java | 27 ------------------- .../avaje/ebean/dbmigration/model/MTable.java | 11 ++++++++ .../model/build/ModelBuildBeanVisitor.java | 5 ++++ src/main/resources/ebean-dbmigration-1.0.xsd | 1 - 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java index 0fdeba4b2..5374e247f 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java @@ -73,12 +73,13 @@ public class PostgresHistoryDdl implements PlatformHistoryDdl { public void createWithHistory(DdlWrite writer, MTable table) throws IOException { String baseTable = table.getName(); + String whenCreatedColumn = table.getWhenCreatedColumn(); // rollback changes in appropriate order dropTriggersEtc(writer.rollback(), baseTable); dropHistoryTableEtc(writer.rollback(), baseTable); - addHistoryTable(writer, table); + addHistoryTable(writer, table, whenCreatedColumn); addStoredFunction(writer, table); addTrigger(writer, table); } @@ -107,15 +108,20 @@ public class PostgresHistoryDdl implements PlatformHistoryDdl { buffer.end(); } - protected void addHistoryTable(DdlWrite writer, MTable table) throws IOException { + protected void addHistoryTable(DdlWrite writer, MTable table, String whenCreatedColumn) throws IOException { String baseTableName = table.getName(); DdlBuffer apply = writer.applyHistory(); + if (whenCreatedColumn == null) { + // effective history start as at current timestamp + whenCreatedColumn = "current_timestamp"; + } + apply .append("alter table ").append(baseTableName) - .append(" add column ").append(sysPeriod).append(" tstzrange not null") + .append(" add column ").append(sysPeriod).append(" tstzrange not null default tstzrange(").append(whenCreatedColumn).append(", null)") .endOfStatement(); apply diff --git a/src/main/java/com/avaje/ebean/dbmigration/migration/AddHistoryTable.java b/src/main/java/com/avaje/ebean/dbmigration/migration/AddHistoryTable.java index 867f3ce6e..1ea167f4e 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/AddHistoryTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/AddHistoryTable.java @@ -18,7 +18,6 @@ import javax.xml.bind.annotation.XmlType; * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <attribute name="baseTable" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> - * <attribute name="whenCreatedColumn" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> @@ -33,8 +32,6 @@ public class AddHistoryTable { @XmlAttribute(name = "baseTable", required = true) protected String baseTable; - @XmlAttribute(name = "whenCreatedColumn") - protected String whenCreatedColumn; /** * Gets the value of the baseTable property. @@ -60,28 +57,4 @@ public class AddHistoryTable { this.baseTable = value; } - /** - * Gets the value of the whenCreatedColumn property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getWhenCreatedColumn() { - return whenCreatedColumn; - } - - /** - * Sets the value of the whenCreatedColumn property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setWhenCreatedColumn(String value) { - this.whenCreatedColumn = value; - } - } 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 e70e6f505..e37962aa4 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java @@ -65,6 +65,8 @@ public class MTable { private AddColumn addColumn; + private String whenCreatedColumn; + /** * Construct for migration. */ @@ -266,6 +268,14 @@ public class MTable { this.sequenceAllocate = sequenceAllocate; } + public void setWhenCreatedColumn(String whenCreatedColumn) { + this.whenCreatedColumn = whenCreatedColumn; + } + + public String getWhenCreatedColumn() { + return whenCreatedColumn; + } + /** * Set the identity type to use for this table. *

@@ -397,4 +407,5 @@ public class MTable { modelDiff.addDropColumn(dropColumn); } + } diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildBeanVisitor.java b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildBeanVisitor.java index 1dc15ad9f..5c23dee93 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildBeanVisitor.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildBeanVisitor.java @@ -8,6 +8,7 @@ import com.avaje.ebean.dbmigration.model.MTable; import com.avaje.ebean.dbmigration.model.visitor.BeanPropertyVisitor; import com.avaje.ebean.dbmigration.model.visitor.BeanVisitor; import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; +import com.avaje.ebeaninternal.server.deploy.BeanProperty; import com.avaje.ebeaninternal.server.deploy.InheritInfo; /** @@ -37,6 +38,10 @@ public class ModelBuildBeanVisitor implements BeanVisitor { MTable table = new MTable(descriptor.getBaseTable()); if (descriptor.isHistorySupport()) { table.setWithHistory(); + BeanProperty whenCreated = descriptor.findWhenCreatedProperty(); + if (whenCreated != null) { + table.setWhenCreatedColumn(whenCreated.getDbColumn()); + } } setIdentity(descriptor, table); diff --git a/src/main/resources/ebean-dbmigration-1.0.xsd b/src/main/resources/ebean-dbmigration-1.0.xsd index b11d1d14d..488fbd9ea 100644 --- a/src/main/resources/ebean-dbmigration-1.0.xsd +++ b/src/main/resources/ebean-dbmigration-1.0.xsd @@ -175,7 +175,6 @@ -