#374 - Add comments for when a history table trigger/function is regenerated

This commit is contained in:
Robin Bygrave committed 2015-08-17 07:44:59 +12:00
1 parent f29ba8383e
commit b9774e64e9
12 files changed
+309 -55

No files matched your search

@@ -22,9 +22,9 @@ import com.avaje.ebean.dbmigration.model.MTable;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.Map;
/**
* Base implementation for 'create table' and 'alter table' statements.
@@ -61,7 +61,7 @@ public class BaseTableDdl implements TableDdl {
* Base tables that have associated history tables that need their triggers
* regenerated as columns have been added or removed.
*/
protected Set<String> regenerateHistoryTriggers = new LinkedHashSet<String>();
protected Map<String,HistoryTableUpdate> regenerateHistoryTriggers = new LinkedHashMap<String,HistoryTableUpdate>();
/**
* Construct with a naming convention and platform specific DDL.
@@ -164,7 +164,7 @@ public class BaseTableDdl implements TableDdl {
String uqName = col.getUniqueOneToOne();
String[] columnNames = {col.getName()};
write.apply()
.append(platformDdl.createExternalUniqueForOneToOne(uqName, tableName, columnNames))
.append(platformDdl.alterTableAddUniqueConstraint(tableName, uqName, columnNames))
.endOfStatement();
write.rollbackForeignKeys()
@@ -289,7 +289,7 @@ public class BaseTableDdl implements TableDdl {
.endOfStatement();
}
private void appendColumns(String[] columns, DdlBuffer buffer) throws IOException {
protected void appendColumns(String[] columns, DdlBuffer buffer) throws IOException {
buffer.append(" (");
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
@@ -389,7 +389,7 @@ public class BaseTableDdl implements TableDdl {
/**
* Return as an array of string column names.
*/
private String[] toColumnNames(List<Column> columns) {
protected String[] toColumnNames(List<Column> columns) {
String[] cols = new String[columns.size()];
for (int i = 0; i < cols.length; i++) {
@@ -401,7 +401,7 @@ public class BaseTableDdl implements TableDdl {
/**
* Return as an array of string column names.
*/
private String[] toColumnNamesSplit(String columns) {
protected String[] toColumnNamesSplit(String columns) {
return columns.split(",");
}
@@ -458,23 +458,35 @@ public class BaseTableDdl implements TableDdl {
return pk;
}
/**
* Add add history table DDL.
*/
@Override
public void generate(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException {
platformDdl.addHistoryTable(writer, addHistoryTable);
}
/**
* Add drop history table DDL.
*/
@Override
public void generate(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException {
platformDdl.dropHistoryTable(writer, dropHistoryTable);
}
/**
* Called at the end to generate additional ddl such as regenerate history triggers.
*/
@Override
public void generateExtra(DdlWrite write) throws IOException {
for (String baseTable : this.regenerateHistoryTriggers) {
platformDdl.regenerateHistoryTriggers(write, baseTable);
for (HistoryTableUpdate update : this.regenerateHistoryTriggers.values()) {
platformDdl.regenerateHistoryTriggers(write, update);
}
}
/**
* Add add column DDL.
*/
@Override
public void generate(DdlWrite writer, AddColumn addColumn) throws IOException {
@@ -488,8 +500,8 @@ public class BaseTableDdl implements TableDdl {
if (isTrue(addColumn.isWithHistory())) {
// make same changes to the history table
String historyTable = historyTable(tableName);
regenerateHistoryTriggers(tableName);
for (Column column : columns) {
regenerateHistoryTriggers(tableName, "added " + column.getName());
alterTableAddColumn(writer.apply(), historyTable, column, true);
alterTableDropColumn(writer.rollback(), historyTable, column.getName());
}
@@ -500,12 +512,18 @@ public class BaseTableDdl implements TableDdl {
writer.rollback().end();
}
/**
* Add drop table DDL.
*/
@Override
public void generate(DdlWrite writer, DropTable dropTable) throws IOException {
dropTable(writer.drop(), dropTable.getName());
}
/**
* Add drop column DDL.
*/
@Override
public void generate(DdlWrite writer, DropColumn dropColumn) throws IOException {
@@ -514,21 +532,24 @@ public class BaseTableDdl implements TableDdl {
alterTableDropColumn(writer.drop(), tableName, dropColumn.getColumnName());
if (isTrue(dropColumn.isWithHistory())) {
// also drop from the history table
regenerateHistoryTriggers(tableName);
regenerateHistoryTriggers(tableName, "dropped "+dropColumn.getColumnName());
alterTableDropColumn(writer.drop(), historyTable(tableName), dropColumn.getColumnName());
}
writer.drop().end();
}
/**
* Add all the appropriate changes based on the column changes.
*/
@Override
public void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException {
// if (isTrue(alterColumn.isHistoryExclude())) {
// historyExcludeColumn(writer, alterColumn);
// } else if (isFalse(alterColumn.isHistoryExclude())) {
// historyIncludeColumn(writer, alterColumn);
// }
if (isTrue(alterColumn.isHistoryExclude())) {
regenerateHistoryTriggers(alterColumn.getTableName(), "exclude " + alterColumn.getColumnName());
} else if (isFalse(alterColumn.isHistoryExclude())) {
regenerateHistoryTriggers(alterColumn.getTableName(), "include " + alterColumn.getColumnName());
}
if (hasValue(alterColumn.getDropForeignKey())) {
alterColumnDropForeignKey(writer, alterColumn);
@@ -566,6 +587,9 @@ public class BaseTableDdl implements TableDdl {
}
}
/**
* Return the name of the history table given the base table name.
*/
protected String historyTable(String baseTable) {
return baseTable + historyTableSuffix;
}
@@ -573,8 +597,14 @@ public class BaseTableDdl implements TableDdl {
/**
* Register the base table that we need to regenerate the history triggers on.
*/
protected void regenerateHistoryTriggers(String baseTableName) {
regenerateHistoryTriggers.add(baseTableName);
protected void regenerateHistoryTriggers(String baseTableName, String columnComment) {
HistoryTableUpdate update = regenerateHistoryTriggers.get(baseTableName);
if (update == null) {
update = new HistoryTableUpdate(baseTableName);
regenerateHistoryTriggers.put(baseTableName, update);
}
update.addComment(columnComment);
}
/**
@@ -586,7 +616,7 @@ public class BaseTableDdl implements TableDdl {
if (hasValue(ddl)) {
writer.apply().append(ddl).endOfStatement();
if (isTrue(alter.isWithHistory()) && alter.getType() != null) {
// mysql and sqlserver column type change allowing nulls in the history table column
// mysql and sql server column type change allowing nulls in the history table column
AlterColumn alterHistoryColumn = new AlterColumn();
alterHistoryColumn.setTableName(historyTable(alter.getTableName()));
alterHistoryColumn.setColumnName(alter.getColumnName());
@@ -655,7 +685,7 @@ public class BaseTableDdl implements TableDdl {
protected void alterColumnDropUniqueConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
writer.apply()
.append(platformDdl.dropIndex(alter.getDropUnique(), alter.getTableName()))
.append(platformDdl.alterTableDropUniqueConstraint(alter.getTableName(), alter.getDropUnique()))
.endOfStatement();
}
@@ -674,7 +704,7 @@ public class BaseTableDdl implements TableDdl {
String[] cols = {alter.getColumnName()};
writer.apply()
.append(platformDdl.createExternalUniqueForOneToOne(uqName, alter.getTableName(), cols))
.append(platformDdl.alterTableAddUniqueConstraint(alter.getTableName(), uqName, cols))
.endOfStatement();
writer.rollbackForeignKeys()
@@ -0,0 +1,44 @@
package com.avaje.ebean.dbmigration.ddlgeneration.platform;
import java.util.ArrayList;
import java.util.List;
/**
* Bean holding comments relating to a history table that needs to have it's
* associated triggers/function updated due to columns added/dropped/included
* or excluded.
*/
public class HistoryTableUpdate {
private final String baseTable;
private final List<String> comments = new ArrayList<String>();
/**
* Construct with a given base table name.
*/
public HistoryTableUpdate(String baseTable) {
this.baseTable = baseTable;
}
/**
* Add a comment for column added, dropped, included or excluded.
*/
public void addComment(String comment) {
comments.add(comment);
}
/**
* Return the base table name.
*/
public String getBaseTable() {
return baseTable;
}
/**
* Return the comments.
*/
public List<String> getComments() {
return comments;
}
}
@@ -31,7 +31,7 @@ public class MsSqlServerDdl extends PlatformDdl {
* MsSqlServer specific null handling on unique constraints.
*/
@Override
public String createExternalUniqueForOneToOne(String uqName, String tableName, String[] columns) {
public String alterTableAddUniqueConstraint(String tableName, String uqName, String[] columns) {
// issues#233
String start = "create unique nonclustered index " + uqName + " on " + tableName+ "(";
@@ -50,7 +50,6 @@ public class MsSqlServerDdl extends PlatformDdl {
return sb.toString();
}
@Override
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
@@ -12,6 +12,7 @@ public class MySqlDdl extends PlatformDdl {
public MySqlDdl(DbTypeMap platformTypes, DbIdentity dbIdentity) {
super(platformTypes, dbIdentity);
this.alterColumn = "modify";
this.dropUniqueConstraint = "drop index";
}
/**
@@ -35,7 +35,7 @@ public class NoHistorySupportDdl implements PlatformHistoryDdl {
}
@Override
public void regenerateHistoryTriggers(DdlWrite write, String baseTable) {
public void regenerateHistoryTriggers(DdlWrite write, HistoryTableUpdate update) {
// does nothing
}
}
@@ -54,7 +54,9 @@ public class PlatformDdl {
protected String dropIndexIfExists = "drop index if exists ";
protected String alterColumn = "alter column";
protected String alterColumn = "alter column";
protected String dropUniqueConstraint = "drop constraint";
protected String columnSetType = "";
@@ -76,11 +78,18 @@ public class PlatformDdl {
this.typeConverter = new PlatformTypeConverter(platformTypes);
}
/**
* Create a DdlHandler for the specific database platform.
*/
public DdlHandler createDdlHandler(ServerConfig serverConfig) {
historyDdl.configure(serverConfig);
return new BaseDdlHandler(serverConfig, this);
}
/**
* Return the identity type to use given the support in the underlying database
* platform for sequences and identity/autoincrement.
*/
public IdType useIdentityType(IdentityType modelIdentityType) {
return dbIdentity.useIdentityType(modelIdentityType);
@@ -122,16 +131,25 @@ public class PlatformDdl {
historyDdl.createWithHistory(writer, table);
}
/**
* Drop history support for a given table.
*/
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException {
historyDdl.dropHistoryTable(writer, dropHistoryTable);
}
/**
* Add history support to an existing table.
*/
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException {
historyDdl.addHistoryTable(writer, addHistoryTable);
}
public void regenerateHistoryTriggers(DdlWrite write, String baseTable) throws IOException {
historyDdl.regenerateHistoryTriggers(write, baseTable);
/**
* Regenerate the history triggers (or function) due to a column being added/dropped/excluded or included.
*/
public void regenerateHistoryTriggers(DdlWrite write, HistoryTableUpdate update) throws IOException {
historyDdl.regenerateHistoryTriggers(write, update);
}
/**
@@ -174,6 +192,31 @@ public class PlatformDdl {
return dropIndexIfExists + indexName;
}
/**
* Drop a unique constraint from the table.
*/
public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) {
return "alter table " + tableName + " " + dropUniqueConstraint + " " + uniqueConstraintName;
}
/**
* Add a unique constraint to the table.
* <p>
* Overridden by MsSqlServer for specific null handling on unique constraints.
*/
public String alterTableAddUniqueConstraint(String tableName, String uqName, String[] columns) {
String base = "alter table " + tableName + " add constraint " + uqName + " unique (";
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
base += ",";
}
base += columns[i];
}
base += ")";
return base;
}
/**
* Return true if unique constraints for OneToOne can be inlined as normal.
* Returns false for MsSqlServer due to it's null handling for unique constraints.
@@ -183,37 +226,54 @@ public class PlatformDdl {
}
/**
* Overridden by MsSqlServer for specific null handling on unique constraints.
* Alter a column type.
* <p>
* Note that that MySql and SQL Server instead use alterColumnBaseAttributes()
* </p>
*/
public String createExternalUniqueForOneToOne(String uqName, String tableName, String[] columns) {
// does nothing by default, really this is a MsSqlServer specific requirement
return "";
}
public String alterColumnType(String tableName, String columnName, String type) {
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + columnSetType + type;
}
/**
* Alter a column adding or removing the not null constraint.
* <p>
* Note that that MySql and SQL Server instead use alterColumnBaseAttributes()
* </p>
*/
public String alterColumnNotnull(String tableName, String columnName, boolean notnull) {
String suffix = notnull ? columnSetNotnull : columnSetNull;
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + suffix;
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + suffix;
}
/**
* Return true if the default value is the special DROP DEFAULT value.
*/
public boolean isDropDefault(String defaultValue) {
return "DROP DEFAULT".equals(defaultValue);
}
/**
* Alter column setting the default value.
*/
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
String suffix = isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + defaultValue;
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + suffix;
}
/**
* Alter column setting both the type and not null constraint.
* <p>
* Used by MySql and SQL Server as these require both column attributes to be set together.
* </p>
*/
public String alterColumnBaseAttributes(AlterColumn alter) {
// by default do nothing, only used by mysql as it can only modify the column with the
// full column definition
// by default do nothing, only used by mysql and sql server as they can only
// modify the column with the full column definition
return null;
}
}
@@ -23,9 +23,18 @@ public interface PlatformHistoryDdl {
*/
void createWithHistory(DdlWrite writer, MTable table) throws IOException;
/**
* Drop history support for the given table.
*/
void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException;
/**
* Add history support to the given table.
*/
void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException;
void regenerateHistoryTriggers(DdlWrite write, String baseTable) throws IOException;
/**
* Regenerate the history triggers/stored function due to column added/dropped/included or excluded.
*/
void regenerateHistoryTriggers(DdlWrite write, HistoryTableUpdate baseTable) throws IOException;
}
@@ -11,6 +11,7 @@ import com.avaje.ebean.dbmigration.model.MTable;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
/**
* Uses DB triggers to maintain a history table.
@@ -37,13 +38,13 @@ public class PostgresHistoryDdl implements PlatformHistoryDdl {
}
@Override
public void regenerateHistoryTriggers(DdlWrite writer, String baseTable) throws IOException {
public void regenerateHistoryTriggers(DdlWrite writer, HistoryTableUpdate update) throws IOException {
MTable table = writer.getTable(baseTable);
MTable table = writer.getTable(update.getBaseTable());
if (table == null) {
throw new IllegalStateException("MTable "+baseTable+" not found in writer? (required for history DDL)");
throw new IllegalStateException("MTable "+update.getBaseTable()+" not found in writer? (required for history DDL)");
}
addStoredFunction(writer, table);
addStoredFunction(writer, table, update.getComments());
}
@Override
@@ -80,7 +81,7 @@ public class PostgresHistoryDdl implements PlatformHistoryDdl {
dropHistoryTableEtc(writer.rollback(), baseTable);
addHistoryTable(writer, table, whenCreatedColumn);
addStoredFunction(writer, table);
addStoredFunction(writer, table, null);
addTrigger(writer, table);
}
@@ -156,11 +157,24 @@ public class PostgresHistoryDdl implements PlatformHistoryDdl {
.append(" for each row execute procedure ").append(procedureName).append("();").newLine().newLine();
}
protected void addStoredFunction(DdlWrite writer, MTable table) throws IOException {
protected void addStoredFunction(DdlWrite writer, MTable table, List<String> comments) throws IOException {
String procedureName = procedureName(table.getName());
DdlBuffer apply = writer.applyHistory();
if (comments != null && !comments.isEmpty()) {
apply.append("-- Regenerated ").append(procedureName).newLine();
apply.append("-- changes: ");
for (int i = 0; i < comments.size(); i++) {
if (i > 0) {
apply.append(", ");
}
apply.append(comments.get(i));
}
apply.newLine();
}
apply
.append("create or replace function ").append(procedureName).append("() returns trigger as $$").newLine()
.append("begin").newLine();
@@ -35,14 +35,29 @@ import java.util.Set;
*/
public class MTable {
/**
* Table name.
*/
private final String name;
/**
* Primary key name.
*/
private String pkName;
/**
* Table comment.
*/
private String comment;
/**
* Tablespace to use.
*/
private String tablespace;
/**
* Tablespace to use for indexes on this table.
*/
private String indexTablespace;
/**
@@ -51,21 +66,44 @@ public class MTable {
*/
private IdentityType identityType;
/**
* DB sequence name.
*/
private String sequenceName;
private int sequenceInitial;
private int sequenceAllocate;
/**
* If set to true this table should has history support.
*/
private boolean withHistory;
/**
* The columns on the table.
*/
private Map<String, MColumn> columns = new LinkedHashMap<String, MColumn>();
/**
* Compound unique constraints.
*/
private List<MCompoundUniqueConstraint> compoundUniqueConstraints = new ArrayList<MCompoundUniqueConstraint>();
/**
* Compound foreign keys.
*/
private List<MCompoundForeignKey> compoundKeys = new ArrayList<MCompoundForeignKey>();
/**
* Column name for the 'When created' column. This can be used for the initial effective start date when adding
* history to an existing table and maps to a @WhenCreated or @CreatedTimestamp column.
*/
private String whenCreatedColumn;
/**
* Temporary - holds addColumn settings.
*/
private AddColumn addColumn;
private String whenCreatedColumn;
/**
* Construct for migration.
@@ -94,6 +132,9 @@ public class MTable {
this.name = name;
}
/**
* Return the CreateTable migration for this table.
*/
public CreateTable createTable() {
CreateTable createTable = new CreateTable();
@@ -121,6 +162,9 @@ public class MTable {
return createTable;
}
/**
* Compare to another version of the same table to perform a diff.
*/
public void compare(ModelDiff modelDiff, MTable newTable) {
if (withHistory != newTable.withHistory) {
@@ -187,7 +231,7 @@ public class MTable {
String columnName = alterColumn.getColumnName();
MColumn existingColumn = columns.get(columnName);
if (existingColumn == null) {
throw new IllegalStateException("Column ["+columnName+"] does not exist for AlterColumn change?");
throw new IllegalStateException("Column [" + columnName + "] does not exist for AlterColumn change?");
}
existingColumn.apply(alterColumn);
}
@@ -347,18 +391,13 @@ public class MTable {
addCompoundUniqueConstraint(cols, oneToOne, constraintName);
}
/**
* Add a compound foreign key.
*/
public void addForeignKey(MCompoundForeignKey compoundKey) {
compoundKeys.add(compoundKey);
}
private int toInt(BigInteger value) {
return (value == null) ? 0 : value.intValue();
}
private BigInteger toBigInteger(int value) {
return (value == 0) ? null : BigInteger.valueOf(value);
}
/**
* Add a column checking if it already exists and if so return the existing column.
* Sometimes the case for a primaryKey that is also a foreign key.
@@ -378,7 +417,9 @@ public class MTable {
return newCol;
}
/**
* Add a 'new column' to the AddColumn migration object.
*/
private void diffNewColumn(MColumn newColumn) {
if (addColumn == null) {
@@ -394,6 +435,9 @@ public class MTable {
addColumn.getColumn().add(newColumn.createColumn());
}
/**
* Add a 'drop column' to the diff.
*/
private void diffDropColumn(ModelDiff modelDiff, MColumn existingColumn) {
DropColumn dropColumn = new DropColumn();
@@ -408,4 +452,12 @@ public class MTable {
modelDiff.addDropColumn(dropColumn);
}
private int toInt(BigInteger value) {
return (value == null) ? 0 : value.intValue();
}
private BigInteger toBigInteger(int value) {
return (value == 0) ? null : BigInteger.valueOf(value);
}
}
@@ -61,6 +61,13 @@ public class ModelBuildBeanVisitor implements BeanVisitor {
return new ModelBuildPropertyVisitor(ctx, table, descriptor.getCompoundUniqueConstraints());
}
/**
* Set the identity type to use for this table.
* <p>
* Takes into account the requested identity type and the underlying support in the
* database platform.
* </p>
*/
private void setIdentity(BeanDescriptor<?> descriptor, MTable table) {
if (IdType.GENERATOR == descriptor.getIdType()) {