#699 - DB Migration not generating sql to alter check constraints

This commit is contained in:
Robin Bygrave
2016-05-12 13:44:30 +12:00
parent 2a323f2256
commit b384cb4043
8 changed files with 81 additions and 7 deletions
@@ -635,6 +635,13 @@ public class BaseTableDdl implements TableDdl {
alterColumnAddUniqueOneToOneConstraint(writer, alterColumn);
}
boolean alterCheckConstraint = hasValue(alterColumn.getCheckConstraint());
if (alterCheckConstraint) {
// drop constraint before altering type etc
dropCheckConstraint(writer, alterColumn);
}
boolean alterBaseAttributes = false;
if (hasValue(alterColumn.getType())) {
alterColumnType(writer, alterColumn);
@@ -648,10 +655,13 @@ public class BaseTableDdl implements TableDdl {
alterColumnNotnull(writer, alterColumn);
alterBaseAttributes = true;
}
if (alterBaseAttributes) {
alterColumnBaseAttributes(writer, alterColumn);
}
if (alterCheckConstraint) {
// add constraint last (after potential type change)
addCheckConstraint(writer, alterColumn);
}
}
/**
@@ -705,6 +715,22 @@ public class BaseTableDdl implements TableDdl {
}
}
protected void dropCheckConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
String ddl = platformDdl.alterTableDropConstraint(alter.getTableName(), alter.getCheckConstraintName());
if (hasValue(ddl)) {
writer.apply().append(ddl).endOfStatement();
}
}
protected void addCheckConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
String ddl = platformDdl.alterTableAddCheckConstraint(alter.getTableName(), alter.getCheckConstraintName(), alter.getCheckConstraint());
if (hasValue(ddl)) {
writer.apply().append(ddl).endOfStatement();
}
}
protected void alterColumnNotnull(DdlWrite writer, AlterColumn alter) throws IOException {
String ddl = platformDdl.alterColumnNotnull(alter.getTableName(), alter.getColumnName(), alter.isNotnull());
@@ -38,8 +38,13 @@ public class MySqlDdl extends PlatformDdl {
}
@Override
public String alterColumnType(String tableName, String columnName, String type) {
public String alterTableDropConstraint(String tableName, String constraintName) {
// drop constraint not supported
return null;
}
@Override
public String alterColumnType(String tableName, String columnName, String type) {
// can't alter itself - done in alterColumnBaseAttributes()
return null;
}
@@ -67,8 +67,12 @@ public class PlatformDdl {
protected String alterColumn = "alter column";
protected String dropConstraint = "drop constraint";
protected String dropUniqueConstraint = "drop constraint";
protected String addConstraint = "add constraint";
protected String columnSetType = "";
protected String columnSetDefault = "set default";
@@ -327,12 +331,19 @@ public class PlatformDdl {
}
/**
* Drop a unique constraint from the table.
* Drop a unique constraint from the table (Sometimes this is an index).
*/
public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) {
return "alter table " + tableName + " " + dropUniqueConstraint + " " + uniqueConstraintName;
}
/**
* Drop a unique constraint from the table.
*/
public String alterTableDropConstraint(String tableName, String constraintName) {
return "alter table " + tableName + " " + dropConstraint + " " + constraintName;
}
/**
* Add a unique constraint to the table.
* <p>
@@ -377,6 +388,14 @@ public class PlatformDdl {
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + suffix;
}
/**
* Alter table adding the check constraint.
*/
public String alterTableAddCheckConstraint(String tableName, String checkConstraintName, String checkConstraint) {
return "alter table " + tableName + " " + addConstraint + " " + checkConstraintName + " " + checkConstraint;
}
/**
* Return true if the default value is the special DROP DEFAULT value.
*/
@@ -317,7 +317,7 @@ public class MColumn {
if (different(checkConstraint, newColumn.checkConstraint)) {
AlterColumn alter = getAlterColumn(tableName, tableWithHistory);
if (hasValue(checkConstraint)) {
if (hasValue(checkConstraint) && !hasValue(newColumn.checkConstraint)) {
alter.setDropCheckConstraint(checkConstraintName);
}
if (hasValue(newColumn.checkConstraint)) {
@@ -49,7 +49,6 @@ class MigrationMetaRow {
version = row.getString("mversion");
comment = row.getString("mcomment");
checksum = row.getInteger("mchecksum");
runOn = row.getTimestamp("run_on");
runBy = row.getString("run_by");
runTime = row.getLong("run_time");
}
@@ -142,7 +142,11 @@ public class MigrationTable {
* Return true if the table exists.
*/
private boolean tableExists(Connection connection) throws SQLException {
return databasePlatform.tableExists(connection, catalog, schema, table);
boolean exists = databasePlatform.tableExists(connection, catalog, schema, table);
if (!exists) {
exists = databasePlatform.tableExists(connection, catalog, schema, table.toUpperCase());
}
return exists;
}
/**