mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#374 - Find whenCreatedColumn and use as default effective start when adding history
This commit is contained in:
1 parent
a15dd33380
commit
f29ba8383e
5 files changed
+25
-31
No files matched your search
+9
-3
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
* <p>
|
||||
@@ -397,4 +407,5 @@ public class MTable {
|
||||
|
||||
modelDiff.addDropColumn(dropColumn);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user