mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Prepared PlatformDdl to to have only one 'alterColumn' method (required for further refactoring)
This commit is contained in:
1 parent
4285cbaeb2
commit
1529a2f12b
14 files changed
+594
-380
No files matched your search
+6
-20
@@ -5,6 +5,7 @@ import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.config.dbplatform.DbPlatformType;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -38,7 +39,7 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnBaseAttributes(AlterColumn alter) {
|
||||
public void alterColumn(DdlWrite writer, AlterColumn alter) {
|
||||
String tableName = alter.getTableName();
|
||||
String columnName = alter.getColumnName();
|
||||
String currentType = alter.getCurrentType();
|
||||
@@ -51,7 +52,7 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
: (alter.getDefaultValue() != null ? alter.getDefaultValue() : alter.getCurrentDefaultValue());
|
||||
String defaultValueClause = (defaultValue == null || defaultValue.isEmpty()) ? "" : " default " + defaultValue;
|
||||
|
||||
DdlBuffer buffer = new BaseDdlBuffer();
|
||||
DdlBuffer buffer = writer.apply();
|
||||
if (!isConvertible(currentType, type)) {
|
||||
// add an intermediate conversion if possible
|
||||
if (isNumberType(currentType)) {
|
||||
@@ -73,17 +74,7 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName)
|
||||
.append(" ").append(type).append(defaultValueClause).append(notnullClause).append(alterColumnSuffix);
|
||||
|
||||
return buffer.getBuffer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnNotnull(String tableName, String columnName, boolean notnull) {
|
||||
return null;
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,11 +82,6 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
return new HanaDdlHandler(config, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnType(String tableName, String columnName, String type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String convertArrayType(String logicalArrayType) {
|
||||
Matcher matcher = ARRAY_PATTERN.matcher(logicalArrayType);
|
||||
@@ -140,8 +126,8 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
* foreign keys. That's why we call a user stored procedure here
|
||||
*/
|
||||
@Override
|
||||
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) {
|
||||
buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')")
|
||||
public void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
writer.apply().append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')")
|
||||
.endOfStatement();
|
||||
}
|
||||
|
||||
|
||||
+27
-62
@@ -646,14 +646,14 @@ public class BaseTableDdl implements TableDdl {
|
||||
String tableName = addColumn.getTableName();
|
||||
List<Column> columns = addColumn.getColumn();
|
||||
for (Column column : columns) {
|
||||
alterTableAddColumn(writer.apply(), tableName, column, false, isTrue(addColumn.isWithHistory()));
|
||||
alterTableAddColumn(writer, tableName, column, false, isTrue(addColumn.isWithHistory()));
|
||||
}
|
||||
if (isTrue(addColumn.isWithHistory()) && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
// make same changes to the history table
|
||||
String historyTable = historyTable(tableName);
|
||||
for (Column column : columns) {
|
||||
regenerateHistoryTriggers(tableName, HistoryTableUpdate.Change.ADD, column.getName());
|
||||
alterTableAddColumn(writer.apply(), historyTable, column, true, true);
|
||||
alterTableAddColumn(writer, historyTable, column, true, true);
|
||||
}
|
||||
}
|
||||
for (Column column : columns) {
|
||||
@@ -686,12 +686,12 @@ public class BaseTableDdl implements TableDdl {
|
||||
@Override
|
||||
public void generate(DdlWrite writer, DropColumn dropColumn) {
|
||||
String tableName = dropColumn.getTableName();
|
||||
alterTableDropColumn(writer.apply(), tableName, dropColumn.getColumnName());
|
||||
alterTableDropColumn(writer, tableName, dropColumn.getColumnName());
|
||||
|
||||
if (isTrue(dropColumn.isWithHistory()) && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
// also drop from the history table
|
||||
regenerateHistoryTriggers(tableName, HistoryTableUpdate.Change.DROP, dropColumn.getColumnName());
|
||||
alterTableDropColumn(writer.apply(), historyTable(tableName), dropColumn.getColumnName());
|
||||
alterTableDropColumn(writer, historyTable(tableName), dropColumn.getColumnName());
|
||||
}
|
||||
writer.apply().end();
|
||||
}
|
||||
@@ -739,22 +739,13 @@ public class BaseTableDdl implements TableDdl {
|
||||
// drop constraint before altering type etc
|
||||
dropCheckConstraint(writer, alterColumn, alterColumn.getCheckConstraintName());
|
||||
}
|
||||
boolean alterBaseAttributes = false;
|
||||
if (hasValue(alterColumn.getType())) {
|
||||
alterColumnType(writer, alterColumn);
|
||||
alterBaseAttributes = true;
|
||||
}
|
||||
if (hasValue(alterColumn.getDefaultValue())) {
|
||||
alterColumnDefaultValue(writer, alterColumn);
|
||||
alterBaseAttributes = true;
|
||||
}
|
||||
if (alterColumn.isNotnull() != null) {
|
||||
alterColumnNotnull(writer, alterColumn);
|
||||
alterBaseAttributes = true;
|
||||
}
|
||||
if (alterBaseAttributes) {
|
||||
alterColumnBaseAttributes(writer, alterColumn);
|
||||
|
||||
if (hasValue(alterColumn.getType())
|
||||
|| hasValue(alterColumn.getDefaultValue())
|
||||
|| alterColumn.isNotnull() != null) {
|
||||
alterColumn(writer, alterColumn);
|
||||
}
|
||||
|
||||
if (alterCheckConstraint) {
|
||||
// add constraint last (after potential type change)
|
||||
addCheckConstraint(writer, alterColumn);
|
||||
@@ -785,28 +776,19 @@ public class BaseTableDdl implements TableDdl {
|
||||
* This is mysql specific - alter all the base attributes of the column together.
|
||||
* Will be called, if there is a type, dbdefault or notnull change.
|
||||
*/
|
||||
protected void alterColumnBaseAttributes(DdlWrite writer, AlterColumn alter) {
|
||||
String ddl = platformDdl.alterColumnBaseAttributes(alter);
|
||||
if (hasValue(ddl)) {
|
||||
writer.apply().appendStatement(ddl);
|
||||
protected void alterColumn(DdlWrite writer, AlterColumn alter) {
|
||||
platformDdl.alterColumn(writer, alter);
|
||||
|
||||
if (isTrue(alter.isWithHistory()) && alter.getType() != null && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
// mysql and sql server column type change allowing nulls in the history table column
|
||||
regenerateHistoryTriggers(alter.getTableName(), HistoryTableUpdate.Change.ALTER, alter.getColumnName());
|
||||
AlterColumn alterHistoryColumn = new AlterColumn();
|
||||
alterHistoryColumn.setTableName(historyTable(alter.getTableName()));
|
||||
alterHistoryColumn.setColumnName(alter.getColumnName());
|
||||
alterHistoryColumn.setType(alter.getType());
|
||||
String histColumnDdl = platformDdl.alterColumnBaseAttributes(alterHistoryColumn);
|
||||
|
||||
// write the apply to history table
|
||||
writer.apply().appendStatement(histColumnDdl);
|
||||
}
|
||||
if (isTrue(alter.isWithHistory()) && alter.getType() != null && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
// mysql and sql server column type change allowing nulls in the history table column
|
||||
regenerateHistoryTriggers(alter.getTableName(), HistoryTableUpdate.Change.ALTER, alter.getColumnName());
|
||||
AlterColumn alterHistoryColumn = new AlterColumn();
|
||||
alterHistoryColumn.setTableName(historyTable(alter.getTableName()));
|
||||
alterHistoryColumn.setColumnName(alter.getColumnName());
|
||||
alterHistoryColumn.setType(alter.getType());
|
||||
platformDdl.alterColumn(writer, alterHistoryColumn);
|
||||
}
|
||||
}
|
||||
|
||||
protected void alterColumnDefaultValue(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().appendStatement(platformDdl.alterColumnDefaultValue(alter.getTableName(), alter.getColumnName(), alter.getDefaultValue()));
|
||||
}
|
||||
|
||||
protected void dropCheckConstraint(DdlWrite writer, AlterColumn alter, String constraintName) {
|
||||
@@ -817,23 +799,6 @@ public class BaseTableDdl implements TableDdl {
|
||||
writer.apply().appendStatement(platformDdl.alterTableAddCheckConstraint(alter.getTableName(), alter.getCheckConstraintName(), alter.getCheckConstraint()));
|
||||
}
|
||||
|
||||
protected void alterColumnNotnull(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().appendStatement(platformDdl.alterColumnNotnull(alter.getTableName(), alter.getColumnName(), alter.isNotnull()));
|
||||
}
|
||||
|
||||
protected void alterColumnType(DdlWrite writer, AlterColumn alter) {
|
||||
String ddl = platformDdl.alterColumnType(alter.getTableName(), alter.getColumnName(), alter.getType());
|
||||
if (hasValue(ddl)) {
|
||||
writer.apply().appendStatement(ddl);
|
||||
if (isTrue(alter.isWithHistory()) && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
regenerateHistoryTriggers(alter.getTableName(), HistoryTableUpdate.Change.ALTER, alter.getColumnName());
|
||||
// apply same type change to matching column in the history table
|
||||
ddl = platformDdl.alterColumnType(historyTable(alter.getTableName()), alter.getColumnName(), alter.getType());
|
||||
writer.apply().appendStatement(ddl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void alterColumnAddForeignKey(DdlWrite writer, AlterColumn alterColumn) {
|
||||
alterTableAddForeignKey(writer.getOptions(), writer.apply(), new WriteForeignKey(alterColumn));
|
||||
}
|
||||
@@ -863,24 +828,24 @@ public class BaseTableDdl implements TableDdl {
|
||||
}
|
||||
|
||||
|
||||
protected void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) {
|
||||
platformDdl.alterTableDropColumn(buffer, tableName, columnName);
|
||||
protected void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
platformDdl.alterTableDropColumn(writer, tableName, columnName);
|
||||
}
|
||||
|
||||
protected void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column, boolean onHistoryTable, boolean withHistory) {
|
||||
protected void alterTableAddColumn(DdlWrite writer, String tableName, Column column, boolean onHistoryTable, boolean withHistory) {
|
||||
DdlMigrationHelp help = new DdlMigrationHelp(tableName, column, withHistory);
|
||||
if (!onHistoryTable) {
|
||||
help.writeBefore(buffer);
|
||||
help.writeBefore(writer.apply());
|
||||
}
|
||||
|
||||
platformDdl.alterTableAddColumn(buffer, tableName, column, onHistoryTable, help.getDefaultValue());
|
||||
platformDdl.alterTableAddColumn(writer, tableName, column, onHistoryTable, help.getDefaultValue());
|
||||
final String comment = column.getComment();
|
||||
if (comment != null && !comment.isEmpty()) {
|
||||
platformDdl.addColumnComment(buffer, tableName, column.getName(), comment);
|
||||
platformDdl.addColumnComment(writer.apply(), tableName, column.getName(), comment);
|
||||
}
|
||||
|
||||
if (!onHistoryTable) {
|
||||
help.writeAfter(buffer);
|
||||
help.writeAfter(writer.apply());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-2
@@ -5,6 +5,7 @@ import io.ebean.config.DbConstraintNaming;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.model.MColumn;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
@@ -241,8 +242,10 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) {
|
||||
platformDdl.alterTableDropColumn(buffer, baseTableName, sysPeriodStart);
|
||||
platformDdl.alterTableDropColumn(buffer, baseTableName, sysPeriodEnd);
|
||||
DdlWrite writer = new DdlWrite();
|
||||
platformDdl.alterTableDropColumn(writer, baseTableName, sysPeriodStart);
|
||||
platformDdl.alterTableDropColumn(writer, baseTableName, sysPeriodEnd);
|
||||
buffer.append(writer.apply().getBuffer()); // Workaround - will be removed later
|
||||
}
|
||||
|
||||
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) {
|
||||
|
||||
+3
-10
@@ -27,11 +27,6 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void alterColumnDefaultValue(DdlWrite writer, AlterColumn alter) {
|
||||
// done in alterColumnBaseAttributes
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(DdlWrite writer, AddColumn addColumn) {
|
||||
String tableName = addColumn.getTableName();
|
||||
@@ -54,7 +49,7 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
String historyTable = historyTable(tableName);
|
||||
List<Column> columns = addColumn.getColumn();
|
||||
for (Column column : columns) {
|
||||
alterTableAddColumn(writer.apply(), historyTable, column, true, true);
|
||||
alterTableAddColumn(writer, historyTable, column, true, true);
|
||||
}
|
||||
|
||||
historyDdl.enableSystemVersioning(writer.apply(), table.getName(), historyTable, false, this.generateUniqueDdl);
|
||||
@@ -91,10 +86,8 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
alterHistoryColumn.setCurrentType(alterColumn.getCurrentType());
|
||||
alterHistoryColumn.setCurrentDefaultValue(alterColumn.getCurrentDefaultValue());
|
||||
alterHistoryColumn.setCurrentNotnull(alterColumn.isCurrentNotnull());
|
||||
String histColumnDdl = platformDdl.alterColumnBaseAttributes(alterHistoryColumn);
|
||||
platformDdl.alterColumn(writer, alterHistoryColumn);
|
||||
|
||||
// write the apply to history table
|
||||
writer.apply().append(histColumnDdl).endOfStatement();
|
||||
}
|
||||
|
||||
historyDdl.enableSystemVersioning(writer.apply(), tableName, historyTable, false, this.generateUniqueDdl);
|
||||
@@ -121,7 +114,7 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
if (manageSystemVersioning) {
|
||||
// also drop from the history table
|
||||
String historyTable = historyTable(tableName);
|
||||
alterTableDropColumn(writer.apply(), historyTable, dropColumn.getColumnName());
|
||||
alterTableDropColumn(writer, historyTable, dropColumn.getColumnName());
|
||||
|
||||
historyDdl.enableSystemVersioning(writer.apply(), tableName, historyTable, false, this.generateUniqueDdl);
|
||||
}
|
||||
|
||||
+29
-33
@@ -3,6 +3,7 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.util.StringHelper;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
|
||||
@@ -21,7 +22,7 @@ public class MySqlDdl extends PlatformDdl {
|
||||
|
||||
public MySqlDdl(DatabasePlatform platform) {
|
||||
super(platform);
|
||||
this.alterColumn = "modify";
|
||||
this.alterColumn = "alter";
|
||||
this.dropUniqueConstraint = "drop index";
|
||||
this.historyDdl = new MySqlHistoryDdl();
|
||||
this.inlineComments = true;
|
||||
@@ -37,11 +38,11 @@ public class MySqlDdl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void alterTableDropColumn(final DdlBuffer buffer, final String tableName, final String columnName) {
|
||||
public void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
if (this.useMigrationStoredProcedures) {
|
||||
buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement();
|
||||
writer.apply().append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement();
|
||||
} else {
|
||||
super.alterTableDropColumn(buffer, tableName, columnName);
|
||||
super.alterTableDropColumn(writer, tableName, columnName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,38 +92,33 @@ public class MySqlDdl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnType(String tableName, String columnName, String type) {
|
||||
// can't alter itself - done in alterColumnBaseAttributes()
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnNotnull(String tableName, String columnName, boolean notnull) {
|
||||
// can't alter itself - done in alterColumnBaseAttributes()
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
|
||||
String suffix = DdlHelp.isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + convertDefaultValue(defaultValue);
|
||||
return "alter table " + tableName + " alter " + columnName + " " + suffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnBaseAttributes(AlterColumn alter) {
|
||||
if (alter.getType() == null && alter.isNotnull() == null) {
|
||||
// No type change or notNull change
|
||||
// defaultValue change already handled in alterColumnDefaultValue
|
||||
return null;
|
||||
}
|
||||
public void alterColumn(DdlWrite writer, AlterColumn alter) {
|
||||
String tableName = alter.getTableName();
|
||||
String columnName = alter.getColumnName();
|
||||
String type = alter.getType() != null ? alter.getType() : alter.getCurrentType();
|
||||
type = convert(type);
|
||||
boolean notnull = (alter.isNotnull() != null) ? alter.isNotnull() : Boolean.TRUE.equals(alter.isCurrentNotnull());
|
||||
String notnullClause = notnull ? " not null" : "";
|
||||
|
||||
return "alter table " + tableName + " modify " + columnName + " " + type + notnullClause;
|
||||
if (alter.getType() == null && alter.isNotnull() == null) {
|
||||
// No type change or notNull change -> handle default value change
|
||||
if (hasValue(alter.getDefaultValue())) {
|
||||
alterColumnDefault(writer, alter);
|
||||
}
|
||||
} else {
|
||||
// we must regenerate whole statement -> read altered and current value
|
||||
String type = alter.getType() != null ? alter.getType() : alter.getCurrentType();
|
||||
type = convert(type);
|
||||
boolean notnull = (alter.isNotnull() != null) ? alter.isNotnull() : Boolean.TRUE.equals(alter.isCurrentNotnull());
|
||||
String defaultValue = alter.getDefaultValue() != null ? alter.getDefaultValue() : alter.getCurrentDefaultValue();
|
||||
|
||||
DdlBuffer buffer = writer.apply().append("alter table ").append(tableName)
|
||||
.appendWithSpace("modify").appendWithSpace(columnName);
|
||||
buffer.appendWithSpace(type);
|
||||
if (notnull) {
|
||||
buffer.append(" not null");
|
||||
}
|
||||
if (hasValue(defaultValue) && !DdlHelp.isDropDefault(defaultValue)) {
|
||||
buffer.append(" default ").append(convertDefaultValue(defaultValue));
|
||||
}
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+50
-20
@@ -538,10 +538,10 @@ public class PlatformDdl {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column, boolean onHistoryTable, String defaultValue) {
|
||||
public void alterTableAddColumn(DdlWrite writer, String tableName, Column column, boolean onHistoryTable, String defaultValue) {
|
||||
|
||||
String convertedType = convert(column.getType());
|
||||
|
||||
DdlBuffer buffer = writer.apply();
|
||||
buffer.append("alter table ").append(tableName)
|
||||
.append(" ").append(addColumn).append(" ").append(column.getName())
|
||||
.append(" ").append(convertedType);
|
||||
@@ -575,8 +575,9 @@ public class PlatformDdl {
|
||||
|
||||
}
|
||||
|
||||
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) {
|
||||
buffer.append("alter table ").append(tableName).append(" ").append(dropColumn).append(" ").append(columnName)
|
||||
public void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
writer.apply().append("alter table ").append(tableName).append(" ")
|
||||
.append(dropColumn).append(" ").append(columnName)
|
||||
.append(dropColumnSuffix).endOfStatement();
|
||||
}
|
||||
|
||||
@@ -592,22 +593,32 @@ public class PlatformDdl {
|
||||
/**
|
||||
* Alter a column type.
|
||||
* <p>
|
||||
* Note that that MySql and SQL Server instead use alterColumnBaseAttributes()
|
||||
* Note that that MySql, SQL Server, and HANA instead use alterColumn()
|
||||
* </p>
|
||||
*/
|
||||
public String alterColumnType(String tableName, String columnName, String type) {
|
||||
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + columnSetType + convert(type) + alterColumnSuffix;
|
||||
protected void alterColumnType(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().append("alter table ").append(alter.getTableName())
|
||||
.appendWithSpace(alterColumn).appendWithSpace(alter.getColumnName())
|
||||
.append(" ").append(columnSetType).append(convert(alter.getType()))
|
||||
.endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter a column adding or removing the not null constraint.
|
||||
* <p>
|
||||
* Note that that MySql, SQL Server, and HANA instead use alterColumnBaseAttributes()
|
||||
* Note that that MySql, SQL Server, and HANA instead use alterColumn()
|
||||
* </p>
|
||||
*/
|
||||
public String alterColumnNotnull(String tableName, String columnName, boolean notnull) {
|
||||
String suffix = notnull ? columnSetNotnull : columnSetNull;
|
||||
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + suffix + alterColumnSuffix;
|
||||
protected void alterColumnNotnull(DdlWrite writer, AlterColumn alter) {
|
||||
|
||||
DdlBuffer buffer = writer.apply().append("alter table ").append(alter.getTableName())
|
||||
.appendWithSpace(alterColumn).appendWithSpace(alter.getColumnName());
|
||||
if (Boolean.TRUE.equals(alter.isNotnull())) {
|
||||
buffer.appendWithSpace(columnSetNotnull);
|
||||
} else {
|
||||
buffer.appendWithSpace(columnSetNull);
|
||||
}
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -619,22 +630,41 @@ public class PlatformDdl {
|
||||
|
||||
/**
|
||||
* Alter column setting the default value.
|
||||
* <p>
|
||||
* Note that that MySql, SQL Server, and HANA instead use alterColumn()
|
||||
* </p>
|
||||
*/
|
||||
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
|
||||
String suffix = DdlHelp.isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + convertDefaultValue(defaultValue);
|
||||
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + suffix + alterColumnSuffix;
|
||||
protected void alterColumnDefault(DdlWrite writer, AlterColumn alter) {
|
||||
DdlBuffer buffer = writer.apply().append("alter table ").append(alter.getTableName())
|
||||
.appendWithSpace(alterColumn).appendWithSpace(alter.getColumnName());
|
||||
if (DdlHelp.isDropDefault(alter.getDefaultValue())) {
|
||||
buffer.appendWithSpace(columnDropDefault);
|
||||
} else {
|
||||
buffer.appendWithSpace(columnSetDefault).appendWithSpace(convertDefaultValue(alter.getDefaultValue()));
|
||||
}
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter column setting both the type and not null constraint.
|
||||
* Alter column setting (type, default value and not null constraint).
|
||||
* <p>
|
||||
* Used by MySql, SQL Server, and HANA as these require both column attributes to be set together.
|
||||
* Used by MySql, SQL Server, and HANA as these require all column attributes to
|
||||
* be set together.
|
||||
* </p>
|
||||
*/
|
||||
public String alterColumnBaseAttributes(AlterColumn alter) {
|
||||
// by default do nothing, only used by mysql, sql server, and HANA as they can only
|
||||
// modify the column with the full column definition
|
||||
return null;
|
||||
public void alterColumn(DdlWrite writer, AlterColumn alter) {
|
||||
|
||||
if (hasValue(alter.getType())) {
|
||||
alterColumnType(writer, alter);
|
||||
}
|
||||
|
||||
if (hasValue(alter.getDefaultValue())) {
|
||||
alterColumnDefault(writer, alter);
|
||||
}
|
||||
|
||||
if (alter.isNotnull() != null) {
|
||||
alterColumnNotnull(writer, alter);
|
||||
}
|
||||
}
|
||||
|
||||
protected void appendColumns(String[] columns, StringBuilder buffer) {
|
||||
|
||||
+10
-2
@@ -2,6 +2,8 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
|
||||
/**
|
||||
* Postgres specific DDL.
|
||||
@@ -56,7 +58,13 @@ public class PostgresDdl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnType(String tableName, String columnName, String type) {
|
||||
return super.alterColumnType(tableName, columnName, type) + " using " + columnName + "::" + convert(type);
|
||||
protected void alterColumnType(DdlWrite writer, AlterColumn alter) {
|
||||
String type = convert(alter.getType());
|
||||
|
||||
writer.apply().append("alter table ").append(alter.getTableName())
|
||||
.appendWithSpace(alterColumn).appendWithSpace(alter.getColumnName())
|
||||
.appendWithSpace(columnSetType).append(type)
|
||||
.append(" using ").append(alter.getColumnName()).append("::").append(type)
|
||||
.endOfStatement();
|
||||
}
|
||||
}
|
||||
+11
-6
@@ -3,6 +3,8 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
|
||||
/**
|
||||
* SQLite platform specific DDL.
|
||||
@@ -59,18 +61,21 @@ public class SQLiteDdl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
|
||||
return "-- not supported: " + super.alterColumnDefaultValue(tableName, columnName, defaultValue);
|
||||
public void alterColumnType(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().append("-- not supported: ");
|
||||
super.alterColumnType(writer, alter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnType(String tableName, String columnName, String type) {
|
||||
return "-- not supported: " + super.alterColumnType(tableName, columnName, type);
|
||||
public void alterColumnDefault(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().append("-- not supported: ");
|
||||
super.alterColumnDefault(writer, alter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnNotnull(String tableName, String columnName, boolean notnull) {
|
||||
return "-- not supported: " + super.alterColumnNotnull(tableName, columnName, notnull);
|
||||
public void alterColumnNotnull(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().append("-- not supported: ");
|
||||
super.alterColumnNotnull(writer, alter);
|
||||
}
|
||||
|
||||
}
|
||||
+73
-53
@@ -11,6 +11,12 @@ import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
*/
|
||||
public class SqlServerDdl extends PlatformDdl {
|
||||
|
||||
private static final String CONSTRAINT = "C";
|
||||
private static final String UNIQUE_CONSTRAINT = "UQ";
|
||||
private static final String USER_TABLE = "U";
|
||||
private static final String FOREIGN_KEY = "F";
|
||||
private static final String SEQUENCE_OBJECT = "SO";
|
||||
|
||||
public SqlServerDdl(DatabasePlatform platform) {
|
||||
super(platform);
|
||||
this.identitySuffix = " identity(1,1)";
|
||||
@@ -31,12 +37,7 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
|
||||
@Override
|
||||
public String dropTable(String tableName) {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append("IF OBJECT_ID('");
|
||||
buffer.append(tableName);
|
||||
buffer.append("', 'U') IS NOT NULL drop table ");
|
||||
buffer.append(tableName);
|
||||
return buffer.toString();
|
||||
return ifObjectExists(tableName, USER_TABLE) + "drop table " + tableName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,12 +47,12 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
if (pos != -1) {
|
||||
objectId = tableName.substring(0, pos + 1) + fkName;
|
||||
}
|
||||
return "IF OBJECT_ID('" + objectId + "', 'F') IS NOT NULL " + super.alterTableDropForeignKey(tableName, fkName);
|
||||
return ifObjectExists(objectId, FOREIGN_KEY) + super.alterTableDropForeignKey(tableName, fkName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String dropSequence(String sequenceName) {
|
||||
return "IF OBJECT_ID('" + sequenceName + "', 'SO') IS NOT NULL drop sequence " + sequenceName;
|
||||
return ifObjectExists(sequenceName, SEQUENCE_OBJECT) + "drop sequence " + sequenceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,12 +72,11 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
// issues#233
|
||||
String start = "create unique nonclustered index " + uqName + " on " + tableName + "(";
|
||||
StringBuilder sb = new StringBuilder(start);
|
||||
|
||||
StringBuilder sb = new StringBuilder(256);
|
||||
sb.append("create unique nonclustered index ").append(uqName).append(" on ").append(tableName).append('(');
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
if (i > 0) {
|
||||
sb.append(",");
|
||||
sb.append(',');
|
||||
}
|
||||
sb.append(columns[i]);
|
||||
}
|
||||
@@ -91,11 +91,9 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
|
||||
@Override
|
||||
public String alterTableDropConstraint(String tableName, String constraintName) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("IF (OBJECT_ID('").append(constraintName).append("', 'C') IS NOT NULL) ");
|
||||
sb.append(super.alterTableDropConstraint(tableName, constraintName));
|
||||
return sb.toString();
|
||||
return ifObjectExists(maxConstraintName(constraintName), CONSTRAINT) + super.alterTableDropConstraint(tableName, constraintName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop a unique constraint from the table (Sometimes this is an index).
|
||||
*/
|
||||
@@ -103,10 +101,12 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(dropIndex(uniqueConstraintName, tableName)).append(";\n");
|
||||
sb.append("IF (OBJECT_ID('").append(maxConstraintName(uniqueConstraintName)).append("', 'UQ') IS NOT NULL) ");
|
||||
sb.append(super.alterTableDropUniqueConstraint(tableName, uniqueConstraintName));
|
||||
|
||||
sb.append(ifObjectExists(maxConstraintName(uniqueConstraintName), UNIQUE_CONSTRAINT))
|
||||
.append(super.alterTableDropUniqueConstraint(tableName, uniqueConstraintName));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and return the create sequence DDL.
|
||||
*/
|
||||
@@ -133,50 +133,57 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
|
||||
protected void alterColumnDefault(DdlWrite writer, AlterColumn alter) {
|
||||
// Unfortunately, the SqlServer creates default values with a random name.
|
||||
// You can specify a name in DDL, but this does not work in conjunction with
|
||||
// temporal tables in certain cases. So we have to delete the constraint with
|
||||
// a rather complex statement.
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (DdlHelp.isDropDefault(defaultValue)) {
|
||||
sb.append("EXEC usp_ebean_drop_default_constraint ").append(tableName).append(", ").append(columnName);
|
||||
} else {
|
||||
sb.append("alter table ").append(tableName);
|
||||
sb.append(" add default ").append(convertDefaultValue(defaultValue)).append(" for ").append(columnName);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnBaseAttributes(AlterColumn alter) {
|
||||
if (alter.getType() == null && alter.isNotnull() == null) {
|
||||
// No type change or notNull change
|
||||
// defaultValue change already handled in alterColumnDefaultValue
|
||||
return null;
|
||||
}
|
||||
String tableName = alter.getTableName();
|
||||
String columnName = alter.getColumnName();
|
||||
String type = alter.getType() != null ? alter.getType() : alter.getCurrentType();
|
||||
type = convert(type);
|
||||
boolean notnull = (alter.isNotnull() != null) ? alter.isNotnull() : Boolean.TRUE.equals(alter.isCurrentNotnull());
|
||||
String notnullClause = notnull ? " not null" : "";
|
||||
|
||||
return "alter table " + tableName + " " + alterColumn + " " + columnName + " " + type + notnullClause;
|
||||
String defaultValue = alter.getDefaultValue();
|
||||
if (DdlHelp.isDropDefault(defaultValue)) {
|
||||
writer.apply().appendStatement(execUspDropDefaultConstraint(tableName, columnName));
|
||||
} else {
|
||||
writer.apply().appendStatement(execUspDropDefaultConstraint(tableName, columnName));
|
||||
setDefaultValue(writer, tableName, columnName, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnType(String tableName, String columnName, String type) {
|
||||
public void alterColumn(DdlWrite writer, AlterColumn alter) {
|
||||
String tableName = alter.getTableName();
|
||||
String columnName = alter.getColumnName();
|
||||
if (alter.getType() == null && alter.isNotnull() == null) {
|
||||
// No type change or notNull change
|
||||
if (hasValue(alter.getDefaultValue())) {
|
||||
alterColumnDefault(writer, alter);
|
||||
}
|
||||
} else {
|
||||
// we must regenerate whole statement -> read altered and current value
|
||||
String type = alter.getType() != null ? alter.getType() : alter.getCurrentType();
|
||||
type = convert(type);
|
||||
boolean notnull = (alter.isNotnull() != null) ? alter.isNotnull() : Boolean.TRUE.equals(alter.isCurrentNotnull());
|
||||
String defaultValue = alter.getDefaultValue() != null ? alter.getDefaultValue() : alter.getCurrentDefaultValue();
|
||||
DdlBuffer buffer = writer.apply();
|
||||
if (hasValue(defaultValue)) {
|
||||
// default value present -> drop default constraint before altering
|
||||
buffer.appendStatement(execUspDropDefaultConstraint(tableName, columnName));
|
||||
}
|
||||
|
||||
// can't alter itself - done in alterColumnBaseAttributes()
|
||||
return null;
|
||||
}
|
||||
buffer = writer.apply().append("alter table ").append(tableName)
|
||||
.appendWithSpace(alterColumn).appendWithSpace(columnName);
|
||||
buffer.appendWithSpace(type);
|
||||
if (notnull) {
|
||||
buffer.append(" not null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterColumnNotnull(String tableName, String columnName, boolean notnull) {
|
||||
buffer.endOfStatement();
|
||||
|
||||
// can't alter itself - done in alterColumnBaseAttributes()
|
||||
return null;
|
||||
// re add - default constraint
|
||||
if (hasValue(defaultValue) && !DdlHelp.isDropDefault(defaultValue)) {
|
||||
setDefaultValue(writer, tableName, columnName, defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,9 +209,8 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
* (constraints, default values, indices and foreign keys). That's why we call a user stored procedure here
|
||||
*/
|
||||
@Override
|
||||
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) {
|
||||
|
||||
buffer.append("EXEC usp_ebean_drop_column ").append(tableName).append(", ").append(columnName).endOfStatement();
|
||||
public void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
writer.apply().append("EXEC usp_ebean_drop_column ").append(tableName).append(", ").append(columnName).endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,4 +252,18 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
.endOfStatement();
|
||||
}
|
||||
|
||||
private String ifObjectExists(String object, String objectType) {
|
||||
return "IF OBJECT_ID('" + object + "', '" + objectType + "') IS NOT NULL ";
|
||||
}
|
||||
|
||||
private String execUspDropDefaultConstraint(String tableName, String columnName) {
|
||||
return "EXEC usp_ebean_drop_default_constraint " + tableName + ", " + columnName;
|
||||
}
|
||||
|
||||
private void setDefaultValue(DdlWrite writer, String tableName, String columnName, String defaultValue) {
|
||||
writer.apply().append("alter table ").append(tableName)
|
||||
.append(" add default ").append(convertDefaultValue(defaultValue)).append(" for ").append(columnName)
|
||||
.endOfStatement();
|
||||
}
|
||||
|
||||
}
|
||||
+10
-2
@@ -4,6 +4,7 @@ import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
@@ -62,9 +63,16 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
apply.append("-- dropping history support for ").append(baseTable).endOfStatement();
|
||||
// drop default constraints
|
||||
DdlWrite tmpWriter = new DdlWrite();
|
||||
AlterColumn alter = new AlterColumn();
|
||||
alter.setTableName(baseTable);
|
||||
alter.setDefaultValue(DdlHelp.DROP_DEFAULT);
|
||||
alter.setColumnName(systemPeriodStart);
|
||||
platformDdl.alterColumn(tmpWriter, alter);
|
||||
alter.setColumnName(systemPeriodEnd);
|
||||
platformDdl.alterColumn(tmpWriter, alter);
|
||||
apply.append(tmpWriter.apply().getBuffer()); // workaround - will be refactored later
|
||||
|
||||
apply.append(platformDdl.alterColumnDefaultValue(baseTable, systemPeriodStart, DdlHelp.DROP_DEFAULT)).endOfStatement();
|
||||
apply.append(platformDdl.alterColumnDefaultValue(baseTable, systemPeriodEnd, DdlHelp.DROP_DEFAULT)).endOfStatement();
|
||||
// switch of versioning & period
|
||||
apply.append("alter table ").append(baseTable).append(" set (system_versioning = off)").endOfStatement();
|
||||
apply.append("alter table ").append(baseTable).append(" drop period for system_time").endOfStatement();
|
||||
|
||||
Reference in new issue
Block a user