From b9774e64e9172180d1ce286297c8f6ce9a5a1c24 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Mon, 17 Aug 2015 07:44:59 +1200 Subject: [PATCH] #374 - Add comments for when a history table trigger/function is regenerated --- .../ddlgeneration/platform/BaseTableDdl.java | 72 +++++++++++----- .../platform/HistoryTableUpdate.java | 44 ++++++++++ .../platform/MsSqlServerDdl.java | 3 +- .../ddlgeneration/platform/MySqlDdl.java | 1 + .../platform/NoHistorySupportDdl.java | 2 +- .../ddlgeneration/platform/PlatformDdl.java | 84 ++++++++++++++++--- .../platform/PlatformHistoryDdl.java | 11 ++- .../platform/PostgresHistoryDdl.java | 26 ++++-- .../avaje/ebean/dbmigration/model/MTable.java | 74 +++++++++++++--- .../model/build/ModelBuildBeanVisitor.java | 7 ++ src/main/resources/ebean-dbmigration-1.0.xsd | 2 +- .../PlatformDdl_dropUniqueConstraintTest.java | 38 +++++++++ 12 files changed, 309 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/HistoryTableUpdate.java create mode 100644 src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java 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 90ccac266..010733734 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 @@ -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 regenerateHistoryTriggers = new LinkedHashSet(); + protected Map regenerateHistoryTriggers = new LinkedHashMap(); /** * 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 columns) { + protected String[] toColumnNames(List 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() diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/HistoryTableUpdate.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/HistoryTableUpdate.java new file mode 100644 index 000000000..c393b6107 --- /dev/null +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/HistoryTableUpdate.java @@ -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 comments = new ArrayList(); + + /** + * 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 getComments() { + return comments; + } +} diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MsSqlServerDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MsSqlServerDdl.java index 8c625a12b..8416745c0 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MsSqlServerDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MsSqlServerDdl.java @@ -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) { diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MySqlDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MySqlDdl.java index 754770719..38d56b706 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MySqlDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/MySqlDdl.java @@ -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"; } /** diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/NoHistorySupportDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/NoHistorySupportDdl.java index 014cf1885..ef697a779 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/NoHistorySupportDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/NoHistorySupportDdl.java @@ -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 } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java index 9091bd54d..d76784044 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java @@ -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. + *

+ * 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. + *

+ * Note that that MySql and SQL Server instead use alterColumnBaseAttributes() + *

*/ - 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. + *

+ * Note that that MySql and SQL Server instead use alterColumnBaseAttributes() + *

+ */ 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. + *

+ * Used by MySql and SQL Server as these require both column attributes to be set together. + *

+ */ 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; } + } diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformHistoryDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformHistoryDdl.java index d1169a6f4..779771765 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformHistoryDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformHistoryDdl.java @@ -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; } 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 5374e247f..15080ef41 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 @@ -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 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(); 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 e37962aa4..8d576280a 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/MTable.java @@ -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 columns = new LinkedHashMap(); + /** + * Compound unique constraints. + */ private List compoundUniqueConstraints = new ArrayList(); + /** + * Compound foreign keys. + */ private List compoundKeys = new ArrayList(); + /** + * 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); + } + } 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 5c23dee93..bf37aeae3 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 @@ -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. + *

+ * Takes into account the requested identity type and the underlying support in the + * database platform. + *

+ */ private void setIdentity(BeanDescriptor descriptor, MTable table) { if (IdType.GENERATOR == descriptor.getIdType()) { diff --git a/src/main/resources/ebean-dbmigration-1.0.xsd b/src/main/resources/ebean-dbmigration-1.0.xsd index 488fbd9ea..61328889e 100644 --- a/src/main/resources/ebean-dbmigration-1.0.xsd +++ b/src/main/resources/ebean-dbmigration-1.0.xsd @@ -355,7 +355,7 @@ - + diff --git a/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java b/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java new file mode 100644 index 000000000..df2f5ccf8 --- /dev/null +++ b/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java @@ -0,0 +1,38 @@ +package com.avaje.ebean.dbmigration.ddlgeneration.platform; + +import com.avaje.ebean.config.dbplatform.H2Platform; +import com.avaje.ebean.config.dbplatform.MsSqlServer2005Platform; +import com.avaje.ebean.config.dbplatform.MySqlPlatform; +import com.avaje.ebean.config.dbplatform.Oracle10Platform; +import com.avaje.ebean.config.dbplatform.PostgresPlatform; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PlatformDdl_dropUniqueConstraintTest { + + + PlatformDdl h2Ddl = new H2Platform().getPlatformDdl(); + PlatformDdl pgDdl = new PostgresPlatform().getPlatformDdl(); + PlatformDdl mysqlDdl = new MySqlPlatform().getPlatformDdl(); + PlatformDdl oraDdl = new Oracle10Platform().getPlatformDdl(); + PlatformDdl sqlServerDdl = new MsSqlServer2005Platform().getPlatformDdl(); + + @Test + public void test() throws Exception { + + String sql = h2Ddl.alterTableDropUniqueConstraint("mytab", "uq_name"); + assertEquals("alter table mytab drop constraint uq_name", sql); + sql = pgDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); + assertEquals("alter table mytab drop constraint uq_name", sql); + sql = oraDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); + assertEquals("alter table mytab drop constraint uq_name", sql); + sql = sqlServerDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); + assertEquals("alter table mytab drop constraint uq_name", sql); + + + sql = mysqlDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); + assertEquals("alter table mytab drop index uq_name", sql); + } + +} \ No newline at end of file