Refactor: BaseDdlBuffer uses StringBuilder instead of StringWriter. Makes IOException obsolete.

This commit is contained in:
Roland Praml committed 2022-02-08 16:04:14 +01:00
1 parent 7bea0d0eb2
commit 6e5eb6b963
20 files changed
+164 -210

No files matched your search

@@ -2,8 +2,6 @@ package io.ebeaninternal.dbmigration.ddlgeneration;
import io.ebeaninternal.dbmigration.model.MConfiguration; import io.ebeaninternal.dbmigration.model.MConfiguration;
import java.io.IOException;
/** /**
* Buffer to append generated DDL to. * Buffer to append generated DDL to.
*/ */
@@ -22,37 +20,37 @@ public interface DdlBuffer {
/** /**
* Append a statement allowing for null or empty statements. * Append a statement allowing for null or empty statements.
*/ */
DdlBuffer appendStatement(String content) throws IOException; DdlBuffer appendStatement(String content);
/** /**
* Append DDL content to the buffer. * Append DDL content to the buffer.
*/ */
DdlBuffer append(String content) throws IOException; DdlBuffer append(String content);
/** /**
* Append DDL content to the buffer with space padding. * Append DDL content to the buffer with space padding.
*/ */
DdlBuffer append(String type, int space) throws IOException; DdlBuffer append(String type, int space);
/** /**
* Append a value that is potentially null or empty and proceed it with a space if so. * Append a value that is potentially null or empty and proceed it with a space if so.
*/ */
DdlBuffer appendWithSpace(String foreignKeyRestrict) throws IOException; DdlBuffer appendWithSpace(String foreignKeyRestrict);
/** /**
* Append new line character to the buffer. * Append new line character to the buffer.
*/ */
DdlBuffer newLine() throws IOException; DdlBuffer newLine();
/** /**
* Append the end of statement content. * Append the end of statement content.
*/ */
DdlBuffer endOfStatement() throws IOException; DdlBuffer endOfStatement();
/** /**
* End of a change - add some whitespace. * End of a change - add some whitespace.
*/ */
DdlBuffer end() throws IOException; DdlBuffer end();
/** /**
* Return the buffer content. * Return the buffer content.
@@ -7,7 +7,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler; import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
import io.ebeaninternal.dbmigration.migration.AlterColumn; import io.ebeaninternal.dbmigration.migration.AlterColumn;
import java.io.IOException;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -52,31 +51,27 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
: (alter.getDefaultValue() != null ? alter.getDefaultValue() : alter.getCurrentDefaultValue()); : (alter.getDefaultValue() != null ? alter.getDefaultValue() : alter.getCurrentDefaultValue());
String defaultValueClause = (defaultValue == null || defaultValue.isEmpty()) ? "" : " default " + defaultValue; String defaultValueClause = (defaultValue == null || defaultValue.isEmpty()) ? "" : " default " + defaultValue;
try { DdlBuffer buffer = new BaseDdlBuffer(null);
DdlBuffer buffer = new BaseDdlBuffer(null); if (!isConvertible(currentType, type)) {
if (!isConvertible(currentType, type)) { // add an intermediate conversion if possible
// add an intermediate conversion if possible if (isNumberType(currentType)) {
if (isNumberType(currentType)) { // numbers can always be converted to decimal
// numbers can always be converted to decimal buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName)
buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName) .append(" decimal ").append(defaultValueClause).append(notnullClause).append(alterColumnSuffix)
.append(" decimal ").append(defaultValueClause).append(notnullClause).append(alterColumnSuffix) .endOfStatement();
.endOfStatement();
} else if (isStringType(currentType)) { } else if (isStringType(currentType)) {
// strings can always be converted to nclob // strings can always be converted to nclob
buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName) buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName)
.append(" nclob ").append(defaultValueClause).append(notnullClause).append(alterColumnSuffix) .append(" nclob ").append(defaultValueClause).append(notnullClause).append(alterColumnSuffix)
.endOfStatement(); .endOfStatement();
}
} }
buffer.append("alter table ").append(tableName).append(" ").append(alterColumn).append(" ").append(columnName)
.append(" ").append(type).append(defaultValueClause).append(notnullClause).append(alterColumnSuffix);
return buffer.getBuffer();
} catch (IOException e) {
throw new RuntimeException(e);
} }
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 @Override
@@ -121,19 +116,16 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
@Override @Override
public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) { public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) {
DdlBuffer buffer = new BaseDdlBuffer(null); DdlBuffer buffer = new BaseDdlBuffer(null);
try {
buffer.append("delimiter $$").newLine(); buffer.append("delimiter $$").newLine();
buffer.append("do").newLine(); buffer.append("do").newLine();
buffer.append("begin").newLine(); buffer.append("begin").newLine();
buffer.append("declare exit handler for sql_error_code 397 begin end").endOfStatement(); buffer.append("declare exit handler for sql_error_code 397 begin end").endOfStatement();
buffer.append("exec 'alter table ").append(tableName).append(" ").append(dropUniqueConstraint).append(" ") buffer.append("exec 'alter table ").append(tableName).append(" ").append(dropUniqueConstraint).append(" ")
.append(maxConstraintName(uniqueConstraintName)).append("'").endOfStatement(); .append(maxConstraintName(uniqueConstraintName)).append("'").endOfStatement();
buffer.append("end").endOfStatement(); buffer.append("end").endOfStatement();
buffer.append("$$"); buffer.append("$$");
return buffer.getBuffer(); return buffer.getBuffer();
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
@Override @Override
@@ -146,7 +138,7 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
* foreign keys. That's why we call a user stored procedure here * foreign keys. That's why we call a user stored procedure here
*/ */
@Override @Override
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) {
buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')") buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')")
.endOfStatement(); .endOfStatement();
} }
@@ -3,21 +3,19 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer; import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.model.MConfiguration; import io.ebeaninternal.dbmigration.model.MConfiguration;
import java.io.IOException;
import java.io.StringWriter;
/** /**
* Base implementation of DdlBuffer using an underlying writer. * Base implementation of DdlBuffer using an underlying writer.
*/ */
public class BaseDdlBuffer implements DdlBuffer { public class BaseDdlBuffer implements DdlBuffer {
protected final StringWriter writer; protected final StringBuilder writer;
protected final MConfiguration configuration; protected final MConfiguration configuration;
public BaseDdlBuffer(MConfiguration configuration) { public BaseDdlBuffer(MConfiguration configuration) {
this.configuration = configuration; this.configuration = configuration;
this.writer = new StringWriter(); this.writer = new StringBuilder();
} }
@Override @Override
@@ -27,19 +25,19 @@ public class BaseDdlBuffer implements DdlBuffer {
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return writer.getBuffer().length() == 0; return writer.length() == 0;
} }
@Override @Override
public DdlBuffer appendWithSpace(String foreignKeyRestrict) throws IOException { public DdlBuffer appendWithSpace(String content) {
if (foreignKeyRestrict != null && !foreignKeyRestrict.isEmpty()) { if (content != null && !content.isEmpty()) {
writer.append(" ").append(foreignKeyRestrict); writer.append(" ").append(content);
} }
return this; return this;
} }
@Override @Override
public DdlBuffer appendStatement(String content) throws IOException { public DdlBuffer appendStatement(String content) {
if (content != null && !content.isEmpty()) { if (content != null && !content.isEmpty()) {
writer.append(content); writer.append(content);
endOfStatement(); endOfStatement();
@@ -48,19 +46,19 @@ public class BaseDdlBuffer implements DdlBuffer {
} }
@Override @Override
public DdlBuffer append(String content) throws IOException { public DdlBuffer append(String content) {
writer.append(content); writer.append(content);
return this; return this;
} }
@Override @Override
public DdlBuffer append(String content, int space) throws IOException { public DdlBuffer append(String content, int space) {
writer.append(content); writer.append(content);
appendSpace(space, content); appendSpace(space, content);
return this; return this;
} }
protected void appendSpace(int max, String content) throws IOException { protected void appendSpace(int max, String content) {
int space = max - content.length(); int space = max - content.length();
if (space > 0) { if (space > 0) {
for (int i = 0; i < space; i++) { for (int i = 0; i < space; i++) {
@@ -71,7 +69,7 @@ public class BaseDdlBuffer implements DdlBuffer {
} }
@Override @Override
public DdlBuffer endOfStatement() throws IOException { public DdlBuffer endOfStatement() {
writer.append(";\n"); writer.append(";\n");
return this; return this;
} }
@@ -81,7 +79,7 @@ public class BaseDdlBuffer implements DdlBuffer {
* This should be just whitespace or a sql comment. * This should be just whitespace or a sql comment.
*/ */
@Override @Override
public DdlBuffer end() throws IOException { public DdlBuffer end() {
if (!isEmpty()) { if (!isEmpty()) {
writer.append("\n"); writer.append("\n");
} }
@@ -89,7 +87,7 @@ public class BaseDdlBuffer implements DdlBuffer {
} }
@Override @Override
public DdlBuffer newLine() throws IOException { public DdlBuffer newLine() {
writer.append("\n"); writer.append("\n");
return this; return this;
} }
@@ -6,8 +6,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler; import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions; import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
import java.io.IOException;
public class ClickHouseDdl extends PlatformDdl { public class ClickHouseDdl extends PlatformDdl {
private static final String LOG_TABLE = "ENGINE = Log()"; private static final String LOG_TABLE = "ENGINE = Log()";
@@ -32,7 +30,7 @@ public class ClickHouseDdl extends PlatformDdl {
* Add an table storage engine to the create table statement. * Add an table storage engine to the create table statement.
*/ */
@Override @Override
public void tableStorageEngine(DdlBuffer apply, String storageEngine) throws IOException { public void tableStorageEngine(DdlBuffer apply, String storageEngine) {
if (storageEngine == null) { if (storageEngine == null) {
// default to Log() table but really should all be explicit (need arguments for MergeTree etc) // default to Log() table but really should all be explicit (need arguments for MergeTree etc)
storageEngine = LOG_TABLE; storageEngine = LOG_TABLE;
@@ -9,8 +9,6 @@ import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
import io.ebeaninternal.dbmigration.model.MColumn; import io.ebeaninternal.dbmigration.model.MColumn;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
import java.util.Collection;
import java.util.List; import java.util.List;
/** /**
@@ -49,7 +47,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void updateTriggers(DdlWrite writer, HistoryTableUpdate update) throws IOException { public void updateTriggers(DdlWrite writer, HistoryTableUpdate update) {
MTable table = writer.getTable(update.getBaseTable()); MTable table = writer.getTable(update.getBaseTable());
if (table == null) { if (table == null) {
@@ -61,13 +59,13 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
/** /**
* Replace the existing triggers/stored procedures/views for history table support given the included columns. * Replace the existing triggers/stored procedures/views for history table support given the included columns.
*/ */
protected abstract void updateHistoryTriggers(DbTriggerUpdate triggerUpdate) throws IOException; protected abstract void updateHistoryTriggers(DbTriggerUpdate triggerUpdate);
/** /**
* Process the HistoryTableUpdate which can result in changes to the apply, rollback * Process the HistoryTableUpdate which can result in changes to the apply, rollback
* and drop scripts. * and drop scripts.
*/ */
protected void updateTriggers(DdlWrite writer, MTable table, HistoryTableUpdate update) throws IOException { protected void updateTriggers(DdlWrite writer, MTable table, HistoryTableUpdate update) {
writer.applyHistoryTrigger().append("-- changes: ").append(update.description()).newLine(); writer.applyHistoryTrigger().append("-- changes: ").append(update.description()).newLine();
@@ -83,7 +81,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException { public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
String baseTable = dropHistoryTable.getBaseTable(); String baseTable = dropHistoryTable.getBaseTable();
@@ -93,7 +91,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException { public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
String baseTable = addHistoryTable.getBaseTable(); String baseTable = addHistoryTable.getBaseTable();
MTable table = writer.getTable(baseTable); MTable table = writer.getTable(baseTable);
@@ -105,7 +103,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void createWithHistory(DdlWrite writer, MTable table) throws IOException { public void createWithHistory(DdlWrite writer, MTable table) {
String baseTable = table.getName(); String baseTable = table.getName();
String whenCreatedColumn = table.getWhenCreatedColumn(); String whenCreatedColumn = table.getWhenCreatedColumn();
@@ -118,11 +116,11 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
createTriggers(writer, table); createTriggers(writer, table);
} }
protected abstract void createTriggers(DdlWrite writer, MTable table) throws IOException; protected abstract void createTriggers(DdlWrite writer, MTable table);
protected abstract void dropTriggers(DdlBuffer buffer, String baseTable) throws IOException; protected abstract void dropTriggers(DdlBuffer buffer, String baseTable);
protected void createStoredFunction(DdlWrite writer, MTable table) throws IOException { protected void createStoredFunction(DdlWrite writer, MTable table) {
// do nothing // do nothing
} }
@@ -150,7 +148,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
return normalise(baseTableName) + "_history_del"; return normalise(baseTableName) + "_history_del";
} }
protected void addHistoryTable(DdlWrite writer, MTable table, String whenCreatedColumn) throws IOException { protected void addHistoryTable(DdlWrite writer, MTable table, String whenCreatedColumn) {
String baseTableName = table.getName(); String baseTableName = table.getName();
@@ -161,7 +159,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
createWithHistoryView(apply, baseTableName); createWithHistoryView(apply, baseTableName);
} }
protected void addSysPeriodColumns(DdlBuffer apply, String baseTableName, String whenCreatedColumn) throws IOException { protected void addSysPeriodColumns(DdlBuffer apply, String baseTableName, String whenCreatedColumn) {
apply.append("alter table ").append(baseTableName).append(" add column ") apply.append("alter table ").append(baseTableName).append(" add column ")
.append(sysPeriodStart).append(" ").append(sysPeriodType).append(" default ").append(now).endOfStatement(); .append(sysPeriodStart).append(" ").append(sysPeriodType).append(" default ").append(now).endOfStatement();
@@ -173,12 +171,12 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
} }
} }
protected void createHistoryTable(DdlBuffer apply, MTable table) throws IOException { protected void createHistoryTable(DdlBuffer apply, MTable table) {
createHistoryTableAs(apply, table); createHistoryTableAs(apply, table);
createHistoryTableWithPeriod(apply); createHistoryTableWithPeriod(apply);
} }
protected void createHistoryTableAs(DdlBuffer apply, MTable table) throws IOException { protected void createHistoryTableAs(DdlBuffer apply, MTable table) {
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(table.getName()).append(historySuffix).append("(").newLine(); apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(table.getName()).append(historySuffix).append("(").newLine();
for (MColumn column : table.allColumns()) { for (MColumn column : table.allColumns()) {
if (!column.isDraftOnly()) { if (!column.isDraftOnly()) {
@@ -188,7 +186,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
} }
} }
protected void createHistoryTableWithPeriod(DdlBuffer apply) throws IOException { protected void createHistoryTableWithPeriod(DdlBuffer apply) {
writeColumnDefinition(apply, sysPeriodStart, sysPeriodType); writeColumnDefinition(apply, sysPeriodStart, sysPeriodType);
apply.append(",").newLine(); apply.append(",").newLine();
writeColumnDefinition(apply, sysPeriodEnd, sysPeriodType); writeColumnDefinition(apply, sysPeriodEnd, sysPeriodType);
@@ -198,7 +196,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
/** /**
* Write the column definition to the create table statement. * Write the column definition to the create table statement.
*/ */
protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type) throws IOException { protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type) {
String platformType = platformDdl.convert(type); String platformType = platformDdl.convert(type);
buffer.append(" "); buffer.append(" ");
@@ -206,7 +204,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
buffer.append(platformType); buffer.append(platformType);
} }
protected void createWithHistoryView(DdlBuffer apply, String baseTableName) throws IOException { protected void createWithHistoryView(DdlBuffer apply, String baseTableName) {
apply apply
.append("create view ").append(baseTableName).append(viewSuffix) .append("create view ").append(baseTableName).append(viewSuffix)
@@ -220,7 +218,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
* For postgres/h2/mysql we need to drop and recreate the view. Well, we could add columns to the end of the view * For postgres/h2/mysql we need to drop and recreate the view. Well, we could add columns to the end of the view
* but otherwise we need to drop and create it. * but otherwise we need to drop and create it.
*/ */
protected void recreateHistoryView(DbTriggerUpdate update) throws IOException { protected void recreateHistoryView(DbTriggerUpdate update) {
DdlBuffer buffer = update.dropDependencyBuffer(); DdlBuffer buffer = update.dropDependencyBuffer();
// we need to drop the view early/first before any changes to the tables etc // we need to drop the view early/first before any changes to the tables etc
@@ -230,24 +228,24 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
createWithHistoryView(update.historyViewBuffer(), update.getBaseTable()); createWithHistoryView(update.historyViewBuffer(), update.getBaseTable());
} }
protected void appendSysPeriodColumns(DdlBuffer apply, String prefix) throws IOException { protected void appendSysPeriodColumns(DdlBuffer apply, String prefix) {
appendColumnName(apply, prefix, sysPeriodStart); appendColumnName(apply, prefix, sysPeriodStart);
appendColumnName(apply, prefix, sysPeriodEnd); appendColumnName(apply, prefix, sysPeriodEnd);
} }
protected void dropHistoryTableEtc(DdlBuffer buffer, String baseTableName) throws IOException { protected void dropHistoryTableEtc(DdlBuffer buffer, String baseTableName) {
buffer.append("drop view ").append(baseTableName).append(viewSuffix).endOfStatement(); buffer.append("drop view ").append(baseTableName).append(viewSuffix).endOfStatement();
dropSysPeriodColumns(buffer, baseTableName); dropSysPeriodColumns(buffer, baseTableName);
buffer.append("drop table ").append(baseTableName).append(historySuffix).endOfStatement().end(); buffer.append("drop table ").append(baseTableName).append(historySuffix).endOfStatement().end();
} }
protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) throws IOException { protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) {
platformDdl.alterTableDropColumn(buffer, baseTableName, sysPeriodStart); platformDdl.alterTableDropColumn(buffer, baseTableName, sysPeriodStart);
platformDdl.alterTableDropColumn(buffer, baseTableName, sysPeriodEnd); platformDdl.alterTableDropColumn(buffer, baseTableName, sysPeriodEnd);
} }
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) throws IOException { protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) {
buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(","); buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
appendColumnNames(buffer, columns, ""); appendColumnNames(buffer, columns, "");
@@ -256,7 +254,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
buffer.append(");").newLine(); buffer.append(");").newLine();
} }
void appendColumnNames(DdlBuffer buffer, List<String> columns, String columnPrefix) throws IOException { void appendColumnNames(DdlBuffer buffer, List<String> columns, String columnPrefix) {
for (int i = 0; i < columns.size(); i++) { for (int i = 0; i < columns.size(); i++) {
if (i > 0) { if (i > 0) {
buffer.append(", "); buffer.append(", ");
@@ -269,7 +267,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
/** /**
* Append a single column to the buffer if it is not null. * Append a single column to the buffer if it is not null.
*/ */
void appendColumnName(DdlBuffer buffer, String prefix, String columnName) throws IOException { void appendColumnName(DdlBuffer buffer, String prefix, String columnName) {
if (columnName != null) { if (columnName != null) {
buffer.append(prefix).append(columnName); buffer.append(prefix).append(columnName);
} }
@@ -4,8 +4,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
/** /**
* H2 history support using DB triggers to maintain a history table. * H2 history support using DB triggers to maintain a history table.
*/ */
@@ -20,26 +18,26 @@ public class H2HistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void dropTriggers(DdlBuffer buffer, String baseTable) throws IOException { protected void dropTriggers(DdlBuffer buffer, String baseTable) {
buffer.append("drop trigger ").append(updateTriggerName(baseTable)).endOfStatement(); buffer.append("drop trigger ").append(updateTriggerName(baseTable)).endOfStatement();
} }
@Override @Override
protected void createTriggers(DdlWrite writer, MTable table) throws IOException { protected void createTriggers(DdlWrite writer, MTable table) {
String baseTableName = table.getName(); String baseTableName = table.getName();
DdlBuffer apply = writer.applyHistoryTrigger(); DdlBuffer apply = writer.applyHistoryTrigger();
addCreateTrigger(apply, updateTriggerName(baseTableName), baseTableName); addCreateTrigger(apply, updateTriggerName(baseTableName), baseTableName);
} }
@Override @Override
protected void updateHistoryTriggers(DbTriggerUpdate update) throws IOException { protected void updateHistoryTriggers(DbTriggerUpdate update) {
recreateHistoryView(update); recreateHistoryView(update);
DdlBuffer buffer = update.historyTriggerBuffer(); DdlBuffer buffer = update.historyTriggerBuffer();
dropTriggers(buffer, update.getBaseTable()); dropTriggers(buffer, update.getBaseTable());
addCreateTrigger(buffer, updateTriggerName(update.getBaseTable()), update.getBaseTable()); addCreateTrigger(buffer, updateTriggerName(update.getBaseTable()), update.getBaseTable());
} }
private void addCreateTrigger(DdlBuffer apply, String triggerName, String baseTable) throws IOException { private void addCreateTrigger(DdlBuffer apply, String triggerName, String baseTable) {
// Note that this does not take into account the historyTable name (excepts _history suffix) and // Note that this does not take into account the historyTable name (excepts _history suffix) and
// does not take into account excluded columns (all columns included in history) // does not take into account excluded columns (all columns included in history)
apply apply
@@ -3,8 +3,6 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer; import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import java.io.IOException;
public class HanaColumnStoreDdl extends AbstractHanaDdl { public class HanaColumnStoreDdl extends AbstractHanaDdl {
public HanaColumnStoreDdl(DatabasePlatform platform) { public HanaColumnStoreDdl(DatabasePlatform platform) {
@@ -32,19 +30,15 @@ public class HanaColumnStoreDdl extends AbstractHanaDdl {
@Override @Override
public String dropIndex(String indexName, String tableName, boolean concurrent) { public String dropIndex(String indexName, String tableName, boolean concurrent) {
DdlBuffer buffer = new BaseDdlBuffer(null); DdlBuffer buffer = new BaseDdlBuffer(null);
try { buffer.append("delimiter $$").newLine();
buffer.append("delimiter $$").newLine(); buffer.append("do").newLine();
buffer.append("do").newLine(); buffer.append("begin").newLine();
buffer.append("begin").newLine(); buffer.append("declare exit handler for sql_error_code 261 begin end").endOfStatement();
buffer.append("declare exit handler for sql_error_code 261 begin end").endOfStatement(); buffer.append("exec '").append(dropIndexIfExists).append(maxConstraintName(indexName)).append("'")
buffer.append("exec '").append(dropIndexIfExists).append(maxConstraintName(indexName)).append("'") .endOfStatement();
.endOfStatement(); buffer.append("end").endOfStatement();
buffer.append("end").endOfStatement(); buffer.append("$$");
buffer.append("$$"); return buffer.getBuffer();
return buffer.getBuffer();
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
} }
@@ -8,7 +8,6 @@ import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
import io.ebeaninternal.dbmigration.model.MColumn; import io.ebeaninternal.dbmigration.model.MColumn;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -32,7 +31,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void createWithHistory(DdlWrite writer, MTable table) throws IOException { public void createWithHistory(DdlWrite writer, MTable table) {
String tableName = table.getName(); String tableName = table.getName();
String historyTableName = tableName + historySuffix; String historyTableName = tableName + historySuffix;
DdlBuffer apply = writer.applyHistoryView(); DdlBuffer apply = writer.applyHistoryView();
@@ -73,12 +72,12 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException { public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
dropHistoryTable(writer.applyDropDependencies(), dropHistoryTable.getBaseTable(), dropHistoryTable(writer.applyDropDependencies(), dropHistoryTable.getBaseTable(),
dropHistoryTable.getBaseTable() + historySuffix); dropHistoryTable.getBaseTable() + historySuffix);
} }
protected void dropHistoryTable(DdlBuffer apply, String baseTable, String historyTable) throws IOException { protected void dropHistoryTable(DdlBuffer apply, String baseTable, String historyTable) {
// disable system versioning // disable system versioning
disableSystemVersioning(apply, baseTable); disableSystemVersioning(apply, baseTable);
@@ -93,7 +92,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException { public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
MTable table = writer.getTable(addHistoryTable.getBaseTable()); MTable table = writer.getTable(addHistoryTable.getBaseTable());
if (table == null) { if (table == null) {
throw new IllegalStateException("MTable " + addHistoryTable.getBaseTable() + " not found in writer? (required for history DDL)"); throw new IllegalStateException("MTable " + addHistoryTable.getBaseTable() + " not found in writer? (required for history DDL)");
@@ -107,7 +106,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
} }
protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type, String defaultValue, protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type, String defaultValue,
boolean isNotNull, String generated) throws IOException { boolean isNotNull, String generated) {
String platformType = platformDdl.convert(type); String platformType = platformDdl.convert(type);
buffer.append(" ").append(platformDdl.lowerColumnName(columnName)); buffer.append(" ").append(platformDdl.lowerColumnName(columnName));
@@ -123,11 +122,11 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
} }
} }
public void disableSystemVersioning(DdlBuffer apply, String tableName) throws IOException { public void disableSystemVersioning(DdlBuffer apply, String tableName) {
disableSystemVersioning(apply, tableName, false); disableSystemVersioning(apply, tableName, false);
} }
public void disableSystemVersioning(DdlBuffer apply, String tableName, boolean uniqueStatement) throws IOException { public void disableSystemVersioning(DdlBuffer apply, String tableName, boolean uniqueStatement) {
apply.append("alter table ").append(tableName).append(" drop system versioning"); apply.append("alter table ").append(tableName).append(" drop system versioning");
if (uniqueStatement) { if (uniqueStatement) {
// needed for the DB migration test to prevent the statement from being filtered // needed for the DB migration test to prevent the statement from being filtered
@@ -138,7 +137,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
} }
public void enableSystemVersioning(DdlBuffer apply, String tableName, String historyTableName, boolean validated, public void enableSystemVersioning(DdlBuffer apply, String tableName, String historyTableName, boolean validated,
boolean uniqueStatement) throws IOException { boolean uniqueStatement) {
apply.append("alter table ").append(tableName).append(" add system versioning history table ").append(historyTableName); apply.append("alter table ").append(tableName).append(" add system versioning history table ").append(historyTableName);
if (!validated) { if (!validated) {
apply.append(" not validated"); apply.append(" not validated");
@@ -7,8 +7,6 @@ import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
import io.ebeaninternal.dbmigration.migration.DropHistoryTable; import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
/** /**
* History DDL for MariaDB. * History DDL for MariaDB.
*/ */
@@ -20,12 +18,12 @@ public class MariaDbHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void createWithHistory(DdlWrite writer, MTable table) throws IOException { public void createWithHistory(DdlWrite writer, MTable table) {
String baseTable = table.getName(); String baseTable = table.getName();
enableSystemVersioning(writer, baseTable); enableSystemVersioning(writer, baseTable);
} }
private void enableSystemVersioning(DdlWrite writer, String baseTable) throws IOException { private void enableSystemVersioning(DdlWrite writer, String baseTable) {
DdlBuffer apply = writer.applyHistoryView(); DdlBuffer apply = writer.applyHistoryView();
apply.append("alter table ").append(baseTable).append(" add system versioning").endOfStatement(); apply.append("alter table ").append(baseTable).append(" add system versioning").endOfStatement();
@@ -34,14 +32,14 @@ public class MariaDbHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException { public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
String baseTable = dropHistoryTable.getBaseTable(); String baseTable = dropHistoryTable.getBaseTable();
DdlBuffer apply = writer.applyHistoryView(); DdlBuffer apply = writer.applyHistoryView();
apply.append("alter table ").append(baseTable).append(" drop system versioning").endOfStatement(); apply.append("alter table ").append(baseTable).append(" drop system versioning").endOfStatement();
} }
@Override @Override
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException { public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
String baseTable = addHistoryTable.getBaseTable(); String baseTable = addHistoryTable.getBaseTable();
enableSystemVersioning(writer, baseTable); enableSystemVersioning(writer, baseTable);
} }
@@ -6,7 +6,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.migration.AlterColumn; import io.ebeaninternal.dbmigration.migration.AlterColumn;
import io.ebeaninternal.dbmigration.migration.Column; import io.ebeaninternal.dbmigration.migration.Column;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
/** /**
@@ -38,7 +37,7 @@ public class MySqlDdl extends PlatformDdl {
} }
@Override @Override
public void alterTableDropColumn(final DdlBuffer buffer, final String tableName, final String columnName) throws IOException { public void alterTableDropColumn(final DdlBuffer buffer, final String tableName, final String columnName) {
if (this.useMigrationStoredProcedures) { if (this.useMigrationStoredProcedures) {
buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement(); buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement();
} else { } else {
@@ -127,7 +126,7 @@ public class MySqlDdl extends PlatformDdl {
} }
@Override @Override
protected void writeColumnDefinition(DdlBuffer buffer, Column column, DdlIdentity identity) throws IOException { protected void writeColumnDefinition(DdlBuffer buffer, Column column, DdlIdentity identity) {
super.writeColumnDefinition(buffer, column, identity); super.writeColumnDefinition(buffer, column, identity);
String comment = column.getComment(); String comment = column.getComment();
if (!StringHelper.isNull(comment)) { if (!StringHelper.isNull(comment)) {
@@ -140,7 +139,7 @@ public class MySqlDdl extends PlatformDdl {
} }
@Override @Override
public void inlineTableComment(DdlBuffer apply, String tableComment) throws IOException { public void inlineTableComment(DdlBuffer apply, String tableComment) {
if (tableComment.length() > 1000) { if (tableComment.length() > 1000) {
tableComment = tableComment.substring(0, 1000); tableComment = tableComment.substring(0, 1000);
} }
@@ -151,7 +150,7 @@ public class MySqlDdl extends PlatformDdl {
* Add table comment as a separate statement (from the create table statement). * Add table comment as a separate statement (from the create table statement).
*/ */
@Override @Override
public void addTableComment(DdlBuffer apply, String tableName, String tableComment) throws IOException { public void addTableComment(DdlBuffer apply, String tableName, String tableComment) {
if (DdlHelp.isDropComment(tableComment)) { if (DdlHelp.isDropComment(tableComment)) {
tableComment = ""; tableComment = "";
} }
@@ -168,7 +167,7 @@ public class MySqlDdl extends PlatformDdl {
* Locks all tables for triggers that have to be updated. * Locks all tables for triggers that have to be updated.
*/ */
@Override @Override
public void lockTables(DdlBuffer buffer, Collection<String> tables) throws IOException { public void lockTables(DdlBuffer buffer, Collection<String> tables) {
if (!tables.isEmpty()) { if (!tables.isEmpty()) {
buffer.append("lock tables "); buffer.append("lock tables ");
int i = 0; int i = 0;
@@ -187,7 +186,7 @@ public class MySqlDdl extends PlatformDdl {
* Unlocks all tables for triggers that have to be updated. * Unlocks all tables for triggers that have to be updated.
*/ */
@Override @Override
public void unlockTables(DdlBuffer buffer, Collection<String> tables) throws IOException { public void unlockTables(DdlBuffer buffer, Collection<String> tables) {
buffer.append("unlock tables").endOfStatement(); buffer.append("unlock tables").endOfStatement();
} }
@@ -4,8 +4,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
/** /**
* MySql history support using DB triggers to maintain a history table. * MySql history support using DB triggers to maintain a history table.
*/ */
@@ -15,13 +13,13 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void dropTriggers(DdlBuffer buffer, String baseTable) throws IOException { protected void dropTriggers(DdlBuffer buffer, String baseTable) {
buffer.append("drop trigger ").append(updateTriggerName(baseTable)).endOfStatement(); buffer.append("drop trigger ").append(updateTriggerName(baseTable)).endOfStatement();
buffer.append("drop trigger ").append(deleteTriggerName(baseTable)).endOfStatement(); buffer.append("drop trigger ").append(deleteTriggerName(baseTable)).endOfStatement();
} }
@Override @Override
protected void createTriggers(DdlWrite writer, MTable table) throws IOException { protected void createTriggers(DdlWrite writer, MTable table) {
DbTriggerUpdate update = createDbTriggerUpdate(writer, table); DbTriggerUpdate update = createDbTriggerUpdate(writer, table);
@@ -30,7 +28,7 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void updateHistoryTriggers(DbTriggerUpdate update) throws IOException { protected void updateHistoryTriggers(DbTriggerUpdate update) {
recreateHistoryView(update); recreateHistoryView(update);
@@ -42,7 +40,7 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
addBeforeDelete(deleteTriggerName(baseTable), update); addBeforeDelete(deleteTriggerName(baseTable), update);
} }
private void addBeforeUpdate(String triggerName, DbTriggerUpdate update) throws IOException { private void addBeforeUpdate(String triggerName, DbTriggerUpdate update) {
DdlBuffer apply = update.historyTriggerBuffer(); DdlBuffer apply = update.historyTriggerBuffer();
apply apply
@@ -55,7 +53,7 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
.append("end$$").newLine(); .append("end$$").newLine();
} }
private void addBeforeDelete(String triggerName, DbTriggerUpdate update) throws IOException { private void addBeforeDelete(String triggerName, DbTriggerUpdate update) {
DdlBuffer apply = update.historyTriggerBuffer(); DdlBuffer apply = update.historyTriggerBuffer();
apply apply
@@ -6,8 +6,6 @@ import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
import io.ebeaninternal.dbmigration.migration.DropHistoryTable; import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
/** /**
* Default history implementation that does nothing. Needs to be replaced * Default history implementation that does nothing. Needs to be replaced
* with an appropriate implementation for the given database platform. * with an appropriate implementation for the given database platform.
@@ -20,7 +18,7 @@ public class NoHistorySupportDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void createWithHistory(DdlWrite writer, MTable table) throws IOException { public void createWithHistory(DdlWrite writer, MTable table) {
// does nothing // does nothing
} }
@@ -30,7 +28,7 @@ public class NoHistorySupportDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException { public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
// does nothing // does nothing
} }
@@ -4,8 +4,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
/** /**
* NuoDB history support using DB triggers to maintain a history table. * NuoDB history support using DB triggers to maintain a history table.
*/ */
@@ -17,14 +15,14 @@ public class NuoDbHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void dropTriggers(DdlBuffer buffer, String baseTable) throws IOException { protected void dropTriggers(DdlBuffer buffer, String baseTable) {
buffer.append("drop trigger ").append(updateTriggerName(baseTable)).endOfStatement(); buffer.append("drop trigger ").append(updateTriggerName(baseTable)).endOfStatement();
buffer.append("drop trigger ").append(deleteTriggerName(baseTable)).endOfStatement(); buffer.append("drop trigger ").append(deleteTriggerName(baseTable)).endOfStatement();
} }
@Override @Override
protected void createTriggers(DdlWrite writer, MTable table) throws IOException { protected void createTriggers(DdlWrite writer, MTable table) {
DbTriggerUpdate update = createDbTriggerUpdate(writer, table); DbTriggerUpdate update = createDbTriggerUpdate(writer, table);
@@ -33,7 +31,7 @@ public class NuoDbHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void updateHistoryTriggers(DbTriggerUpdate update) throws IOException { protected void updateHistoryTriggers(DbTriggerUpdate update) {
recreateHistoryView(update); recreateHistoryView(update);
@@ -45,7 +43,7 @@ public class NuoDbHistoryDdl extends DbTriggerBasedHistoryDdl {
addBeforeDelete(deleteTriggerName(baseTable), update); addBeforeDelete(deleteTriggerName(baseTable), update);
} }
private void addBeforeUpdate(String triggerName, DbTriggerUpdate update) throws IOException { private void addBeforeUpdate(String triggerName, DbTriggerUpdate update) {
DdlBuffer apply = update.historyTriggerBuffer(); DdlBuffer apply = update.historyTriggerBuffer();
addTriggerStart(triggerName, update, apply, " before update for each row as "); addTriggerStart(triggerName, update, apply, " before update for each row as ");
@@ -54,7 +52,7 @@ public class NuoDbHistoryDdl extends DbTriggerBasedHistoryDdl {
addEndTrigger(apply); addEndTrigger(apply);
} }
private void addBeforeDelete(String triggerName, DbTriggerUpdate update) throws IOException { private void addBeforeDelete(String triggerName, DbTriggerUpdate update) {
DdlBuffer apply = update.historyTriggerBuffer(); DdlBuffer apply = update.historyTriggerBuffer();
addTriggerStart(triggerName, update, apply, " before delete for each row as"); addTriggerStart(triggerName, update, apply, " before delete for each row as");
@@ -62,14 +60,14 @@ public class NuoDbHistoryDdl extends DbTriggerBasedHistoryDdl {
addEndTrigger(apply); addEndTrigger(apply);
} }
private void addTriggerStart(String triggerName, DbTriggerUpdate update, DdlBuffer apply, String s) throws IOException { private void addTriggerStart(String triggerName, DbTriggerUpdate update, DdlBuffer apply, String s) {
apply apply
.append("delimiter $$").newLine() .append("delimiter $$").newLine()
.append("create or replace trigger ").append(triggerName).append(" for ").append(update.getBaseTable()) .append("create or replace trigger ").append(triggerName).append(" for ").append(update.getBaseTable())
.append(s).newLine(); .append(s).newLine();
} }
private void addEndTrigger(DdlBuffer apply) throws IOException { private void addEndTrigger(DdlBuffer apply) {
apply.append("end_trigger") apply.append("end_trigger")
.endOfStatement() .endOfStatement()
.append("$$").newLine() .append("$$").newLine()
@@ -21,7 +21,6 @@ import io.ebeaninternal.dbmigration.migration.Column;
import io.ebeaninternal.dbmigration.migration.DropHistoryTable; import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@@ -234,7 +233,7 @@ public class PlatformDdl {
/** /**
* Write all the table columns converting to platform types as necessary. * Write all the table columns converting to platform types as necessary.
*/ */
public void writeTableColumns(DdlBuffer apply, List<Column> columns, DdlIdentity identity) throws IOException { public void writeTableColumns(DdlBuffer apply, List<Column> columns, DdlIdentity identity) {
for (int i = 0; i < columns.size(); i++) { for (int i = 0; i < columns.size(); i++) {
if (i > 0) { if (i > 0) {
apply.append(","); apply.append(",");
@@ -258,7 +257,7 @@ public class PlatformDdl {
/** /**
* Write the column definition to the create table statement. * Write the column definition to the create table statement.
*/ */
protected void writeColumnDefinition(DdlBuffer buffer, Column column, DdlIdentity identity) throws IOException { protected void writeColumnDefinition(DdlBuffer buffer, Column column, DdlIdentity identity) {
String columnDefn = convert(column.getType()); String columnDefn = convert(column.getType());
if (identity.useIdentity() && isTrue(column.isPrimaryKey())) { if (identity.useIdentity() && isTrue(column.isPrimaryKey())) {
@@ -285,7 +284,7 @@ public class PlatformDdl {
/** /**
* Allow for platform overriding (e.g. ClickHouse). * Allow for platform overriding (e.g. ClickHouse).
*/ */
protected void writeColumnNotNull(DdlBuffer buffer) throws IOException { protected void writeColumnNotNull(DdlBuffer buffer) {
buffer.append(" not null"); buffer.append(" not null");
} }
@@ -337,28 +336,28 @@ public class PlatformDdl {
/** /**
* Add history support to this table using the platform specific mechanism. * Add history support to this table using the platform specific mechanism.
*/ */
public void createWithHistory(DdlWrite writer, MTable table) throws IOException { public void createWithHistory(DdlWrite writer, MTable table) {
historyDdl.createWithHistory(writer, table); historyDdl.createWithHistory(writer, table);
} }
/** /**
* Drop history support for a given table. * Drop history support for a given table.
*/ */
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException { public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
historyDdl.dropHistoryTable(writer, dropHistoryTable); historyDdl.dropHistoryTable(writer, dropHistoryTable);
} }
/** /**
* Add history support to an existing table. * Add history support to an existing table.
*/ */
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException { public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
historyDdl.addHistoryTable(writer, addHistoryTable); historyDdl.addHistoryTable(writer, addHistoryTable);
} }
/** /**
* Regenerate the history triggers (or function) due to a column being added/dropped/excluded or included. * Regenerate the history triggers (or function) due to a column being added/dropped/excluded or included.
*/ */
public void regenerateHistoryTriggers(DdlWrite write, HistoryTableUpdate update) throws IOException { public void regenerateHistoryTriggers(DdlWrite write, HistoryTableUpdate update) {
historyDdl.updateTriggers(write, update); historyDdl.updateTriggers(write, update);
} }
@@ -521,7 +520,7 @@ public class PlatformDdl {
return buffer.toString(); return buffer.toString();
} }
public void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column, boolean onHistoryTable, String defaultValue) throws IOException { public void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column, boolean onHistoryTable, String defaultValue) {
String convertedType = convert(column.getType()); String convertedType = convert(column.getType());
@@ -558,7 +557,7 @@ public class PlatformDdl {
} }
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) {
buffer.append("alter table ").append(tableName).append(" ").append(dropColumn).append(" ").append(columnName) buffer.append("alter table ").append(tableName).append(" ").append(dropColumn).append(" ").append(columnName)
.append(dropColumnSuffix).endOfStatement(); .append(dropColumnSuffix).endOfStatement();
} }
@@ -676,21 +675,21 @@ public class PlatformDdl {
/** /**
* Add an inline table comment to the create table statement. * Add an inline table comment to the create table statement.
*/ */
public void inlineTableComment(DdlBuffer apply, String tableComment) throws IOException { public void inlineTableComment(DdlBuffer apply, String tableComment) {
// do nothing by default (MySql only) // do nothing by default (MySql only)
} }
/** /**
* Add an table storage engine to the create table statement. * Add an table storage engine to the create table statement.
*/ */
public void tableStorageEngine(DdlBuffer apply, String storageEngine) throws IOException { public void tableStorageEngine(DdlBuffer apply, String storageEngine) {
// do nothing by default // do nothing by default
} }
/** /**
* Add table comment as a separate statement (from the create table statement). * Add table comment as a separate statement (from the create table statement).
*/ */
public void addTableComment(DdlBuffer apply, String tableName, String tableComment) throws IOException { public void addTableComment(DdlBuffer apply, String tableName, String tableComment) {
if (DdlHelp.isDropComment(tableComment)) { if (DdlHelp.isDropComment(tableComment)) {
tableComment = ""; tableComment = "";
} }
@@ -700,7 +699,7 @@ public class PlatformDdl {
/** /**
* Add column comment as a separate statement. * Add column comment as a separate statement.
*/ */
public void addColumnComment(DdlBuffer apply, String table, String column, String comment) throws IOException { public void addColumnComment(DdlBuffer apply, String table, String column, String comment) {
if (DdlHelp.isDropComment(comment)) { if (DdlHelp.isDropComment(comment)) {
comment = ""; comment = "";
} }
@@ -710,14 +709,14 @@ public class PlatformDdl {
/** /**
* Use this to generate a prolog for each script (stored procedures) * Use this to generate a prolog for each script (stored procedures)
*/ */
public void generateProlog(DdlWrite write) throws IOException { public void generateProlog(DdlWrite write) {
} }
/** /**
* Use this to generate an epilog. Will be added at the end of script * Use this to generate an epilog. Will be added at the end of script
*/ */
public void generateEpilog(DdlWrite write) throws IOException { public void generateEpilog(DdlWrite write) {
} }
@@ -743,14 +742,14 @@ public class PlatformDdl {
/** /**
* Mysql-specific: Locks all tables for triggers that have to be updated. * Mysql-specific: Locks all tables for triggers that have to be updated.
*/ */
public void lockTables(DdlBuffer buffer, Collection<String> tables) throws IOException { public void lockTables(DdlBuffer buffer, Collection<String> tables) {
} }
/** /**
* Mysql-specific: Unlocks all tables for triggers that have to be updated. * Mysql-specific: Unlocks all tables for triggers that have to be updated.
*/ */
public void unlockTables(DdlBuffer buffer, Collection<String> tables) throws IOException { public void unlockTables(DdlBuffer buffer, Collection<String> tables) {
} }
@@ -769,7 +768,7 @@ public class PlatformDdl {
return false; return false;
} }
public void addTablePartition(DdlBuffer apply, String partitionMode, String partitionColumn) throws IOException { public void addTablePartition(DdlBuffer apply, String partitionMode, String partitionColumn) {
// only supported by postgres initially // only supported by postgres initially
} }
} }
@@ -6,8 +6,6 @@ import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
import io.ebeaninternal.dbmigration.migration.DropHistoryTable; import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
/** /**
* Defines the implementation for adding history support to a table. * Defines the implementation for adding history support to a table.
*/ */
@@ -21,20 +19,20 @@ public interface PlatformHistoryDdl {
/** /**
* Creates a new table and add history support to the table using platform specific mechanism. * Creates a new table and add history support to the table using platform specific mechanism.
*/ */
void createWithHistory(DdlWrite writer, MTable table) throws IOException; void createWithHistory(DdlWrite writer, MTable table);
/** /**
* Drop history support for the given table. * Drop history support for the given table.
*/ */
void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException; void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable);
/** /**
* Add history support to the given table. * Add history support to the given table.
*/ */
void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException; void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable);
/** /**
* Regenerate the history triggers/stored function due to column added/dropped/included or excluded. * Regenerate the history triggers/stored function due to column added/dropped/included or excluded.
*/ */
void updateTriggers(DdlWrite write, HistoryTableUpdate baseTable) throws IOException; void updateTriggers(DdlWrite write, HistoryTableUpdate baseTable);
} }
@@ -3,8 +3,6 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer; import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import java.io.IOException;
/** /**
* Postgres specific DDL. * Postgres specific DDL.
*/ */
@@ -40,7 +38,7 @@ public class PostgresDdl extends PlatformDdl {
} }
@Override @Override
public void addTablePartition(DdlBuffer apply, String partitionMode, String partitionColumn) throws IOException { public void addTablePartition(DdlBuffer apply, String partitionMode, String partitionColumn) {
apply.append(" partition by range (").append(partitionColumn).append(")"); apply.append(" partition by range (").append(partitionColumn).append(")");
} }
@@ -4,7 +4,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
@@ -21,7 +20,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
* Use Postgres create table like to create the history table. * Use Postgres create table like to create the history table.
*/ */
@Override @Override
protected void createHistoryTable(DdlBuffer apply, MTable table) throws IOException { protected void createHistoryTable(DdlBuffer apply, MTable table) {
apply.append("create table ").append(table.getName()).append(historySuffix) apply.append("create table ").append(table.getName()).append(historySuffix)
.append("(like ").append(table.getName()).append(")").endOfStatement(); .append("(like ").append(table.getName()).append(")").endOfStatement();
} }
@@ -30,7 +29,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
* Use Postgres range type rather than start and end timestamps. * Use Postgres range type rather than start and end timestamps.
*/ */
@Override @Override
protected void addSysPeriodColumns(DdlBuffer apply, String baseTableName, String whenCreatedColumn) throws IOException { protected void addSysPeriodColumns(DdlBuffer apply, String baseTableName, String whenCreatedColumn) {
apply apply
.append("alter table ").append(baseTableName) .append("alter table ").append(baseTableName)
.append(" add column ").append(sysPeriod).append(" tstzrange not null default tstzrange(").append(now).append(", null)") .append(" add column ").append(sysPeriod).append(" tstzrange not null default tstzrange(").append(now).append(", null)")
@@ -43,17 +42,17 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void appendSysPeriodColumns(DdlBuffer apply, String prefix) throws IOException { protected void appendSysPeriodColumns(DdlBuffer apply, String prefix) {
appendColumnName(apply, prefix, sysPeriod); appendColumnName(apply, prefix, sysPeriod);
} }
@Override @Override
protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) throws IOException { protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) {
buffer.append("alter table ").append(baseTableName).append(" drop column ").append(sysPeriod).endOfStatement(); buffer.append("alter table ").append(baseTableName).append(" drop column ").append(sysPeriod).endOfStatement();
} }
@Override @Override
protected void createTriggers(DdlWrite writer, MTable table) throws IOException { protected void createTriggers(DdlWrite writer, MTable table) {
String baseTableName = table.getName(); String baseTableName = table.getName();
String procedureName = procedureName(baseTableName); String procedureName = procedureName(baseTableName);
String triggerName = triggerName(baseTableName); String triggerName = triggerName(baseTableName);
@@ -66,14 +65,14 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void dropTriggers(DdlBuffer buffer, String baseTable) throws IOException { protected void dropTriggers(DdlBuffer buffer, String baseTable) {
// rollback trigger then function // rollback trigger then function
buffer.append("drop trigger if exists ").append(triggerName(baseTable)).append(" on ").append(baseTable).append(" cascade").endOfStatement(); buffer.append("drop trigger if exists ").append(triggerName(baseTable)).append(" on ").append(baseTable).append(" cascade").endOfStatement();
buffer.append("drop function if exists ").append(procedureName(baseTable)).append("()").endOfStatement(); buffer.append("drop function if exists ").append(procedureName(baseTable)).append("()").endOfStatement();
buffer.end(); buffer.end();
} }
protected void createOrReplaceFunction(DdlBuffer apply, String procedureName, String historyTable, List<String> includedColumns) throws IOException { protected void createOrReplaceFunction(DdlBuffer apply, String procedureName, String historyTable, List<String> includedColumns) {
apply apply
.append("create or replace function ").append(procedureName).append("() returns trigger as $$").newLine(); .append("create or replace function ").append(procedureName).append("() returns trigger as $$").newLine();
@@ -105,7 +104,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void createStoredFunction(DdlWrite writer, MTable table) throws IOException { protected void createStoredFunction(DdlWrite writer, MTable table) {
String procedureName = procedureName(table.getName()); String procedureName = procedureName(table.getName());
String historyTable = historyTableName(table.getName()); String historyTable = historyTableName(table.getName());
@@ -114,14 +113,14 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
} }
@Override @Override
protected void updateHistoryTriggers(DbTriggerUpdate update) throws IOException { protected void updateHistoryTriggers(DbTriggerUpdate update) {
String procedureName = procedureName(update.getBaseTable()); String procedureName = procedureName(update.getBaseTable());
recreateHistoryView(update); recreateHistoryView(update);
createOrReplaceFunction(update.historyTriggerBuffer(), procedureName, update.getHistoryTable(), update.getColumns()); createOrReplaceFunction(update.historyTriggerBuffer(), procedureName, update.getHistoryTable(), update.getColumns());
} }
@Override @Override
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) throws IOException { protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) {
buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriod).append(","); buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriod).append(",");
appendColumnNames(buffer, columns, ""); appendColumnNames(buffer, columns, "");
buffer.append(") values (tstzrange(lowerTs,upperTs), "); buffer.append(") values (tstzrange(lowerTs,upperTs), ");
@@ -6,8 +6,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.migration.AlterColumn; import io.ebeaninternal.dbmigration.migration.AlterColumn;
import java.io.IOException;
/** /**
* MS SQL Server platform specific DDL. * MS SQL Server platform specific DDL.
*/ */
@@ -185,7 +183,7 @@ public class SqlServerDdl extends PlatformDdl {
* Add table comment as a separate statement (from the create table statement). * Add table comment as a separate statement (from the create table statement).
*/ */
@Override @Override
public void addTableComment(DdlBuffer apply, String tableName, String tableComment) throws IOException { public void addTableComment(DdlBuffer apply, String tableName, String tableComment) {
// do nothing for MS SQL Server (cause it requires stored procedures etc) // do nothing for MS SQL Server (cause it requires stored procedures etc)
} }
@@ -194,7 +192,7 @@ public class SqlServerDdl extends PlatformDdl {
* Add column comment as a separate statement. * Add column comment as a separate statement.
*/ */
@Override @Override
public void addColumnComment(DdlBuffer apply, String table, String column, String comment) throws IOException { public void addColumnComment(DdlBuffer apply, String table, String column, String comment) {
// do nothing for MS SQL Server (cause it requires stored procedures etc) // do nothing for MS SQL Server (cause it requires stored procedures etc)
} }
@@ -204,7 +202,7 @@ public class SqlServerDdl extends PlatformDdl {
* (constraints, default values, indices and foreign keys). That's why we call a user stored procedure here * (constraints, default values, indices and foreign keys). That's why we call a user stored procedure here
*/ */
@Override @Override
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) {
buffer.append("EXEC usp_ebean_drop_column ").append(tableName).append(", ").append(columnName).endOfStatement(); buffer.append("EXEC usp_ebean_drop_column ").append(tableName).append(", ").append(columnName).endOfStatement();
} }
@@ -213,7 +211,7 @@ public class SqlServerDdl extends PlatformDdl {
* This writes the multi value datatypes needed for MultiValueBind. * This writes the multi value datatypes needed for MultiValueBind.
*/ */
@Override @Override
public void generateProlog(DdlWrite write) throws IOException { public void generateProlog(DdlWrite write) {
super.generateProlog(write); super.generateProlog(write);
generateTVPDefinitions(write, "bigint"); generateTVPDefinitions(write, "bigint");
@@ -227,7 +225,7 @@ public class SqlServerDdl extends PlatformDdl {
} }
private void generateTVPDefinitions(DdlWrite write, String definition) throws IOException { private void generateTVPDefinitions(DdlWrite write, String definition) {
int pos = definition.indexOf('('); int pos = definition.indexOf('(');
String name = pos == -1 ? definition : definition.substring(0, pos); String name = pos == -1 ? definition : definition.substring(0, pos);
@@ -236,12 +234,12 @@ public class SqlServerDdl extends PlatformDdl {
//createTVP(write.apply(), name, definition); //createTVP(write.apply(), name, definition);
} }
private void dropTVP(DdlBuffer ddl, String name) throws IOException { private void dropTVP(DdlBuffer ddl, String name) {
ddl.append("if exists (select name from sys.types where name = 'ebean_").append(name) ddl.append("if exists (select name from sys.types where name = 'ebean_").append(name)
.append("_tvp') drop type ebean_").append(name).append("_tvp").endOfStatement(); .append("_tvp') drop type ebean_").append(name).append("_tvp").endOfStatement();
} }
private void createTVP(DdlBuffer ddl, String name, String definition) throws IOException { private void createTVP(DdlBuffer ddl, String name, String definition) {
ddl.append("if not exists (select name from sys.types where name = 'ebean_").append(name) ddl.append("if not exists (select name from sys.types where name = 'ebean_").append(name)
.append("_tvp') create type ebean_").append(name).append("_tvp as table (c1 ").append(definition).append(")") .append("_tvp') create type ebean_").append(name).append("_tvp as table (c1 ").append(definition).append(")")
.endOfStatement(); .endOfStatement();
@@ -7,8 +7,6 @@ import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
import io.ebeaninternal.dbmigration.migration.DropHistoryTable; import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
/** /**
* @author Vilmos Nagy * @author Vilmos Nagy
*/ */
@@ -26,7 +24,7 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void createWithHistory(DdlWrite writer, MTable table) throws IOException { public void createWithHistory(DdlWrite writer, MTable table) {
String baseTable = table.getName(); String baseTable = table.getName();
enableSystemVersioning(writer, baseTable); enableSystemVersioning(writer, baseTable);
} }
@@ -43,7 +41,7 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
return historyTable; return historyTable;
} }
private void enableSystemVersioning(DdlWrite writer, String baseTable) throws IOException { private void enableSystemVersioning(DdlWrite writer, String baseTable) {
DdlBuffer apply = writer.applyHistoryView(); DdlBuffer apply = writer.applyHistoryView();
apply.append("alter table ").append(baseTable).newLine() apply.append("alter table ").append(baseTable).newLine()
.append(" add ").append(systemPeriodStart).append(" datetime2 GENERATED ALWAYS AS ROW START NOT NULL DEFAULT SYSUTCDATETIME(),").newLine() .append(" add ").append(systemPeriodStart).append(" datetime2 GENERATED ALWAYS AS ROW START NOT NULL DEFAULT SYSUTCDATETIME(),").newLine()
@@ -59,7 +57,7 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException { public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
String baseTable = dropHistoryTable.getBaseTable(); String baseTable = dropHistoryTable.getBaseTable();
DdlBuffer apply = writer.applyHistoryView(); DdlBuffer apply = writer.applyHistoryView();
apply.append("-- dropping history support for ").append(baseTable).endOfStatement(); apply.append("-- dropping history support for ").append(baseTable).endOfStatement();
@@ -78,13 +76,13 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
} }
@Override @Override
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) throws IOException { public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
String baseTable = addHistoryTable.getBaseTable(); String baseTable = addHistoryTable.getBaseTable();
enableSystemVersioning(writer, baseTable); enableSystemVersioning(writer, baseTable);
} }
@Override @Override
public void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable) throws IOException { public void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable) {
// SQL Server 2016 does not need triggers // SQL Server 2016 does not need triggers
DdlBuffer apply = writer.applyHistoryView(); DdlBuffer apply = writer.applyHistoryView();
String baseTableName = baseTable.getBaseTable(); String baseTableName = baseTable.getBaseTable();
@@ -3,12 +3,10 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer; import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.model.MTable; import io.ebeaninternal.dbmigration.model.MTable;
import java.io.IOException;
public class YugabyteHistoryDdl extends PostgresHistoryDdl { public class YugabyteHistoryDdl extends PostgresHistoryDdl {
@Override @Override
protected void createHistoryTable(DdlBuffer apply, MTable table) throws IOException { protected void createHistoryTable(DdlBuffer apply, MTable table) {
createHistoryTableAs(apply, table); createHistoryTableAs(apply, table);
writeColumnDefinition(apply, sysPeriod, "tstzrange"); writeColumnDefinition(apply, sysPeriod, "tstzrange");
apply.newLine().append(")").endOfStatement(); apply.newLine().append(")").endOfStatement();