mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Code changes to group alterTables by tableName
This commit is contained in:
1 parent
c3bdac5271
commit
1049d951d9
13 files changed
+547
-204
No files matched your search
+45
@@ -0,0 +1,45 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Object that represents an Alter Table statements. Table alters are grouped together by tableName in DDL and can be extended by
|
||||
* a ddl-specific handler (e.g. doing a reorg for DB2 or implement special grouping for Hana). There is one instance per table.
|
||||
*
|
||||
* @author TODO Roland Praml, FOCONIS AG
|
||||
*/
|
||||
public interface DdlAlterTable {
|
||||
|
||||
/**
|
||||
* Writes the alter table statements to <code>target</code>
|
||||
*/
|
||||
void write(Appendable target) throws IOException;
|
||||
|
||||
/**
|
||||
* Adds an alter table command for given column. When you invoke<br>
|
||||
* <code>alterTable(writer, "my_table").add("alter column","my_column).append("type integer")</code> the resulting DDL (in
|
||||
* standard implementation) would be<br>
|
||||
* <code>alter table my_table alter column my_column type integer</code>
|
||||
*
|
||||
* @return a DdlBuffer, which can be used for further appends. Note you MUST NOT call <code>.endOfStatement()</code> on this
|
||||
* buffer.
|
||||
*/
|
||||
DdlBuffer append(String operation, String columnName);
|
||||
|
||||
/**
|
||||
* Adds a raw command. This is mainly used for executing user stored procedures.
|
||||
*/
|
||||
DdlBuffer raw(String string);
|
||||
|
||||
/**
|
||||
* Flag that detects if history DDL (switching on) is handled for this table.
|
||||
*/
|
||||
boolean isHistoryHandled();
|
||||
|
||||
/**
|
||||
* Sets the history handled flag for this table.
|
||||
*/
|
||||
void setHistoryHandled();
|
||||
|
||||
|
||||
}
|
||||
+98
@@ -1,5 +1,10 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.BaseDdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.model.MConfiguration;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
@@ -16,6 +21,10 @@ public class DdlWrite {
|
||||
|
||||
private final DdlBuffer apply = new BaseDdlBuffer();
|
||||
|
||||
private final Map<String, DdlAlterTable> applyAlterTables = new TreeMap<>();
|
||||
|
||||
private final DdlBuffer applyPostAlter = new BaseDdlBuffer();
|
||||
|
||||
private final DdlBuffer applyForeignKeys = new BaseDdlBuffer();
|
||||
|
||||
private final DdlBuffer applyHistoryView = new BaseDdlBuffer();
|
||||
@@ -66,12 +75,21 @@ public class DdlWrite {
|
||||
*/
|
||||
public boolean isApplyEmpty() {
|
||||
return apply.getBuffer().isEmpty()
|
||||
&& applyAlterTables.isEmpty()
|
||||
&& applyPostAlter.getBuffer().isEmpty()
|
||||
&& applyForeignKeys.getBuffer().isEmpty()
|
||||
&& applyHistoryView.getBuffer().isEmpty()
|
||||
&& applyHistoryTrigger.getBuffer().isEmpty()
|
||||
&& applyDropDependencies.getBuffer().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the buffer that POST ALTER is written to.
|
||||
*/
|
||||
public DdlBuffer applyPostAlter() {
|
||||
return applyPostAlter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the buffer that APPLY DDL is written to.
|
||||
*/
|
||||
@@ -86,6 +104,20 @@ public class DdlWrite {
|
||||
return applyDropDependencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates or returns the DdlAlterTable statement for <code>tablename</code>.
|
||||
*
|
||||
* Note: All alters on a particular table are sorted in natural order in the ddl script. This allows optimizing the alters by
|
||||
* merging them or doing a reorg table after altering is done.
|
||||
*
|
||||
* @param tableName the table name
|
||||
* @param factory the factory to construct a new object
|
||||
* @return
|
||||
*/
|
||||
public DdlAlterTable applyAlterTable(String tableName, Function<String, DdlAlterTable> factory) {
|
||||
return applyAlterTables.computeIfAbsent(tableName, factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the buffer that APPLY DDL is written to for foreign keys and their associated indexes.
|
||||
* <p>
|
||||
@@ -124,4 +156,70 @@ public class DdlWrite {
|
||||
return dropAll;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the apply ddl to the target.
|
||||
*/
|
||||
public void writeApply(Appendable target) throws IOException {
|
||||
if (!applyDropDependencies.isEmpty()) {
|
||||
target.append("-- drop dependencies\n");
|
||||
target.append(applyDropDependencies.getBuffer());
|
||||
}
|
||||
if (!apply.isEmpty()) {
|
||||
target.append("-- apply changes\n");
|
||||
target.append(apply.getBuffer());
|
||||
}
|
||||
if (!applyAlterTables.isEmpty()) {
|
||||
// target.append("-- apply alter tables\n"); maybe added later. Makes diff easier
|
||||
for (DdlAlterTable alterTable : applyAlterTables.values()) {
|
||||
alterTable.write(target);
|
||||
}
|
||||
}
|
||||
if (!applyPostAlter.isEmpty()) {
|
||||
// target.append("-- apply post alter\n"); maybe added later.
|
||||
target.append(applyPostAlter.getBuffer());
|
||||
}
|
||||
if (!applyForeignKeys.isEmpty()) {
|
||||
// target.append("-- foreign keys and indices\n"); maybe added later.
|
||||
target.append(applyForeignKeys.getBuffer());
|
||||
}
|
||||
|
||||
if (!applyHistoryView.isEmpty()) {
|
||||
target.append(applyHistoryView.getBuffer());
|
||||
}
|
||||
|
||||
if (!applyHistoryTrigger.isEmpty()) {
|
||||
target.append(applyHistoryTrigger.getBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the drop all ddl to the target.
|
||||
*/
|
||||
public void writeDropAll(Appendable target) throws IOException {
|
||||
if (!dropAllForeignKeys.isEmpty()) {
|
||||
target.append("-- drop all foreign keys\n");
|
||||
target.append(dropAllForeignKeys.getBuffer());
|
||||
}
|
||||
if (!dropAll.isEmpty()) {
|
||||
target.append("-- drop all\n");
|
||||
target.append(dropAll.getBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all create statements. Mainly used for unit-tests
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
writeDropAll(sb);
|
||||
writeApply(sb);
|
||||
} catch (IOException e) {
|
||||
// can not happen
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+86
-22
@@ -3,12 +3,21 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.config.dbplatform.DbPlatformType;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
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.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -18,16 +27,13 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
|
||||
public AbstractHanaDdl(DatabasePlatform platform) {
|
||||
super(platform);
|
||||
this.addColumn = "add (";
|
||||
this.addColumnSuffix = ")";
|
||||
this.alterColumn = "alter (";
|
||||
this.alterColumnSuffix = ")";
|
||||
this.columnDropDefault = " default null";
|
||||
this.columnSetDefault = " default";
|
||||
this.columnSetNotnull = " not null";
|
||||
this.addColumn = "add";
|
||||
this.alterColumn = "alter";
|
||||
this.columnDropDefault = "default null";
|
||||
this.columnSetDefault = "default";
|
||||
this.columnSetNotnull = "not null";
|
||||
this.columnSetNull = " null";
|
||||
this.dropColumn = "drop (";
|
||||
this.dropColumnSuffix = ")";
|
||||
this.dropColumn = "drop";
|
||||
this.dropConstraintIfExists = "drop constraint ";
|
||||
this.dropIndexIfExists = "drop index ";
|
||||
this.dropSequenceIfExists = "drop sequence ";
|
||||
@@ -52,29 +58,21 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
: (alter.getDefaultValue() != null ? alter.getDefaultValue() : alter.getCurrentDefaultValue());
|
||||
String defaultValueClause = (defaultValue == null || defaultValue.isEmpty()) ? "" : " default " + defaultValue;
|
||||
|
||||
DdlBuffer buffer = writer.apply();
|
||||
if (!isConvertible(currentType, type)) {
|
||||
// add an intermediate conversion if possible
|
||||
if (isNumberType(currentType)) {
|
||||
// numbers can always be converted to decimal
|
||||
buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName)
|
||||
.append(" decimal ").append(notnullClause).append(alterColumnSuffix)
|
||||
.endOfStatement();
|
||||
alterTable(writer, tableName).append(alterColumn, columnName).append("decimal").append(notnullClause);
|
||||
|
||||
} else if (isStringType(currentType)) {
|
||||
// strings can always be converted to nclob
|
||||
// Note: we do not add default clause here to avoid error[SAP DBTech JDBC: [336]: invalid default value:
|
||||
// default value cannot be created on column of data type NCLOB
|
||||
buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName)
|
||||
.append(" nclob").append(notnullClause).append(alterColumnSuffix)
|
||||
.endOfStatement();
|
||||
alterTable(writer, tableName).append(alterColumn, columnName).append("nclob").append(notnullClause);
|
||||
}
|
||||
}
|
||||
|
||||
buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName)
|
||||
.append(" ").append(type).append(defaultValueClause).append(notnullClause).append(alterColumnSuffix);
|
||||
|
||||
buffer.endOfStatement();
|
||||
alterTable(writer, tableName).append(alterColumn, columnName).append(type).append(defaultValueClause).append(notnullClause);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -127,8 +125,7 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
*/
|
||||
@Override
|
||||
public void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
writer.apply().append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')")
|
||||
.endOfStatement();
|
||||
alterTable(writer, tableName).raw("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,4 +199,71 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
return type != null
|
||||
&& (type.startsWith("varchar") || type.startsWith("nvarchar") || "clob".equals(type) || "nclob".equals(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(tableName, HanaAlterTableWrite::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins alter table commands and add open/closing brackets for the alter statements
|
||||
*/
|
||||
private static class HanaAlterTableWrite extends BaseAlterTableWrite {
|
||||
|
||||
public HanaAlterTableWrite(String tableName) {
|
||||
super(tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AlterCmd> postProcessCommands(List<AlterCmd> cmds) {
|
||||
List<AlterCmd> newCmds = new ArrayList<>();
|
||||
Map<String, List<AlterCmd>> batches = new LinkedHashMap<>();
|
||||
Set<String> columns = new HashSet<>();
|
||||
for (AlterCmd cmd : cmds) {
|
||||
switch (cmd.getOperation()) {
|
||||
case "add":
|
||||
case "alter":
|
||||
case "drop":
|
||||
if (cmd.getColumn() != null && !columns.add(cmd.getColumn())) {
|
||||
// column already seen
|
||||
flushBatches(newCmds, batches);
|
||||
columns.clear();
|
||||
}
|
||||
batches.computeIfAbsent(cmd.getOperation(), k -> new ArrayList<>()).add(cmd);
|
||||
break;
|
||||
default:
|
||||
flushBatches(newCmds, batches);
|
||||
columns.clear();
|
||||
newCmds.add(cmd);
|
||||
}
|
||||
}
|
||||
flushBatches(newCmds, batches);
|
||||
return newCmds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges add/alter/drop commands into one statement.
|
||||
*/
|
||||
private void flushBatches(List<AlterCmd> newCmds, Map<String, List<AlterCmd>> batches) {
|
||||
for (Entry<String, List<AlterCmd>> entry : batches.entrySet()) {
|
||||
AlterCmd raw = newRawCommand("alter table ").append(tableName()).append(" ")
|
||||
.append(entry.getKey()).append(" (");
|
||||
List<AlterCmd> cmds = entry.getValue();
|
||||
for (int i = 0; i < cmds.size(); i++) {
|
||||
AlterCmd cmd = cmds.get(i);
|
||||
if (i > 0) {
|
||||
raw.append(",\n ");
|
||||
}
|
||||
raw.append(cmd.getColumn());
|
||||
if (!cmd.getAlternation().isEmpty()) {
|
||||
raw.append(" ").append(cmd.getAlternation());
|
||||
}
|
||||
}
|
||||
raw.append(")");
|
||||
newCmds.add(raw);
|
||||
}
|
||||
batches.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
|
||||
/**
|
||||
* Contains alter statements per table.
|
||||
*
|
||||
* @author Roland Praml, FOCONIS AG
|
||||
*/
|
||||
public class BaseAlterTableWrite implements DdlAlterTable {
|
||||
|
||||
protected static final String RAW_OPERATION = "$RAW";
|
||||
|
||||
public class AlterCmd {
|
||||
// the command (e.g. "alter", "modify"
|
||||
private final String operation;
|
||||
// the affected column (note: each column can only be altered once on MariaDB)
|
||||
private final String column;
|
||||
|
||||
private final DdlBuffer alternationBuffer = new BaseDdlBuffer() {
|
||||
@Override
|
||||
public DdlBuffer endOfStatement() {
|
||||
throw new UnsupportedOperationException();
|
||||
};
|
||||
};
|
||||
|
||||
protected AlterCmd(String operation, String column) {
|
||||
this.operation = operation;
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public AlterCmd append(String content) {
|
||||
alternationBuffer.append(content);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public String getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public String getAlternation() {
|
||||
return alternationBuffer.getBuffer();
|
||||
}
|
||||
|
||||
protected void write(Appendable target) throws IOException {
|
||||
if (operation.equals(RAW_OPERATION)) {
|
||||
// this is a raw command. e.g. an USP call. Must be done in the correct order
|
||||
// of all alter commands
|
||||
target.append(getAlternation());
|
||||
} else {
|
||||
target.append("alter table ").append(tableName).append(' ').append(operation);
|
||||
if (column != null) {
|
||||
target.append(' ').append(column);
|
||||
}
|
||||
if (!getAlternation().isEmpty()) {
|
||||
target.append(' ').append(getAlternation());
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
write(sb);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private final String tableName;
|
||||
|
||||
private List<AlterCmd> cmds = new ArrayList<>();
|
||||
|
||||
private boolean historyHandled;
|
||||
|
||||
public BaseAlterTableWrite(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String tableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
protected AlterCmd newRawCommand(String sql) {
|
||||
AlterCmd cmd = new AlterCmd(RAW_OPERATION, null);
|
||||
cmd.alternationBuffer.append(sql);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public AlterCmd newOperation(String operation, String column) {
|
||||
return new AlterCmd(operation, column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a statement. The statement is prefixed with "alter table TABLENAME" and may be batched, if platform supports this. The
|
||||
* returned StringBuilder can be used, to complete the statement
|
||||
*/
|
||||
@Override
|
||||
public DdlBuffer append(String operation, String column) {
|
||||
AlterCmd cmd = new AlterCmd(operation, column);
|
||||
cmds.add(cmd);
|
||||
return cmd.alternationBuffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DdlBuffer raw(String sql) {
|
||||
AlterCmd cmd = newRawCommand(sql);
|
||||
cmds.add(cmd);
|
||||
return cmd.alternationBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method can be overwritten to return a new list of commands. The given list must not be modified, but a new command list with
|
||||
* modified commands can be returned (e.g. to handle DB2 reorg or special syntax in Hana)
|
||||
*/
|
||||
protected List<AlterCmd> postProcessCommands(List<AlterCmd> cmds) {
|
||||
return cmds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the DDL to <code>target</code>.
|
||||
*/
|
||||
@Override
|
||||
public void write(Appendable target) throws IOException {
|
||||
for (AlterCmd cmd : postProcessCommands(Collections.unmodifiableList(cmds))) {
|
||||
cmd.write(target);
|
||||
target.append(";\n");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHistoryHandled() {
|
||||
return historyHandled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHistoryHandled() {
|
||||
historyHandled = true;
|
||||
}
|
||||
}
|
||||
+58
-53
@@ -11,7 +11,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.TableDdl;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.platform.util.IndexSet;
|
||||
import io.ebeaninternal.dbmigration.migration.AddColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.migration.AddTableComment;
|
||||
@@ -127,29 +126,29 @@ public class BaseTableDdl implements TableDdl {
|
||||
after = getScriptsForPlatform(alter.getAfter());
|
||||
}
|
||||
|
||||
void writeBefore(DdlBuffer buffer) {
|
||||
void write(DdlWrite writer) {
|
||||
if (!before.isEmpty()) {
|
||||
buffer.end();
|
||||
}
|
||||
|
||||
if (!before.isEmpty() && withHistory) {
|
||||
buffer.append("-- NOTE: table has @History - special migration may be necessary").newLine();
|
||||
}
|
||||
for (String ddlScript : before) {
|
||||
buffer.appendStatement(translate(ddlScript, tableName, columnName, defaultValue));
|
||||
}
|
||||
}
|
||||
|
||||
void writeAfter(DdlBuffer buffer) {
|
||||
if (!after.isEmpty() && withHistory) {
|
||||
buffer.append("-- NOTE: table has @History - special migration may be necessary").newLine();
|
||||
}
|
||||
// here we run post migration scripts
|
||||
for (String ddlScript : after) {
|
||||
buffer.appendStatement(translate(ddlScript, tableName, columnName, defaultValue));
|
||||
writer.apply().end();
|
||||
if (withHistory) {
|
||||
writer.apply().append("-- NOTE: table has @History - special migration may be necessary").newLine();
|
||||
}
|
||||
for (String ddlScript : before) {
|
||||
writer.apply().appendStatement(translate(ddlScript, tableName, columnName, defaultValue));
|
||||
}
|
||||
}
|
||||
// effective order in DDL.sql:
|
||||
// 'before' goes to apply-buffer
|
||||
// altered columns will be in the alterTable buffers.
|
||||
// 'after' goes to the post-alter-buffer
|
||||
if (!after.isEmpty()) {
|
||||
buffer.end();
|
||||
writer.applyPostAlter().append("-- NOTE: table has @History - special migration may be necessary").newLine();
|
||||
|
||||
// here we run post migration scripts
|
||||
for (String ddlScript : after) {
|
||||
writer.applyPostAlter().appendStatement(translate(ddlScript, tableName, columnName, defaultValue));
|
||||
}
|
||||
|
||||
writer.applyPostAlter().end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,7 +247,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
}
|
||||
}
|
||||
if (platformDdl.isInlineForeignKeys()) {
|
||||
writeInlineForeignKeys(writer, createTable);
|
||||
writeInlineForeignKeys(apply, createTable);
|
||||
}
|
||||
apply.newLine().append(")");
|
||||
addTableStorageEngine(apply, createTable);
|
||||
@@ -340,7 +339,8 @@ public class BaseTableDdl implements TableDdl {
|
||||
uqName = col.getUnique();
|
||||
}
|
||||
String[] columnNames = {col.getName()};
|
||||
writer.apply().appendStatement(platformDdl.alterTableAddUniqueConstraint(tableName, uqName, columnNames, Boolean.TRUE.equals(col.isNotnull()) ? null : columnNames));
|
||||
String[] nullableColumns = Boolean.TRUE.equals(col.isNotnull()) ? null : columnNames;
|
||||
writer.applyPostAlter().appendStatement(platformDdl.alterTableAddUniqueConstraint(tableName, uqName, columnNames, nullableColumns));
|
||||
writer.dropAllForeignKeys().appendStatement(platformDdl.dropIndex(uqName, tableName));
|
||||
}
|
||||
|
||||
@@ -349,7 +349,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
String[] columnNames = split(constraint.getColumnNames());
|
||||
String[] nullableColumns = split(constraint.getNullableColumns());
|
||||
|
||||
writer.apply().appendStatement(platformDdl.alterTableAddUniqueConstraint(tableName, uqName, columnNames, nullableColumns));
|
||||
writer.applyPostAlter().appendStatement(platformDdl.alterTableAddUniqueConstraint(tableName, uqName, columnNames, nullableColumns));
|
||||
writer.dropAllForeignKeys().appendStatement(platformDdl.dropIndex(uqName, tableName));
|
||||
}
|
||||
}
|
||||
@@ -368,25 +368,25 @@ public class BaseTableDdl implements TableDdl {
|
||||
platformDdl.createWithHistory(writer, table);
|
||||
}
|
||||
|
||||
protected void writeInlineForeignKeys(DdlWrite writer, CreateTable createTable) {
|
||||
protected void writeInlineForeignKeys(DdlBuffer buffer, CreateTable createTable) {
|
||||
for (Column column : createTable.getColumn()) {
|
||||
String references = column.getReferences();
|
||||
if (hasValue(references)) {
|
||||
writeInlineForeignKey(writer, column);
|
||||
writeInlineForeignKey(buffer, column);
|
||||
}
|
||||
}
|
||||
writeInlineCompoundForeignKeys(writer, createTable);
|
||||
writeInlineCompoundForeignKeys(buffer, createTable);
|
||||
}
|
||||
|
||||
protected void writeInlineForeignKey(DdlWrite writer, Column column) {
|
||||
protected void writeInlineForeignKey(DdlBuffer buffer, Column column) {
|
||||
String fkConstraint = platformDdl.tableInlineForeignKey(new WriteForeignKey(null, column));
|
||||
writer.apply().append(",").newLine().append(" ").append(fkConstraint);
|
||||
buffer.append(",").newLine().append(" ").append(fkConstraint);
|
||||
}
|
||||
|
||||
protected void writeInlineCompoundForeignKeys(DdlWrite writer, CreateTable createTable) {
|
||||
protected void writeInlineCompoundForeignKeys(DdlBuffer buffer, CreateTable createTable) {
|
||||
for (ForeignKey key : createTable.getForeignKey()) {
|
||||
String fkConstraint = platformDdl.tableInlineForeignKey(new WriteForeignKey(null, key));
|
||||
writer.apply().append(",").newLine().append(" ").append(fkConstraint);
|
||||
buffer.append(",").newLine().append(" ").append(fkConstraint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -558,7 +558,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
@Override
|
||||
public void generate(DdlWrite writer, CreateIndex index) {
|
||||
if (platformInclude(index.getPlatforms())) {
|
||||
writer.apply().appendStatement(platformDdl.createIndex(new WriteCreateIndex(index)));
|
||||
writer.applyPostAlter().appendStatement(platformDdl.createIndex(new WriteCreateIndex(index)));
|
||||
writer.dropAll().appendStatement(platformDdl.dropIndex(index.getIndexName(), index.getTableName(), Boolean.TRUE.equals(index.isConcurrent())));
|
||||
}
|
||||
}
|
||||
@@ -566,7 +566,8 @@ public class BaseTableDdl implements TableDdl {
|
||||
@Override
|
||||
public void generate(DdlWrite writer, DropIndex dropIndex) {
|
||||
if (platformInclude(dropIndex.getPlatforms())) {
|
||||
writer.apply().appendStatement(platformDdl.dropIndex(dropIndex.getIndexName(), dropIndex.getTableName(), Boolean.TRUE.equals(dropIndex.isConcurrent())));
|
||||
writer.applyDropDependencies()
|
||||
.appendStatement(platformDdl.dropIndex(dropIndex.getIndexName(), dropIndex.getTableName(), Boolean.TRUE.equals(dropIndex.isConcurrent())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,12 +575,14 @@ public class BaseTableDdl implements TableDdl {
|
||||
public void generate(DdlWrite writer, AddUniqueConstraint constraint) {
|
||||
if (platformInclude(constraint.getPlatforms())) {
|
||||
if (DdlHelp.isDropConstraint(constraint.getColumnNames())) {
|
||||
writer.apply().appendStatement(platformDdl.alterTableDropUniqueConstraint(constraint.getTableName(), constraint.getConstraintName()));
|
||||
writer.applyDropDependencies()
|
||||
.appendStatement(platformDdl.alterTableDropUniqueConstraint(constraint.getTableName(), constraint.getConstraintName()));
|
||||
|
||||
} else {
|
||||
String[] cols = split(constraint.getColumnNames());
|
||||
String[] nullableColumns = split(constraint.getNullableColumns());
|
||||
writer.apply().appendStatement(platformDdl.alterTableAddUniqueConstraint(constraint.getTableName(), constraint.getConstraintName(), cols, nullableColumns));
|
||||
writer.applyPostAlter()
|
||||
.appendStatement(platformDdl.alterTableAddUniqueConstraint(constraint.getTableName(), constraint.getConstraintName(), cols, nullableColumns));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -587,9 +590,9 @@ public class BaseTableDdl implements TableDdl {
|
||||
@Override
|
||||
public void generate(DdlWrite writer, AlterForeignKey alterForeignKey) {
|
||||
if (DdlHelp.isDropForeignKey(alterForeignKey.getColumnNames())) {
|
||||
writer.apply().appendStatement(platformDdl.alterTableDropForeignKey(alterForeignKey.getTableName(), alterForeignKey.getName()));
|
||||
writer.applyDropDependencies().appendStatement(platformDdl.alterTableDropForeignKey(alterForeignKey.getTableName(), alterForeignKey.getName()));
|
||||
} else {
|
||||
writer.apply().appendStatement(platformDdl.alterTableAddForeignKey(writer.getOptions(), new WriteForeignKey(alterForeignKey)));
|
||||
writer.applyForeignKeys().appendStatement(platformDdl.alterTableAddForeignKey(writer.getOptions(), new WriteForeignKey(alterForeignKey)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -634,7 +637,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
@Override
|
||||
public void generate(DdlWrite writer, AddTableComment addTableComment) {
|
||||
if (hasValue(addTableComment.getComment())) {
|
||||
platformDdl.addTableComment(writer.apply(), addTableComment.getName(), addTableComment.getComment());
|
||||
platformDdl.addTableComment(writer.applyPostAlter(), addTableComment.getName(), addTableComment.getComment());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -669,7 +672,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
*/
|
||||
@Override
|
||||
public void generate(DdlWrite writer, DropTable dropTable) {
|
||||
dropTable(writer.apply(), dropTable.getName());
|
||||
dropTable(writer.applyPostAlter(), dropTable.getName());
|
||||
if (hasValue(dropTable.getSequenceCol())
|
||||
&& platformDdl.getPlatform().getDbIdentity().isSupportsSequence()) {
|
||||
String sequenceName = dropTable.getSequenceName();
|
||||
@@ -702,7 +705,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
@Override
|
||||
public void generate(DdlWrite writer, AlterColumn alterColumn) {
|
||||
DdlMigrationHelp ddlHelp = new DdlMigrationHelp(alterColumn);
|
||||
ddlHelp.writeBefore(writer.apply());
|
||||
ddlHelp.write(writer);
|
||||
|
||||
if (isTrue(alterColumn.isHistoryExclude())) {
|
||||
regenerateHistoryTriggers(alterColumn.getTableName(), HistoryTableUpdate.Change.EXCLUDE, alterColumn.getColumnName());
|
||||
@@ -750,11 +753,10 @@ public class BaseTableDdl implements TableDdl {
|
||||
// add constraint last (after potential type change)
|
||||
addCheckConstraint(writer, alterColumn);
|
||||
}
|
||||
ddlHelp.writeAfter(writer.apply());
|
||||
}
|
||||
|
||||
private void alterColumnComment(DdlWrite writer, AlterColumn alterColumn) {
|
||||
platformDdl.addColumnComment(writer.apply(), alterColumn.getTableName(), alterColumn.getColumnName(), alterColumn.getComment());
|
||||
platformDdl.addColumnComment(writer.applyPostAlter(), alterColumn.getTableName(), alterColumn.getColumnName(), alterColumn.getComment());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -792,23 +794,28 @@ public class BaseTableDdl implements TableDdl {
|
||||
}
|
||||
|
||||
protected void dropCheckConstraint(DdlWrite writer, AlterColumn alter, String constraintName) {
|
||||
writer.apply().appendStatement(platformDdl.alterTableDropConstraint(alter.getTableName(), constraintName));
|
||||
writer.applyDropDependencies()
|
||||
.appendStatement(platformDdl.alterTableDropConstraint(alter.getTableName(), constraintName));
|
||||
}
|
||||
|
||||
protected void addCheckConstraint(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().appendStatement(platformDdl.alterTableAddCheckConstraint(alter.getTableName(), alter.getCheckConstraintName(), alter.getCheckConstraint()));
|
||||
writer.applyPostAlter()
|
||||
.appendStatement(platformDdl.alterTableAddCheckConstraint(alter.getTableName(), alter.getCheckConstraintName(), alter.getCheckConstraint()));
|
||||
}
|
||||
|
||||
|
||||
protected void alterColumnAddForeignKey(DdlWrite writer, AlterColumn alterColumn) {
|
||||
alterTableAddForeignKey(writer.getOptions(), writer.apply(), new WriteForeignKey(alterColumn));
|
||||
alterTableAddForeignKey(writer.getOptions(), writer.applyForeignKeys(), new WriteForeignKey(alterColumn));
|
||||
}
|
||||
|
||||
protected void alterColumnDropForeignKey(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().appendStatement(platformDdl.alterTableDropForeignKey(alter.getTableName(), alter.getDropForeignKey()));
|
||||
writer.applyDropDependencies()
|
||||
.appendStatement(platformDdl.alterTableDropForeignKey(alter.getTableName(), alter.getDropForeignKey()));
|
||||
}
|
||||
|
||||
protected void alterColumnDropUniqueConstraint(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().appendStatement(platformDdl.alterTableDropUniqueConstraint(alter.getTableName(), alter.getDropUnique()));
|
||||
writer.applyDropDependencies()
|
||||
.appendStatement(platformDdl.alterTableDropUniqueConstraint(alter.getTableName(), alter.getDropUnique()));
|
||||
}
|
||||
|
||||
protected void alterColumnAddUniqueOneToOneConstraint(DdlWrite writer, AlterColumn alter) {
|
||||
@@ -822,7 +829,8 @@ public class BaseTableDdl implements TableDdl {
|
||||
protected void addUniqueConstraint(DdlWrite writer, AlterColumn alter, String uqName) {
|
||||
String[] cols = {alter.getColumnName()};
|
||||
boolean notNull = alter.isNotnull() != null ? alter.isNotnull() : Boolean.TRUE.equals(alter.isNotnull());
|
||||
writer.apply().appendStatement(platformDdl.alterTableAddUniqueConstraint(alter.getTableName(), uqName, cols, notNull ? null : cols));
|
||||
writer.applyPostAlter()
|
||||
.appendStatement(platformDdl.alterTableAddUniqueConstraint(alter.getTableName(), uqName, cols, notNull ? null : cols));
|
||||
|
||||
writer.dropAllForeignKeys().appendStatement(platformDdl.dropIndex(uqName, alter.getTableName()));
|
||||
}
|
||||
@@ -835,18 +843,15 @@ public class BaseTableDdl implements TableDdl {
|
||||
protected void alterTableAddColumn(DdlWrite writer, String tableName, Column column, boolean onHistoryTable, boolean withHistory) {
|
||||
DdlMigrationHelp help = new DdlMigrationHelp(tableName, column, withHistory);
|
||||
if (!onHistoryTable) {
|
||||
help.writeBefore(writer.apply());
|
||||
help.write(writer);
|
||||
}
|
||||
|
||||
platformDdl.alterTableAddColumn(writer, tableName, column, onHistoryTable, help.getDefaultValue());
|
||||
final String comment = column.getComment();
|
||||
if (comment != null && !comment.isEmpty()) {
|
||||
platformDdl.addColumnComment(writer.apply(), tableName, column.getName(), comment);
|
||||
platformDdl.addColumnComment(writer.applyPostAlter(), tableName, column.getName(), comment);
|
||||
}
|
||||
|
||||
if (!onHistoryTable) {
|
||||
help.writeAfter(writer.apply());
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isFalse(Boolean value) {
|
||||
|
||||
+23
-18
@@ -5,7 +5,6 @@ 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;
|
||||
@@ -63,8 +62,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
protected abstract void updateHistoryTriggers(DbTriggerUpdate triggerUpdate);
|
||||
|
||||
/**
|
||||
* Process the HistoryTableUpdate which can result in changes to the apply, rollback
|
||||
* and drop scripts.
|
||||
* Process the HistoryTableUpdate which can result in changes to the apply, rollback and drop scripts.
|
||||
*/
|
||||
protected void updateTriggers(DdlWrite writer, MTable table, HistoryTableUpdate update) {
|
||||
|
||||
@@ -88,7 +86,10 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
// drop in appropriate order
|
||||
dropTriggers(writer.applyDropDependencies(), baseTable);
|
||||
dropHistoryTableEtc(writer.applyDropDependencies(), baseTable);
|
||||
dropWithHistoryView(writer.applyDropDependencies(), baseTable);
|
||||
dropHistoryTable(writer.applyDropDependencies(), baseTable);
|
||||
|
||||
dropSysPeriodColumns(writer, baseTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,14 +108,19 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
public void createWithHistory(DdlWrite writer, MTable table) {
|
||||
|
||||
String baseTable = table.getName();
|
||||
String whenCreatedColumn = table.getWhenCreatedColumn();
|
||||
|
||||
dropTriggers(writer.dropAll(), baseTable);
|
||||
dropHistoryTableEtc(writer.dropAll(), baseTable);
|
||||
addSysPeriodColumns(writer.apply(), baseTable, table.getWhenCreatedColumn());
|
||||
createHistoryTable(writer.applyPostAlter(), table);
|
||||
|
||||
addHistoryTable(writer, table, whenCreatedColumn);
|
||||
createStoredFunction(writer, table);
|
||||
createTriggers(writer, table);
|
||||
createWithHistoryView(writer.applyPostAlter(), table.getName());
|
||||
|
||||
// drop all scripts
|
||||
dropTriggers(writer.dropAll(), baseTable);
|
||||
dropWithHistoryView(writer.dropAll(), baseTable);
|
||||
dropHistoryTable(writer.dropAll(), baseTable);
|
||||
// no need to dropSysPeriodColumns as whole table will be deleted soon
|
||||
|
||||
}
|
||||
|
||||
protected abstract void createTriggers(DdlWrite writer, MTable table);
|
||||
@@ -156,7 +162,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
|
||||
addSysPeriodColumns(apply, baseTableName, whenCreatedColumn);
|
||||
createHistoryTable(apply, table);
|
||||
createHistoryTable(writer.applyPostAlter(), table);
|
||||
createWithHistoryView(apply, baseTableName);
|
||||
}
|
||||
|
||||
@@ -234,18 +240,17 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
appendColumnName(apply, prefix, sysPeriodEnd);
|
||||
}
|
||||
|
||||
protected void dropHistoryTableEtc(DdlBuffer buffer, String baseTableName) {
|
||||
|
||||
buffer.append("drop view ").append(baseTableName).append(viewSuffix).endOfStatement();
|
||||
dropSysPeriodColumns(buffer, baseTableName);
|
||||
buffer.append("drop table ").append(baseTableName).append(historySuffix).endOfStatement().end();
|
||||
protected void dropWithHistoryView(DdlBuffer apply, String baseTableName) {
|
||||
apply.append("drop view ").append(baseTableName).append(viewSuffix).endOfStatement();
|
||||
}
|
||||
|
||||
protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) {
|
||||
DdlWrite writer = new DdlWrite();
|
||||
protected void dropHistoryTable(DdlBuffer apply, String baseTableName) {
|
||||
apply.append("drop table ").append(baseTableName).append(historySuffix).endOfStatement().end();
|
||||
}
|
||||
|
||||
protected void dropSysPeriodColumns(DdlWrite writer, String baseTableName) {
|
||||
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) {
|
||||
|
||||
+5
-21
@@ -19,7 +19,6 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
private String systemPeriodEnd;
|
||||
private PlatformDdl platformDdl;
|
||||
private String historySuffix;
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
private Map<String, String> createdHistoryTables = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
@@ -64,7 +63,8 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
apply.append("alter table ").append(tableName).append(" add period for system_time(").append(systemPeriodStart)
|
||||
.append(",").append(systemPeriodEnd).append(")").endOfStatement();
|
||||
|
||||
enableSystemVersioning(apply, tableName, historyTableName, true, false);
|
||||
enableSystemVersioning(apply, tableName, true);
|
||||
platformDdl.alterTable(writer, tableName).setHistoryHandled();
|
||||
|
||||
createdHistoryTables.put(tableName, historyTableName);
|
||||
|
||||
@@ -123,30 +123,14 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
public void disableSystemVersioning(DdlBuffer apply, String tableName) {
|
||||
disableSystemVersioning(apply, tableName, false);
|
||||
apply.append("alter table ").append(tableName).append(" drop system versioning").endOfStatement();
|
||||
}
|
||||
|
||||
public void disableSystemVersioning(DdlBuffer apply, String tableName, boolean uniqueStatement) {
|
||||
apply.append("alter table ").append(tableName).append(" drop system versioning");
|
||||
if (uniqueStatement) {
|
||||
// needed for the DB migration test to prevent the statement from being filtered
|
||||
// out as a duplicate
|
||||
apply.append(" /* ").append(String.valueOf(counter.getAndIncrement())).append(" */");
|
||||
}
|
||||
apply.endOfStatement();
|
||||
}
|
||||
|
||||
public void enableSystemVersioning(DdlBuffer apply, String tableName, String historyTableName, boolean validated,
|
||||
boolean uniqueStatement) {
|
||||
apply.append("alter table ").append(tableName).append(" add system versioning history table ").append(historyTableName);
|
||||
public void enableSystemVersioning(DdlBuffer apply, String tableName, boolean validated) {
|
||||
apply.append("alter table ").append(tableName).append(" add system versioning history table ").append(tableName).append(historySuffix);
|
||||
if (!validated) {
|
||||
apply.append(" not validated");
|
||||
}
|
||||
if (uniqueStatement) {
|
||||
// needed for the DB migration test to prevent the statement from being filtered
|
||||
// out as a duplicate
|
||||
apply.append(" /* ").append(String.valueOf(counter.getAndIncrement())).append(" */");
|
||||
}
|
||||
apply.endOfStatement();
|
||||
}
|
||||
|
||||
|
||||
+19
-15
@@ -2,6 +2,7 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.PropertiesWrapper;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
@@ -14,17 +15,10 @@ import java.util.List;
|
||||
public class HanaTableDdl extends BaseTableDdl {
|
||||
|
||||
private final HanaHistoryDdl historyDdl;
|
||||
private final boolean generateUniqueDdl;
|
||||
|
||||
public HanaTableDdl(DatabaseConfig config, PlatformDdl platformDdl) {
|
||||
super(config, platformDdl);
|
||||
this.historyDdl = (HanaHistoryDdl) platformDdl.historyDdl;
|
||||
if (config.getProperties() != null) {
|
||||
PropertiesWrapper wrapper = new PropertiesWrapper("ebean", "hana", config.getProperties(), config.getClassLoadConfig());
|
||||
this.generateUniqueDdl = wrapper.getBoolean("generateUniqueDdl", false);
|
||||
} else {
|
||||
this.generateUniqueDdl = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,7 +33,12 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
historyDdl.disableSystemVersioning(writer.apply(), table.getName(), this.generateUniqueDdl);
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
alter.setHistoryHandled();
|
||||
historyDdl.disableSystemVersioning(writer.apply(), table.getName());
|
||||
historyDdl.enableSystemVersioning(writer.applyPostAlter(), table.getName(), false);
|
||||
}
|
||||
}
|
||||
|
||||
super.generate(writer, addColumn);
|
||||
@@ -51,8 +50,6 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
for (Column column : columns) {
|
||||
alterTableAddColumn(writer, historyTable, column, true, true);
|
||||
}
|
||||
|
||||
historyDdl.enableSystemVersioning(writer.apply(), table.getName(), historyTable, false, this.generateUniqueDdl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +65,12 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
historyDdl.disableSystemVersioning(writer.apply(), tableName, this.generateUniqueDdl);
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
alter.setHistoryHandled();
|
||||
historyDdl.disableSystemVersioning(writer.apply(), table.getName());
|
||||
historyDdl.enableSystemVersioning(writer.applyPostAlter(), table.getName(), false);
|
||||
}
|
||||
}
|
||||
|
||||
super.generate(writer, alterColumn);
|
||||
@@ -89,8 +91,6 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
platformDdl.alterColumn(writer, alterHistoryColumn);
|
||||
|
||||
}
|
||||
|
||||
historyDdl.enableSystemVersioning(writer.apply(), tableName, historyTable, false, this.generateUniqueDdl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +106,12 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
historyDdl.disableSystemVersioning(writer.apply(), tableName, this.generateUniqueDdl);
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
alter.setHistoryHandled();
|
||||
historyDdl.disableSystemVersioning(writer.apply(), table.getName());
|
||||
historyDdl.enableSystemVersioning(writer.applyPostAlter(), tableName, false);
|
||||
}
|
||||
}
|
||||
|
||||
super.generate(writer, dropColumn);
|
||||
@@ -116,7 +121,6 @@ public class HanaTableDdl extends BaseTableDdl {
|
||||
String historyTable = historyTable(tableName);
|
||||
alterTableDropColumn(writer, historyTable, dropColumn.getColumnName());
|
||||
|
||||
historyDdl.enableSystemVersioning(writer.apply(), tableName, historyTable, false, this.generateUniqueDdl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+19
-36
@@ -77,16 +77,12 @@ public class PlatformDdl {
|
||||
|
||||
protected String alterColumn = "alter column";
|
||||
|
||||
protected String alterColumnSuffix = "";
|
||||
|
||||
protected String dropUniqueConstraint = "drop constraint";
|
||||
|
||||
protected String addConstraint = "add constraint";
|
||||
|
||||
protected String addColumn = "add column";
|
||||
|
||||
protected String addColumnSuffix = "";
|
||||
|
||||
protected String columnSetType = "";
|
||||
|
||||
protected String columnSetDefault = "set default";
|
||||
@@ -105,8 +101,6 @@ public class PlatformDdl {
|
||||
|
||||
protected String dropColumn = "drop column";
|
||||
|
||||
protected String dropColumnSuffix = "";
|
||||
|
||||
protected String addForeignKeySkipCheck = "";
|
||||
|
||||
protected String uniqueIndex = "unique";
|
||||
@@ -536,10 +530,8 @@ public class PlatformDdl {
|
||||
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);
|
||||
DdlBuffer buffer = alterTable(writer, tableName).append(addColumn, column.getName());
|
||||
buffer.append(convertedType);
|
||||
|
||||
// Add default value also to history table if it is not excluded
|
||||
if (defaultValue != null) {
|
||||
@@ -553,27 +545,18 @@ public class PlatformDdl {
|
||||
if (isTrue(column.isNotnull())) {
|
||||
buffer.appendWithSpace(columnNotNull);
|
||||
}
|
||||
buffer.append(addColumnSuffix);
|
||||
buffer.endOfStatement();
|
||||
|
||||
// check constraints cannot be added in one statement for h2
|
||||
if (!StringHelper.isNull(column.getCheckConstraint())) {
|
||||
String ddl = alterTableAddCheckConstraint(tableName, column.getCheckConstraintName(), column.getCheckConstraint());
|
||||
if (hasValue(ddl)) {
|
||||
buffer.append(ddl).endOfStatement();
|
||||
}
|
||||
writer.applyPostAlter().appendStatement(ddl);
|
||||
}
|
||||
} else {
|
||||
buffer.append(addColumnSuffix);
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
alterTable(writer, tableName).append(dropColumn, columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -592,10 +575,8 @@ public class PlatformDdl {
|
||||
* </p>
|
||||
*/
|
||||
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();
|
||||
alterTable(writer, alter.getTableName()).append(alterColumn, alter.getColumnName())
|
||||
.append(columnSetType).append(convert(alter.getType()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -605,15 +586,12 @@ public class PlatformDdl {
|
||||
* </p>
|
||||
*/
|
||||
protected void alterColumnNotnull(DdlWrite writer, AlterColumn alter) {
|
||||
|
||||
DdlBuffer buffer = writer.apply().append("alter table ").append(alter.getTableName())
|
||||
.appendWithSpace(alterColumn).appendWithSpace(alter.getColumnName());
|
||||
DdlBuffer buffer = alterTable(writer, alter.getTableName()).append(alterColumn, alter.getColumnName());
|
||||
if (Boolean.TRUE.equals(alter.isNotnull())) {
|
||||
buffer.appendWithSpace(columnSetNotnull);
|
||||
buffer.append(columnSetNotnull);
|
||||
} else {
|
||||
buffer.appendWithSpace(columnSetNull);
|
||||
buffer.append(columnSetNull);
|
||||
}
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -630,14 +608,12 @@ public class PlatformDdl {
|
||||
* </p>
|
||||
*/
|
||||
protected void alterColumnDefault(DdlWrite writer, AlterColumn alter) {
|
||||
DdlBuffer buffer = writer.apply().append("alter table ").append(alter.getTableName())
|
||||
.appendWithSpace(alterColumn).appendWithSpace(alter.getColumnName());
|
||||
DdlBuffer buffer = alterTable(writer, alter.getTableName()).append(alterColumn, alter.getColumnName());
|
||||
if (DdlHelp.isDropDefault(alter.getDefaultValue())) {
|
||||
buffer.appendWithSpace(columnDropDefault);
|
||||
buffer.append(columnDropDefault);
|
||||
} else {
|
||||
buffer.appendWithSpace(columnSetDefault).appendWithSpace(convertDefaultValue(alter.getDefaultValue()));
|
||||
buffer.append(columnSetDefault).appendWithSpace(convertDefaultValue(alter.getDefaultValue()));
|
||||
}
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -662,6 +638,13 @@ public class PlatformDdl {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates or replace a new DdlAlterTable for given tableName.
|
||||
*/
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(tableName, BaseAlterTableWrite::new);
|
||||
}
|
||||
|
||||
protected void appendColumns(String[] columns, StringBuilder buffer) {
|
||||
buffer.append(" (");
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
|
||||
+4
-3
@@ -47,8 +47,8 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) {
|
||||
buffer.append("alter table ").append(baseTableName).append(" drop column ").append(sysPeriod).endOfStatement();
|
||||
protected void dropSysPeriodColumns(DdlWrite writer, String baseTableName) {
|
||||
platformDdl.alterTableDropColumn(writer, baseTableName, sysPeriod);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,8 +56,9 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
String baseTableName = table.getName();
|
||||
String procedureName = procedureName(baseTableName);
|
||||
String triggerName = triggerName(baseTableName);
|
||||
|
||||
List<String> columnNames = columnNamesForApply(table);
|
||||
DdlBuffer apply = writer.applyHistoryTrigger();
|
||||
createOrReplaceFunction(apply, procedureName, historyTableName(baseTableName), columnNames);
|
||||
apply
|
||||
.append("create trigger ").append(triggerName).newLine()
|
||||
.append(" before update or delete on ").append(baseTableName).newLine()
|
||||
|
||||
+24
-14
@@ -1,10 +1,13 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
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.
|
||||
@@ -61,21 +64,28 @@ public class SQLiteDdl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void alterColumnType(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().append("-- not supported: ");
|
||||
super.alterColumnType(writer, alter);
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(tableName, SQLiteAlterTableWrite::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void alterColumnDefault(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().append("-- not supported: ");
|
||||
super.alterColumnDefault(writer, alter);
|
||||
}
|
||||
static class SQLiteAlterTableWrite extends BaseAlterTableWrite {
|
||||
public SQLiteAlterTableWrite(String tableName) {
|
||||
super(tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void alterColumnNotnull(DdlWrite writer, AlterColumn alter) {
|
||||
writer.apply().append("-- not supported: ");
|
||||
super.alterColumnNotnull(writer, alter);
|
||||
@Override
|
||||
protected List<AlterCmd> postProcessCommands(List<AlterCmd> cmds) {
|
||||
List<AlterCmd> ret = new ArrayList<AlterCmd>();
|
||||
for (AlterCmd cmd : cmds) {
|
||||
switch (cmd.getOperation()) {
|
||||
case "alter column":
|
||||
ret.add(newRawCommand("-- not supported: " + cmd));
|
||||
break;
|
||||
default:
|
||||
ret.add(cmd);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+13
-6
@@ -16,6 +16,7 @@ import io.ebeaninternal.extraddl.model.ExtraDdl;
|
||||
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
|
||||
@@ -127,10 +128,13 @@ public class CurrentModel {
|
||||
if (jaxbPresent) {
|
||||
addExtraDdl(ddl, ExtraDdlXmlReader.readBuiltin(), "-- init script ");
|
||||
}
|
||||
ddl.append(writer.apply().getBuffer());
|
||||
ddl.append(writer.applyForeignKeys().getBuffer());
|
||||
ddl.append(writer.applyHistoryView().getBuffer());
|
||||
ddl.append(writer.applyHistoryTrigger().getBuffer());
|
||||
|
||||
try {
|
||||
writer.writeApply(ddl);
|
||||
} catch (IOException e) { // should not happen on StringBuilder
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return ddl.toString();
|
||||
}
|
||||
|
||||
@@ -157,8 +161,11 @@ public class CurrentModel {
|
||||
if (ddlHeader != null && !ddlHeader.isEmpty()) {
|
||||
ddl.append(ddlHeader).append('\n');
|
||||
}
|
||||
ddl.append(writer.dropAllForeignKeys().getBuffer());
|
||||
ddl.append(writer.dropAll().getBuffer());
|
||||
try {
|
||||
writer.writeDropAll(ddl);
|
||||
} catch (IOException e) { // should not happen on StringBuilder
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return ddl.toString();
|
||||
}
|
||||
|
||||
|
||||
+1
-16
@@ -3,7 +3,6 @@ package io.ebeaninternal.dbmigration.model;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.util.IOUtils;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
@@ -91,21 +90,7 @@ public class PlatformDdlWriter {
|
||||
if (header != null && !header.isEmpty()) {
|
||||
writer.append(header).append('\n');
|
||||
}
|
||||
// merge the apply buffers in the appropriate order
|
||||
prependDropDependencies(writer, ddl.applyDropDependencies());
|
||||
writer.append("-- apply changes\n");
|
||||
writer.append(ddl.apply().getBuffer());
|
||||
writer.append(ddl.applyForeignKeys().getBuffer());
|
||||
writer.append(ddl.applyHistoryView().getBuffer());
|
||||
writer.append(ddl.applyHistoryTrigger().getBuffer());
|
||||
}
|
||||
|
||||
private void prependDropDependencies(Writer writer, DdlBuffer buffer) throws IOException {
|
||||
if (!buffer.isEmpty()) {
|
||||
writer.append("-- drop dependencies\n");
|
||||
writer.append(buffer.getBuffer());
|
||||
writer.append("\n");
|
||||
}
|
||||
ddl.writeApply(writer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user