mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Merge remote-tracking branch 'upstream/master'
# Conflicts: # ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java # ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/BaseTableDdl.java # ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/Db2HistoryDdl.java # ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/DbTriggerBasedHistoryDdl.java # ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/HanaHistoryDdl.java # ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl.java # ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PostgresHistoryDdl.java # ebean-test/src/test/resources/migrationtest/dbmigration/db2fori/1.1.sql # ebean-test/src/test/resources/migrationtest/dbmigration/db2fori/idx_db2.migrations # ebean-test/src/test/resources/migrationtest/dbmigration/db2legacy/1.1.sql # ebean-test/src/test/resources/migrationtest/dbmigration/db2legacy/idx_db2.migrations # ebean-test/src/test/resources/migrationtest/dbmigration/db2luw/1.1.sql # ebean-test/src/test/resources/migrationtest/dbmigration/db2luw/idx_db2.migrations # ebean-test/src/test/resources/migrationtest/dbmigration/db2zos/1.1.sql # ebean-test/src/test/resources/migrationtest/dbmigration/db2zos/idx_db2.migrations # ebean-test/src/test/resources/migrationtest/dbmigration/model/1.3.model.xml
This commit is contained in:
commit
bcec447193
141 files changed
+1478
-646
No files matched your search
+21
-19
@@ -1,18 +1,5 @@
|
||||
package io.ebeaninternal.dbmigration;
|
||||
|
||||
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.avaje.classpath.scanner.core.Location;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
@@ -53,15 +40,22 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.Migration;
|
||||
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlWriter;
|
||||
import io.ebeaninternal.dbmigration.model.CurrentModel;
|
||||
import io.ebeaninternal.dbmigration.model.MConfiguration;
|
||||
import io.ebeaninternal.dbmigration.model.MigrationModel;
|
||||
import io.ebeaninternal.dbmigration.model.ModelContainer;
|
||||
import io.ebeaninternal.dbmigration.model.ModelDiff;
|
||||
import io.ebeaninternal.dbmigration.model.PlatformDdlWriter;
|
||||
import io.ebeaninternal.dbmigration.model.*;
|
||||
import io.ebeaninternal.extraddl.model.DdlScript;
|
||||
import io.ebeaninternal.extraddl.model.ExtraDdl;
|
||||
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
|
||||
|
||||
/**
|
||||
* Generates DB Migration xml and sql scripts.
|
||||
@@ -686,6 +680,7 @@ public class DefaultDbMigration implements DbMigration {
|
||||
if (version == null) {
|
||||
version = (nextVersion != null) ? nextVersion : initialVersion;
|
||||
}
|
||||
checkDropVersion(version, dropsFor);
|
||||
|
||||
String fullVersion = applyPrefix + version;
|
||||
String name = name();
|
||||
@@ -701,6 +696,13 @@ public class DefaultDbMigration implements DbMigration {
|
||||
return fullVersion;
|
||||
}
|
||||
|
||||
void checkDropVersion(String version, String dropsFor) {
|
||||
if (dropsFor != null && dropsFor.equals(version)) {
|
||||
throw new IllegalArgumentException("The next migration version must not be the same as the pending drops version of " +
|
||||
dropsFor + ". Please make the next migration version higher than " + dropsFor + ".");
|
||||
}
|
||||
}
|
||||
|
||||
String trimDropsFor(String dropsFor) {
|
||||
if (dropsFor.startsWith("V") || dropsFor.startsWith("v")) {
|
||||
dropsFor = dropsFor.substring(1);
|
||||
|
||||
+1
-1
@@ -96,7 +96,7 @@ public class BaseDdlHandler implements DdlHandler {
|
||||
public void generate(DdlWrite writer, DropTable dropTable) {
|
||||
tableDdl.generate(writer, dropTable);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generate(DdlWrite writer, AlterTable alterTable) {
|
||||
tableDdl.generate(writer, alterTable);
|
||||
|
||||
+3
-3
@@ -194,16 +194,16 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
|
||||
@Override
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(lowerTableName(tableName), HanaAlterTableWrite::new);
|
||||
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 {
|
||||
class HanaAlterTableWrite extends BaseAlterTableWrite {
|
||||
|
||||
public HanaAlterTableWrite(String tableName) {
|
||||
super(tableName);
|
||||
super(tableName, AbstractHanaDdl.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
-3
@@ -17,6 +17,8 @@ public class BaseAlterTableWrite implements DdlAlterTable {
|
||||
|
||||
protected static final String RAW_OPERATION = "$RAW";
|
||||
|
||||
protected final PlatformDdl platformDdl;
|
||||
|
||||
public class AlterCmd {
|
||||
// the command (e.g. "alter", "modify"
|
||||
private final String operation;
|
||||
@@ -58,9 +60,9 @@ public class BaseAlterTableWrite implements DdlAlterTable {
|
||||
// of all alter commands
|
||||
target.append(getAlternation());
|
||||
} else {
|
||||
target.append("alter table ").append(tableName).append(' ').append(operation);
|
||||
target.append("alter table ").append(platformDdl.quote(tableName)).append(' ').append(operation);
|
||||
if (column != null) {
|
||||
target.append(' ').append(column);
|
||||
target.append(' ').append(platformDdl.quote(column));
|
||||
}
|
||||
if (!getAlternation().isEmpty()) {
|
||||
target.append(' ').append(getAlternation());
|
||||
@@ -85,8 +87,9 @@ public class BaseAlterTableWrite implements DdlAlterTable {
|
||||
|
||||
private boolean historyHandled;
|
||||
|
||||
public BaseAlterTableWrite(String tableName) {
|
||||
public BaseAlterTableWrite(String tableName, PlatformDdl platformDdl) {
|
||||
this.tableName = tableName;
|
||||
this.platformDdl = platformDdl;
|
||||
}
|
||||
|
||||
public String tableName() {
|
||||
|
||||
+9
-21
@@ -205,7 +205,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
public void generate(DdlWrite writer, CreateTable createTable) {
|
||||
reset();
|
||||
|
||||
String tableName = lowerTableName(createTable.getName());
|
||||
String tableName = createTable.getName();
|
||||
List<Column> columns = createTable.getColumn();
|
||||
List<Column> pk = determinePrimaryKeyColumns(columns);
|
||||
|
||||
@@ -223,7 +223,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
String partitionMode = createTable.getPartitionMode();
|
||||
|
||||
DdlBuffer apply = writer.apply();
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(lowerTableName(tableName)).append(" (");
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(platformDdl.quote(tableName)).append(" (");
|
||||
writeTableColumns(apply, columns, identity);
|
||||
writeUniqueConstraints(apply, createTable);
|
||||
writeCompoundUniqueConstraints(apply, createTable);
|
||||
@@ -402,7 +402,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
|
||||
protected void writeForeignKey(DdlWrite writer, WriteForeignKey request) {
|
||||
DdlBuffer fkeyBuffer = writer.applyForeignKeys();
|
||||
String tableName = lowerTableName(request.table());
|
||||
String tableName = request.table();
|
||||
if (request.indexName() != null) {
|
||||
// no matching unique constraint so add the index
|
||||
fkeyBuffer.appendStatement(platformDdl.createIndex(new WriteCreateIndex(request.indexName(), tableName, request.cols(), false)));
|
||||
@@ -427,7 +427,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
if (i > 0) {
|
||||
buffer.append(",");
|
||||
}
|
||||
buffer.append(lowerColumnName(columns[i].trim()));
|
||||
buffer.append(platformDdl.quote(columns[i].trim()));
|
||||
}
|
||||
buffer.append(")");
|
||||
}
|
||||
@@ -494,7 +494,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
buffer.append(",").newLine();
|
||||
buffer.append(" constraint ").append(uqName).append(" unique ");
|
||||
buffer.append("(");
|
||||
buffer.append(lowerColumnName(column.getName()));
|
||||
buffer.append(platformDdl.quote(column.getName()));
|
||||
buffer.append(")");
|
||||
}
|
||||
|
||||
@@ -518,20 +518,6 @@ public class BaseTableDdl implements TableDdl {
|
||||
return cols;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the table lower case.
|
||||
*/
|
||||
protected String lowerTableName(String name) {
|
||||
return naming.lowerTableName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the column name to lower case.
|
||||
*/
|
||||
protected String lowerColumnName(String name) {
|
||||
return naming.lowerColumnName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of columns that make the primary key.
|
||||
*/
|
||||
@@ -544,7 +530,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
}
|
||||
return pk;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generate(DdlWrite writer, CreateIndex index) {
|
||||
if (platformInclude(index.getPlatforms())) {
|
||||
@@ -775,7 +761,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
* Return the name of the history table given the base table name.
|
||||
*/
|
||||
protected String historyTable(String baseTable) {
|
||||
return baseTable + historyTableSuffix;
|
||||
return naming.normaliseTable(baseTable) + historyTableSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -832,6 +818,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
.appendStatement(platformDdl.alterTableAddCheckConstraint(alter.getTableName(), alter.getCheckConstraintName(), alter.getCheckConstraint()));
|
||||
}
|
||||
|
||||
|
||||
protected void alterColumnAddForeignKey(DdlWrite writer, AlterColumn alterColumn) {
|
||||
alterTableAddForeignKey(writer.getOptions(), writer.applyForeignKeys(), new WriteForeignKey(alterColumn));
|
||||
}
|
||||
@@ -863,6 +850,7 @@ public class BaseTableDdl implements TableDdl {
|
||||
writer.dropAllForeignKeys().appendStatement(platformDdl.dropIndex(uqName, alter.getTableName()));
|
||||
}
|
||||
|
||||
|
||||
protected void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
platformDdl.alterTableDropColumn(writer, tableName, columnName);
|
||||
}
|
||||
|
||||
+8
-8
@@ -1,12 +1,12 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.util.StringHelper;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
@@ -62,7 +62,6 @@ public class DB2Ddl extends PlatformDdl {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addTablespace(DdlBuffer apply, String tablespaceName, String indexTablespace, String lobTablespace) {
|
||||
apply.append(" in ").append(tablespaceName).append(" index in ").append(indexTablespace).append(" long in ").append(lobTablespace);
|
||||
@@ -112,9 +111,10 @@ public class DB2Ddl extends PlatformDdl {
|
||||
.append("begin\n")
|
||||
.append("if exists (select constname from syscat.tabconst where tabschema = current_schema and constname = '")
|
||||
.append(maxConstraintName(constraintName).toUpperCase())
|
||||
.append("' and tabname = '").append(lowerTableName(tableName).toUpperCase()).append("') then\n")
|
||||
|
||||
.append(" prepare stmt from 'alter table ").append(lowerTableName(tableName))
|
||||
.append("' and tabname = '").append(naming.normaliseTable(tableName).toUpperCase()).append("') then\n")
|
||||
|
||||
.append(" prepare stmt from 'alter table ").append(tableName)
|
||||
.append(" drop constraint ").append(maxConstraintName(constraintName)).append("';\n")
|
||||
|
||||
.append(" execute stmt;\n")
|
||||
@@ -174,13 +174,13 @@ public class DB2Ddl extends PlatformDdl {
|
||||
|
||||
@Override
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(lowerTableName(tableName), Db2AlterTableWrite::new);
|
||||
return writer.applyAlterTable(tableName, Db2AlterTableWrite::new);
|
||||
};
|
||||
|
||||
static class Db2AlterTableWrite extends BaseAlterTableWrite {
|
||||
class Db2AlterTableWrite extends BaseAlterTableWrite {
|
||||
|
||||
public Db2AlterTableWrite(String tableName) {
|
||||
super(tableName);
|
||||
super(tableName, DB2Ddl.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+17
-39
@@ -1,14 +1,12 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.DbConstraintNaming;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.model.MColumn;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
/**
|
||||
@@ -22,6 +20,7 @@ public class Db2HistoryDdl implements PlatformHistoryDdl {
|
||||
private String systemPeriodEnd;
|
||||
private String transactionId;
|
||||
private PlatformDdl platformDdl;
|
||||
private DbConstraintNaming constraintNaming;
|
||||
private String historySuffix;
|
||||
|
||||
@Override
|
||||
@@ -30,36 +29,21 @@ public class Db2HistoryDdl implements PlatformHistoryDdl {
|
||||
this.systemPeriodEnd = config.getAsOfSysPeriod() + "_end";
|
||||
this.transactionId = config.getAsOfSysPeriod() + "_txn"; // required for DB2
|
||||
this.platformDdl = platformDdl;
|
||||
this.constraintNaming = config.getConstraintNaming();
|
||||
this.historySuffix = config.getHistoryTableSuffix();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createWithHistory(DdlWrite writer, MTable table) {
|
||||
String tableName = table.getName();
|
||||
String historyTableName = tableName + historySuffix;
|
||||
String historyTableName = historyTable(tableName);
|
||||
|
||||
DdlBuffer apply = writer.applyPostAlter();
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ")
|
||||
.append(platformDdl.lowerTableName(historyTableName)).append(" (").newLine();
|
||||
|
||||
// create history table
|
||||
Collection<MColumn> cols = table.allColumns();
|
||||
for (MColumn column : cols) {
|
||||
if (!column.isDraftOnly()) {
|
||||
writeColumnDefinition(apply, column.getName(), column.getType(), column.isNotnull() || column.isPrimaryKey());
|
||||
apply.append(",").newLine();
|
||||
}
|
||||
}
|
||||
writeColumnDefinition(apply, systemPeriodStart, "timestamp(12)", true);
|
||||
apply.append(",").newLine();
|
||||
writeColumnDefinition(apply, systemPeriodEnd, "timestamp(12)", true);
|
||||
apply.append(",").newLine();
|
||||
writeColumnDefinition(apply, transactionId, "timestamp(12)", false);
|
||||
apply.newLine().append(")").endOfStatement();
|
||||
|
||||
// enable system versioning
|
||||
// DB2 requires an EXACT copy (same column types with null/non-null, same order)
|
||||
addSysPeriodColumns(writer, tableName);
|
||||
enableSystemVersioning(apply, tableName);
|
||||
writer.applyPostAlter().append("create table ").append(historyTableName)
|
||||
.append(" as (select * from ").append(tableName).append(") with no data").endOfStatement();
|
||||
|
||||
enableSystemVersioning(writer.applyPostAlter(), tableName);
|
||||
platformDdl.alterTable(writer, tableName).setHistoryHandled();
|
||||
|
||||
// drop all: We do not drop columns here, as the whole table will be dropped
|
||||
@@ -77,10 +61,10 @@ public class Db2HistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
@Override
|
||||
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
|
||||
dropHistoryTable(writer, dropHistoryTable.getBaseTable(), dropHistoryTable.getBaseTable() + historySuffix);
|
||||
dropHistoryTable(writer, dropHistoryTable.getBaseTable());
|
||||
}
|
||||
|
||||
protected void dropHistoryTable(DdlWrite writer, String baseTable, String historyTable) {
|
||||
protected void dropHistoryTable(DdlWrite writer, String baseTable) {
|
||||
disableSystemVersioning(writer.apply(), baseTable);
|
||||
writer.apply().append("alter table ").append(baseTable).append(" drop period system_time").endOfStatement();
|
||||
|
||||
@@ -90,7 +74,7 @@ public class Db2HistoryDdl implements PlatformHistoryDdl {
|
||||
platformDdl.alterTableDropColumn(writer, baseTable, transactionId);
|
||||
|
||||
// drop the history table
|
||||
writer.applyPostAlter().append("drop table ").append(historyTable).endOfStatement();
|
||||
writer.applyPostAlter().append("drop table ").append(historyTable(baseTable)).endOfStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -118,22 +102,16 @@ public class Db2HistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type, boolean isNotNull) {
|
||||
|
||||
String platformType = platformDdl.convert(type);
|
||||
buffer.append(" ").append(platformDdl.lowerColumnName(columnName));
|
||||
buffer.append(" ").append(platformType);
|
||||
if (isNotNull) {
|
||||
buffer.append(" not null");
|
||||
}
|
||||
}
|
||||
|
||||
public void disableSystemVersioning(DdlBuffer apply, String tableName) {
|
||||
apply.append("alter table ").append(tableName).append(" drop versioning").endOfStatement();
|
||||
}
|
||||
|
||||
public void enableSystemVersioning(DdlBuffer apply, String tableName) {
|
||||
apply.append("alter table ").append(tableName).append(" add versioning use history table ").append(tableName).append(historySuffix).endOfStatement();
|
||||
apply.append("alter table ").append(tableName).append(" add versioning use history table ").append(historyTable(tableName)).endOfStatement();
|
||||
}
|
||||
|
||||
protected String historyTable(String tableName) {
|
||||
return constraintNaming.normaliseTable(tableName) + historySuffix;
|
||||
}
|
||||
|
||||
}
|
||||
+22
-22
@@ -127,11 +127,15 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
protected String historyTableName(String baseTableName) {
|
||||
return baseTableName + historySuffix;
|
||||
return quote(normalise(baseTableName) + historySuffix);
|
||||
}
|
||||
|
||||
protected String historyViewName(String baseTableName) {
|
||||
return quote(normalise(baseTableName) + viewSuffix);
|
||||
}
|
||||
|
||||
protected String procedureName(String baseTableName) {
|
||||
return baseTableName + "_history_version";
|
||||
return normalise(baseTableName) + "_history_version";
|
||||
}
|
||||
|
||||
protected String triggerName(String baseTableName) {
|
||||
@@ -146,16 +150,6 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
return normalise(baseTableName) + "_history_del";
|
||||
}
|
||||
|
||||
protected void addHistoryTable(DdlWrite writer, MTable table, String whenCreatedColumn) {
|
||||
|
||||
String baseTableName = table.getName();
|
||||
|
||||
|
||||
addSysPeriodColumns(writer, baseTableName, whenCreatedColumn);
|
||||
createHistoryTable(writer.applyPostAlter(), table);
|
||||
createWithHistoryView(writer.applyPostAlter(), baseTableName);
|
||||
}
|
||||
|
||||
protected void addSysPeriodColumns(DdlWrite writer, String baseTableName, String whenCreatedColumn) {
|
||||
|
||||
platformDdl.alterTableAddColumn(writer, baseTableName, sysPeriodStart, sysPeriodType, now);
|
||||
@@ -172,14 +166,17 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
protected void createHistoryTableAs(DdlBuffer apply, MTable table) {
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ")
|
||||
.append(platformDdl.lowerTableName(table.getName() + historySuffix)).append("(").newLine();
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(historyTableName(table.getName())).append("(").newLine();
|
||||
for (MColumn column : table.allColumns()) {
|
||||
if (!column.isDraftOnly()) {
|
||||
writeColumnDefinition(apply, column.getName(), column.getType());
|
||||
apply.append(",").newLine();
|
||||
}
|
||||
}
|
||||
// TODO: We must apply also pending dropped columns. Let's do that in a later step
|
||||
if (table.hasDroppedColumns()) {
|
||||
throw new IllegalStateException(table.getName() + " has dropped columns. Please generate drop script before enabling history");
|
||||
}
|
||||
}
|
||||
|
||||
protected void createHistoryTableWithPeriod(DdlBuffer apply) {
|
||||
@@ -196,16 +193,16 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
String platformType = platformDdl.convert(type);
|
||||
buffer.append(" ");
|
||||
buffer.append(platformDdl.lowerColumnName(columnName), 29);
|
||||
buffer.append(quote(columnName), 29);
|
||||
buffer.append(platformType);
|
||||
}
|
||||
|
||||
protected void createWithHistoryView(DdlBuffer apply, String baseTableName) {
|
||||
|
||||
apply
|
||||
.append("create view ").append(platformDdl.lowerTableName(baseTableName)).append(viewSuffix)
|
||||
.append(" as select * from ").append(platformDdl.lowerTableName(baseTableName))
|
||||
.append(" union all select * from ").append(platformDdl.lowerTableName(baseTableName + historySuffix))
|
||||
.append("create view ").append(historyViewName(baseTableName))
|
||||
.append(" as select * from ").append(quote(baseTableName))
|
||||
.append(" union all select * from ").append(historyTableName(baseTableName))
|
||||
.endOfStatement();
|
||||
}
|
||||
|
||||
@@ -215,11 +212,11 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
protected void dropWithHistoryView(DdlBuffer apply, String baseTableName) {
|
||||
apply.append("drop view ").append(platformDdl.lowerTableName(baseTableName + viewSuffix)).endOfStatement();
|
||||
apply.append("drop view ").append(historyViewName(baseTableName)).endOfStatement();
|
||||
}
|
||||
|
||||
protected void dropHistoryTable(DdlBuffer apply, String baseTableName) {
|
||||
apply.append("drop table ").append(platformDdl.lowerTableName(baseTableName + historySuffix)).endOfStatement().end();
|
||||
apply.append("drop table ").append(historyTableName(baseTableName)).endOfStatement().end();
|
||||
}
|
||||
|
||||
protected void dropSysPeriodColumns(DdlWrite writer, String baseTableName) {
|
||||
@@ -229,8 +226,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
protected void appendInsertIntoHistory(DdlBuffer buffer, String baseTable, List<String> columns) {
|
||||
|
||||
buffer.append(" insert into ").append(platformDdl.lowerTableName(baseTable + historySuffix))
|
||||
.append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
|
||||
buffer.append(" insert into ").append(historyTableName(baseTable)).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
|
||||
appendColumnNames(buffer, columns, "");
|
||||
buffer.append(") values (OLD.").append(sysPeriodStart).append(", ").append(sysPeriodEndValue).append(",");
|
||||
appendColumnNames(buffer, columns, "OLD.");
|
||||
@@ -272,4 +268,8 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
public boolean alterHistoryTables() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected String quote(String dbName) {
|
||||
return platformDdl.quote(dbName);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -31,7 +31,7 @@ public class H2HistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
// 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)
|
||||
apply
|
||||
.append("create trigger ").append(triggerName).append(" before update,delete on ").append(baseTable)
|
||||
.append("create trigger ").append(triggerName).append(" before update,delete on ").append(quote(baseTable))
|
||||
.append(" for each row call \"" + TRIGGER_CLASS + "\";").newLine();
|
||||
}
|
||||
|
||||
|
||||
+13
-8
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.DbConstraintNaming;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
@@ -16,6 +17,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
private String systemPeriodStart;
|
||||
private String systemPeriodEnd;
|
||||
private PlatformDdl platformDdl;
|
||||
private DbConstraintNaming constraintNaming;
|
||||
private String historySuffix;
|
||||
|
||||
@Override
|
||||
@@ -23,17 +25,17 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
this.systemPeriodStart = config.getAsOfSysPeriod() + "_start";
|
||||
this.systemPeriodEnd = config.getAsOfSysPeriod() + "_end";
|
||||
this.platformDdl = platformDdl;
|
||||
this.constraintNaming = config.getConstraintNaming();
|
||||
this.historySuffix = config.getHistoryTableSuffix();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createWithHistory(DdlWrite writer, MTable table) {
|
||||
String tableName = table.getName();
|
||||
String historyTableName = tableName + historySuffix;
|
||||
String historyTableName = historyTableName(tableName);
|
||||
DdlBuffer apply = writer.applyPostAlter();
|
||||
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ")
|
||||
.append(platformDdl.lowerTableName(historyTableName)).append(" (").newLine();
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(historyTableName).append(" (").newLine();
|
||||
|
||||
// create history table
|
||||
Collection<MColumn> cols = table.allColumns();
|
||||
@@ -67,7 +69,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
@Override
|
||||
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
|
||||
dropHistoryTable(writer.applyDropDependencies(), dropHistoryTable.getBaseTable(),
|
||||
dropHistoryTable.getBaseTable() + historySuffix);
|
||||
historyTableName(dropHistoryTable.getBaseTable()));
|
||||
}
|
||||
|
||||
protected void dropHistoryTable(DdlBuffer apply, String baseTable, String historyTable) {
|
||||
@@ -113,7 +115,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
boolean isNotNull, String generated) {
|
||||
|
||||
String platformType = platformDdl.convert(type);
|
||||
buffer.append(" ").append(platformDdl.lowerColumnName(columnName));
|
||||
buffer.append(" ").append(columnName);
|
||||
buffer.append(" ").append(platformType);
|
||||
if (defaultValue != null) {
|
||||
buffer.append(" default ").append(defaultValue);
|
||||
@@ -127,16 +129,19 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
public void disableSystemVersioning(DdlBuffer apply, String tableName) {
|
||||
apply.append("alter table ").append(platformDdl.lowerTableName(tableName)).append(" drop system versioning").endOfStatement();
|
||||
apply.append("alter table ").append(tableName).append(" drop system versioning").endOfStatement();
|
||||
}
|
||||
|
||||
public void enableSystemVersioning(DdlBuffer apply, String tableName, boolean validated) {
|
||||
apply.append("alter table ").append(platformDdl.lowerTableName(tableName))
|
||||
.append(" add system versioning history table ").append(platformDdl.lowerTableName(tableName + historySuffix));
|
||||
apply.append("alter table ").append(tableName).append(" add system versioning history table ").append(historyTableName(tableName));
|
||||
if (!validated) {
|
||||
apply.append(" not validated");
|
||||
}
|
||||
apply.endOfStatement();
|
||||
}
|
||||
|
||||
protected String historyTableName(String tableName) {
|
||||
return constraintNaming.normaliseTable(tableName) + historySuffix;
|
||||
}
|
||||
|
||||
}
|
||||
+5
-4
@@ -32,13 +32,14 @@ public class MySqlDdl extends PlatformDdl {
|
||||
*/
|
||||
@Override
|
||||
public String dropIndex(String indexName, String tableName, boolean concurrent) {
|
||||
return "drop index " + maxConstraintName(indexName) + " on " + tableName;
|
||||
return "drop index " + maxConstraintName(indexName) + " on " + quote(tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void alterTableDropColumn(DdlWrite writer, String tableName, String columnName) {
|
||||
if (this.useMigrationStoredProcedures) {
|
||||
alterTable(writer, tableName).raw("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')");
|
||||
alterTable(writer, tableName).raw("CALL usp_ebean_drop_column('").append(naming.normaliseTable(tableName))
|
||||
.append("', '").append(naming.normaliseColumn(columnName)).append("')");
|
||||
} else {
|
||||
super.alterTableDropColumn(writer, tableName, columnName);
|
||||
}
|
||||
@@ -49,7 +50,7 @@ public class MySqlDdl extends PlatformDdl {
|
||||
*/
|
||||
@Override
|
||||
public String alterTableDropForeignKey(String tableName, String fkName) {
|
||||
return "alter table " + tableName + " drop foreign key " + maxConstraintName(fkName);
|
||||
return "alter table " + quote(tableName) + " drop foreign key " + maxConstraintName(fkName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -146,7 +147,7 @@ public class MySqlDdl extends PlatformDdl {
|
||||
if (DdlHelp.isDropComment(tableComment)) {
|
||||
tableComment = "";
|
||||
}
|
||||
apply.append(String.format("alter table %s comment = '%s'", tableName, tableComment)).endOfStatement();
|
||||
apply.append(String.format("alter table %s comment = '%s'", quote(tableName), tableComment)).endOfStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+38
-40
@@ -20,12 +20,17 @@ import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Controls the DDL generation for a specific database platform.
|
||||
*/
|
||||
public class PlatformDdl {
|
||||
|
||||
// matches on pattern "check ( COLUMNAME ... )". ColumnName is match group 2;
|
||||
private static final Pattern CHECK_PATTERN = Pattern.compile("(.*?\\( *)([^ ]+)(.*)");
|
||||
|
||||
protected final DatabasePlatform platform;
|
||||
|
||||
protected PlatformHistoryDdl historyDdl = new NoHistorySupportDdl();
|
||||
@@ -119,7 +124,7 @@ public class PlatformDdl {
|
||||
protected boolean inlineForeignKeys;
|
||||
|
||||
protected boolean includeStorageEngine;
|
||||
|
||||
|
||||
protected final DbDefaultValue dbDefaultValue;
|
||||
|
||||
protected String fallbackArrayType = "varchar(1000)";
|
||||
@@ -258,7 +263,7 @@ public class PlatformDdl {
|
||||
}
|
||||
|
||||
buffer.append(" ");
|
||||
buffer.append(lowerColumnName(column.getName()), 29);
|
||||
buffer.append(quote(column.getName()), 29);
|
||||
buffer.append(columnDefn);
|
||||
if (!Boolean.TRUE.equals(column.isPrimaryKey())) {
|
||||
String defaultValue = convertDefaultValue(column.getDefaultValue());
|
||||
@@ -278,7 +283,7 @@ public class PlatformDdl {
|
||||
* Returns the check constraint.
|
||||
*/
|
||||
public String createCheckConstraint(String ckName, String checkConstraint) {
|
||||
return " constraint " + ckName + " " + checkConstraint;
|
||||
return " constraint " + maxConstraintName(ckName) + " " + quoteCheckConstraint(checkConstraint);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -292,7 +297,7 @@ public class PlatformDdl {
|
||||
* Return the drop foreign key clause.
|
||||
*/
|
||||
public String alterTableDropForeignKey(String tableName, String fkName) {
|
||||
return "alter table " + alterTableIfExists + lowerTableName(tableName) + " " + dropConstraintIfExists + " " + maxConstraintName(fkName);
|
||||
return "alter table " + alterTableIfExists + quote(tableName) + " " + dropConstraintIfExists + " " + maxConstraintName(fkName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -372,7 +377,7 @@ public class PlatformDdl {
|
||||
*/
|
||||
public String createSequence(String sequenceName, DdlIdentity identity) {
|
||||
StringBuilder sb = new StringBuilder("create sequence ");
|
||||
sb.append(sequenceName);
|
||||
sb.append(quote(sequenceName));
|
||||
sb.append(identity.sequenceOptions(sequenceStartWith, sequenceIncrementBy, sequenceCache));
|
||||
sb.append(";");
|
||||
return sb.toString();
|
||||
@@ -382,14 +387,14 @@ public class PlatformDdl {
|
||||
* Return the drop sequence statement (potentially with if exists clause).
|
||||
*/
|
||||
public String dropSequence(String sequenceName) {
|
||||
return dropSequenceIfExists + sequenceName;
|
||||
return dropSequenceIfExists + quote(sequenceName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the drop table statement (potentially with if exists clause).
|
||||
*/
|
||||
public String dropTable(String tableName) {
|
||||
return dropTableIfExists + lowerTableName(tableName) + dropTableCascade;
|
||||
return dropTableIfExists + quote(tableName) + dropTableCascade;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -405,7 +410,7 @@ public class PlatformDdl {
|
||||
public String dropIndex(String indexName, String tableName, boolean concurrent) {
|
||||
return dropIndexIfExists + maxConstraintName(indexName);
|
||||
}
|
||||
|
||||
|
||||
public String createIndex(WriteCreateIndex create) {
|
||||
if (create.useDefinition()) {
|
||||
return create.getDefinition();
|
||||
@@ -422,7 +427,7 @@ public class PlatformDdl {
|
||||
if (create.isNotExistsCheck()) {
|
||||
buffer.append(createIndexIfNotExists);
|
||||
}
|
||||
buffer.append(maxConstraintName(create.getIndexName())).append(" on ").append(lowerTableName(create.getTableName()));
|
||||
buffer.append(maxConstraintName(create.getIndexName())).append(" on ").append(quote(create.getTableName()));
|
||||
appendColumns(create.getColumns(), buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
@@ -435,7 +440,7 @@ public class PlatformDdl {
|
||||
StringBuilder buffer = new StringBuilder(90);
|
||||
buffer.append("foreign key");
|
||||
appendColumns(request.cols(), buffer);
|
||||
buffer.append(" references ").append(lowerTableName(request.refTable()));
|
||||
buffer.append(" references ").append(quote(request.refTable()));
|
||||
appendColumns(request.refCols(), buffer);
|
||||
appendForeignKeySuffix(request, buffer);
|
||||
return buffer.toString();
|
||||
@@ -448,13 +453,13 @@ public class PlatformDdl {
|
||||
|
||||
StringBuilder buffer = new StringBuilder(90);
|
||||
buffer
|
||||
.append("alter table ").append(lowerTableName(request.table()))
|
||||
.append("alter table ").append(quote(request.table()))
|
||||
.append(" add constraint ").append(maxConstraintName(request.fkName()))
|
||||
.append(" foreign key");
|
||||
appendColumns(request.cols(), buffer);
|
||||
buffer
|
||||
.append(" references ")
|
||||
.append(lowerTableName(request.refTable()));
|
||||
.append(quote(request.refTable()));
|
||||
appendColumns(request.refCols(), buffer);
|
||||
appendForeignKeySuffix(request, buffer);
|
||||
if (options.isForeignKeySkipCheck()) {
|
||||
@@ -503,14 +508,14 @@ public class PlatformDdl {
|
||||
* Drop a unique constraint from the table (Sometimes this is an index).
|
||||
*/
|
||||
public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) {
|
||||
return "alter table " + lowerTableName(tableName) + " " + dropUniqueConstraint + " " + maxConstraintName(uniqueConstraintName);
|
||||
return "alter table " + quote(tableName) + " " + dropUniqueConstraint + " " + maxConstraintName(uniqueConstraintName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop a unique constraint from the table.
|
||||
*/
|
||||
public String alterTableDropConstraint(String tableName, String constraintName) {
|
||||
return "alter table " + lowerTableName(tableName) + " " + dropConstraintIfExists + " " + maxConstraintName(constraintName);
|
||||
return "alter table " + quote(tableName) + " " + dropConstraintIfExists + " " + maxConstraintName(constraintName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -528,7 +533,7 @@ public class PlatformDdl {
|
||||
public String alterTableAddUniqueConstraint(String tableName, String uqName, String[] columns, String[] nullableColumns) {
|
||||
|
||||
StringBuilder buffer = new StringBuilder(90);
|
||||
buffer.append("alter table ").append(tableName).append(" add constraint ").append(maxConstraintName(uqName)).append(" unique ");
|
||||
buffer.append("alter table ").append(quote(tableName)).append(" add constraint ").append(maxConstraintName(uqName)).append(" unique ");
|
||||
appendColumns(columns, buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
@@ -619,9 +624,10 @@ public class PlatformDdl {
|
||||
* Alter table adding the check constraint.
|
||||
*/
|
||||
public String alterTableAddCheckConstraint(String tableName, String checkConstraintName, String checkConstraint) {
|
||||
return "alter table " + lowerTableName(tableName) + " " + addConstraint + " " + maxConstraintName(checkConstraintName) + " " + checkConstraint;
|
||||
return "alter table " + quote(tableName) + " " + addConstraint + " " + maxConstraintName(checkConstraintName) + " " + quoteCheckConstraint(checkConstraint);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Alter column setting the default value.
|
||||
* <p>
|
||||
@@ -663,7 +669,7 @@ public class PlatformDdl {
|
||||
* Creates or replace a new DdlAlterTable for given tableName.
|
||||
*/
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(lowerTableName(tableName), BaseAlterTableWrite::new);
|
||||
return writer.applyAlterTable(tableName, k -> new BaseAlterTableWrite(k, this));
|
||||
}
|
||||
|
||||
protected void appendColumns(String[] columns, StringBuilder buffer) {
|
||||
@@ -672,31 +678,11 @@ public class PlatformDdl {
|
||||
if (i > 0) {
|
||||
buffer.append(",");
|
||||
}
|
||||
buffer.append(lowerColumnName(columns[i].trim()));
|
||||
buffer.append(quote(columns[i].trim()));
|
||||
}
|
||||
buffer.append(")");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the table to lower case.
|
||||
* <p>
|
||||
* Override as desired. Generally lower case with underscore is a good cross database
|
||||
* choice for column/table names.
|
||||
*/
|
||||
protected String lowerTableName(String name) {
|
||||
return naming.lowerTableName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the column name to lower case.
|
||||
* <p>
|
||||
* Override as desired. Generally lower case with underscore is a good cross database
|
||||
* choice for column/table names.
|
||||
*/
|
||||
protected String lowerColumnName(String name) {
|
||||
return naming.lowerColumnName(name);
|
||||
}
|
||||
|
||||
public DatabasePlatform getPlatform() {
|
||||
return platform;
|
||||
}
|
||||
@@ -740,7 +726,7 @@ public class PlatformDdl {
|
||||
if (DdlHelp.isDropComment(tableComment)) {
|
||||
tableComment = "";
|
||||
}
|
||||
apply.append(String.format("comment on table %s is '%s'", tableName, tableComment)).endOfStatement();
|
||||
apply.append(String.format("comment on table %s is '%s'", quote(tableName), tableComment)).endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -750,7 +736,7 @@ public class PlatformDdl {
|
||||
if (DdlHelp.isDropComment(comment)) {
|
||||
comment = "";
|
||||
}
|
||||
apply.append(String.format("comment on column %s.%s is '%s'", table, column, comment)).endOfStatement();
|
||||
apply.append(String.format("comment on column %s.%s is '%s'", quote(table), quote(column), comment)).endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -813,4 +799,16 @@ public class PlatformDdl {
|
||||
// now only supported for db2
|
||||
}
|
||||
|
||||
protected String quote(String dbName) {
|
||||
return platform.convertQuotedIdentifiers(dbName);
|
||||
}
|
||||
|
||||
protected String quoteCheckConstraint(String checkConstraint) {
|
||||
Matcher matcher = CHECK_PATTERN.matcher(checkConstraint);
|
||||
if (matcher.matches()) {
|
||||
return matcher.replaceFirst("$1" + quote(matcher.group(2)) + "$3");
|
||||
}
|
||||
return checkConstraint;
|
||||
}
|
||||
|
||||
}
|
||||
+7
-8
@@ -21,8 +21,8 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
*/
|
||||
@Override
|
||||
protected void createHistoryTable(DdlBuffer apply, MTable table) {
|
||||
apply.append("create table ").append(platformDdl.lowerTableName(table.getName() + historySuffix))
|
||||
.append("(like ").append(platformDdl.lowerTableName(table.getName())).append(")").endOfStatement();
|
||||
apply.append("create table ").append(historyTableName(table.getName()))
|
||||
.append("(like ").append(table.getName()).append(")").endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,8 +33,8 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
platformDdl.alterTableAddColumn(writer, baseTableName, sysPeriod, "tstzrange not null", "tstzrange(" + now + ", null)");
|
||||
if (whenCreatedColumn != null) {
|
||||
writer.applyPostAlter()
|
||||
.append("update ").append(platformDdl.lowerTableName(baseTableName)).append(" set ")
|
||||
.append(sysPeriod).append(" = tstzrange(").append(platformDdl.lowerColumnName(whenCreatedColumn)).append(", null)").endOfStatement();
|
||||
.append("update ").append(baseTableName).append(" set ")
|
||||
.append(sysPeriod).append(" = tstzrange(").append(whenCreatedColumn).append(", null)").endOfStatement();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,15 +55,14 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
createOrReplaceFunction(buffer, procedureName, historyTableName(baseTableName), columnNames);
|
||||
buffer
|
||||
.append("create trigger ").append(triggerName).newLine()
|
||||
.append(" before update or delete on ").append(platformDdl.lowerTableName(baseTableName)).newLine()
|
||||
.append(" before update or delete on ").append(baseTableName).newLine()
|
||||
.append(" for each row execute procedure ").append(procedureName).append("();").newLine().newLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void dropTriggers(DdlBuffer buffer, String baseTable) {
|
||||
// rollback trigger then function
|
||||
buffer.append("drop trigger if exists ").append(triggerName(baseTable)).append(" on ")
|
||||
.append(platformDdl.lowerTableName(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.end();
|
||||
}
|
||||
@@ -101,7 +100,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
|
||||
@Override
|
||||
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) {
|
||||
buffer.append(" insert into ").append(platformDdl.lowerTableName(historyTable)).append(" (").append(sysPeriod).append(",");
|
||||
buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriod).append(",");
|
||||
appendColumnNames(buffer, columns, "");
|
||||
buffer.append(") values (tstzrange(lowerTs,upperTs), ");
|
||||
appendColumnNames(buffer, columns, "OLD.");
|
||||
|
||||
+3
-3
@@ -65,12 +65,12 @@ public class SQLiteDdl extends PlatformDdl {
|
||||
|
||||
@Override
|
||||
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
|
||||
return writer.applyAlterTable(lowerTableName(tableName), SQLiteAlterTableWrite::new);
|
||||
return writer.applyAlterTable(tableName, SQLiteAlterTableWrite::new);
|
||||
}
|
||||
|
||||
static class SQLiteAlterTableWrite extends BaseAlterTableWrite {
|
||||
class SQLiteAlterTableWrite extends BaseAlterTableWrite {
|
||||
public SQLiteAlterTableWrite(String tableName) {
|
||||
super(tableName);
|
||||
super(tableName, SQLiteDdl.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
-1
@@ -72,7 +72,6 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
// issues#233
|
||||
|
||||
StringBuilder sb = new StringBuilder(256);
|
||||
sb.append("create unique nonclustered index ").append(uqName).append(" on ").append(tableName).append('(');
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
|
||||
+42
-23
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.DbConstraintNaming;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
@@ -17,12 +18,17 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
|
||||
private String systemPeriodStart;
|
||||
private String systemPeriodEnd;
|
||||
private PlatformDdl platformDdl;
|
||||
protected DbConstraintNaming constraintNaming;
|
||||
protected String historySuffix;
|
||||
|
||||
@Override
|
||||
public void configure(DatabaseConfig config, PlatformDdl platformDdl) {
|
||||
this.systemPeriodStart = config.getAsOfSysPeriod() + "From";
|
||||
this.systemPeriodEnd = config.getAsOfSysPeriod() + "To";
|
||||
this.platformDdl = platformDdl;
|
||||
|
||||
this.constraintNaming = config.getConstraintNaming();
|
||||
this.historySuffix = config.getHistoryTableSuffix();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,31 +37,21 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
|
||||
enableSystemVersioning(writer, baseTable);
|
||||
}
|
||||
|
||||
String getHistoryTable(String baseTable) {
|
||||
String historyTable = baseTable + "_history";
|
||||
if (baseTable.startsWith("[")) {
|
||||
historyTable = historyTable.replace("]", "") + "]";
|
||||
}
|
||||
if (historyTable.indexOf('.') == -1) {
|
||||
// history must contain schema, add the default schema if none was specified
|
||||
historyTable = "dbo." + historyTable;
|
||||
}
|
||||
return historyTable;
|
||||
}
|
||||
|
||||
private void enableSystemVersioning(DdlWrite writer, String baseTable) {
|
||||
DdlBuffer apply = writer.applyPostAlter();
|
||||
apply.append("alter table ").append(baseTable).newLine()
|
||||
apply.append("alter table ").append(quote(baseTable)).newLine()
|
||||
.append(" add ").append(systemPeriodStart).append(" datetime2 GENERATED ALWAYS AS ROW START NOT NULL DEFAULT SYSUTCDATETIME(),").newLine()
|
||||
.append(" ").append(systemPeriodEnd).append(" datetime2 GENERATED ALWAYS AS ROW END NOT NULL DEFAULT '9999-12-31T23:59:59.9999999',").newLine()
|
||||
.append("period for system_time (").append(systemPeriodStart).append(", ").append(systemPeriodEnd).append(")").endOfStatement();
|
||||
|
||||
apply.append("alter table ").append(baseTable).append(" set (system_versioning = on (history_table=")
|
||||
.append(getHistoryTable(baseTable)).append("))").endOfStatement();
|
||||
.append(historyTableWithSchema(baseTable)).append("))").endOfStatement();
|
||||
|
||||
DdlBuffer drop = writer.dropAll();
|
||||
drop.append("IF OBJECT_ID('").append(baseTable).append("', 'U') IS NOT NULL alter table ").append(baseTable).append(" set (system_versioning = off)").endOfStatement();
|
||||
drop.append("IF OBJECT_ID('").append(baseTable).append("_history', 'U') IS NOT NULL drop table ").append(baseTable).append("_history").endOfStatement();
|
||||
drop.append("IF OBJECT_ID('").append(quote(baseTable)).append("', 'U') IS NOT NULL alter table ")
|
||||
.append(quote(baseTable)).append(" set (system_versioning = off)").endOfStatement();
|
||||
drop.append("IF OBJECT_ID('").append(historyTableName(baseTable)).append("', 'U') IS NOT NULL drop table ")
|
||||
.append(historyTableName(baseTable)).endOfStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,13 +69,13 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
|
||||
// switch of versioning & period - must be done before altering
|
||||
DdlBuffer apply = writer.apply();
|
||||
apply.append("-- dropping history support for ").append(baseTable).endOfStatement();
|
||||
apply.append("alter table ").append(baseTable).append(" set (system_versioning = off)").endOfStatement();
|
||||
apply.append("alter table ").append(baseTable).append(" drop period for system_time").endOfStatement();
|
||||
apply.append("alter table ").append(quote(baseTable)).append(" set (system_versioning = off)").endOfStatement();
|
||||
apply.append("alter table ").append(quote(baseTable)).append(" drop period for system_time").endOfStatement();
|
||||
apply.end();
|
||||
// now drop tables & columns, they will go to alter table/post alter buffers
|
||||
platformDdl.alterTableDropColumn(writer, baseTable, systemPeriodStart);
|
||||
platformDdl.alterTableDropColumn(writer, baseTable, systemPeriodEnd);
|
||||
writer.applyPostAlter().appendStatement(platformDdl.dropTable(baseTable + "_history"));
|
||||
writer.applyPostAlter().appendStatement(platformDdl.dropTable(historyTableName(baseTable)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,13 +91,36 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
|
||||
if (!alter.isHistoryHandled()) {
|
||||
// SQL Server 2016 does not need triggers
|
||||
DdlBuffer apply = writer.apply();
|
||||
apply.append("-- alter table ").append(tableName).append(" set (system_versioning = off (history_table=")
|
||||
.append(getHistoryTable(tableName)).append("))").endOfStatement();
|
||||
apply.append("-- alter table ").append(quote(tableName)).append(" set (system_versioning = off (history_table=")
|
||||
.append(historyTableWithSchema(tableName)).append("))").endOfStatement();
|
||||
apply.append("-- history migration goes here").newLine();
|
||||
apply.append("-- alter table ").append(tableName).append(" set (system_versioning = on (history_table=")
|
||||
.append(getHistoryTable(tableName)).append("))").endOfStatement();
|
||||
apply.append("-- alter table ").append(quote(tableName)).append(" set (system_versioning = on (history_table=")
|
||||
.append(historyTableWithSchema(tableName)).append("))").endOfStatement();
|
||||
}
|
||||
alter.setHistoryHandled();
|
||||
}
|
||||
|
||||
protected String quote(String baseTable) {
|
||||
return platformDdl.quote(baseTable);
|
||||
}
|
||||
|
||||
protected String normalise(String tableName) {
|
||||
return constraintNaming.normaliseTable(tableName);
|
||||
}
|
||||
|
||||
protected String historyTableName(String baseTableName) {
|
||||
return normalise(baseTableName) + historySuffix;
|
||||
}
|
||||
|
||||
protected String historyTableWithSchema(String baseTableName) {
|
||||
String historyTable = historyTableName(baseTableName);
|
||||
int lastPeriod = baseTableName.lastIndexOf('.');
|
||||
if (lastPeriod == -1) {
|
||||
// history must contain schema, add the default schema if none was specified
|
||||
return "dbo." + historyTable;
|
||||
} else {
|
||||
return baseTableName.substring(0, lastPeriod + 1) + historyTable;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -309,7 +309,7 @@ public class MTable {
|
||||
if (localColumn == null) {
|
||||
// can ignore if draftOnly column and non-draft table
|
||||
if (!newColumn.isDraftOnly() || draft) {
|
||||
diffNewColumn(newColumn);
|
||||
diffNewColumn(newColumn, newTable);
|
||||
}
|
||||
} else {
|
||||
localColumn.compare(modelDiff, this, newColumn);
|
||||
@@ -509,6 +509,13 @@ public class MTable {
|
||||
return columnNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true, if there are pending dropped columns.
|
||||
*/
|
||||
public boolean hasDroppedColumns() {
|
||||
return !droppedColumns.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the columns (excluding columns marked as dropped).
|
||||
*/
|
||||
@@ -628,11 +635,11 @@ public class MTable {
|
||||
/**
|
||||
* Add a 'new column' to the AddColumn migration object.
|
||||
*/
|
||||
private void diffNewColumn(MColumn newColumn) {
|
||||
private void diffNewColumn(MColumn newColumn, MTable newTable) {
|
||||
if (addColumn == null) {
|
||||
addColumn = new AddColumn();
|
||||
addColumn.setTableName(name);
|
||||
if (withHistory) {
|
||||
if (newTable.isWithHistory()) {
|
||||
// These addColumns need to occur on the history
|
||||
// table as well as the base table
|
||||
addColumn.setWithHistory(Boolean.TRUE);
|
||||
|
||||
+1
-3
@@ -424,9 +424,7 @@ public class ModelContainer {
|
||||
for (Object change : changeSet.getChangeSetChildren()) {
|
||||
if (change instanceof DropColumn) {
|
||||
DropColumn dropColumn = (DropColumn) change;
|
||||
if (Boolean.TRUE.equals(dropColumn.isWithHistory())) {
|
||||
registerPendingDropColumn(dropColumn);
|
||||
}
|
||||
registerPendingDropColumn(dropColumn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+15
-2
@@ -3,13 +3,14 @@ package io.ebeaninternal.dbmigration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class DefaultDbMigrationTest {
|
||||
class DefaultDbMigrationTest {
|
||||
|
||||
private final DefaultDbMigration migration = new DefaultDbMigration();
|
||||
|
||||
@Test
|
||||
public void trimDropsFor() {
|
||||
void trimDropsFor() {
|
||||
assertEquals("1.2", migration.trimDropsFor("V1.2__hello"));
|
||||
assertEquals("1.2", migration.trimDropsFor("v1.2__hello"));
|
||||
assertEquals("1.2", migration.trimDropsFor("v1.2"));
|
||||
@@ -17,4 +18,16 @@ public class DefaultDbMigrationTest {
|
||||
assertEquals("junk1.2", migration.trimDropsFor("junk1.2__"));
|
||||
assertEquals("junk1.2", migration.trimDropsFor("junk1.2__more"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkDropVersion_when_matches_throwsIAE() {
|
||||
assertThrows(IllegalArgumentException.class, () -> migration.checkDropVersion("1.0", "1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkDropVersion_ok() {
|
||||
migration.checkDropVersion("1.0", null);
|
||||
migration.checkDropVersion("1.0", "1.0.0");
|
||||
migration.checkDropVersion("1.0", "1.1");
|
||||
}
|
||||
}
|
||||
+10
-3
@@ -2,6 +2,9 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SqlServerHistoryDdlTest {
|
||||
@@ -10,8 +13,12 @@ public class SqlServerHistoryDdlTest {
|
||||
public void getHistoryTable() {
|
||||
|
||||
SqlServerHistoryDdl ddl = new SqlServerHistoryDdl();
|
||||
assertThat(ddl.getHistoryTable("foo")).isEqualTo("dbo.foo_history");
|
||||
assertThat(ddl.getHistoryTable("bar.foo")).isEqualTo("bar.foo_history");
|
||||
assertThat(ddl.getHistoryTable("[Foo]")).isEqualTo("dbo.[Foo_history]");
|
||||
ddl.configure(new DatabaseConfig(), new SqlServerDdl(new SqlServer17Platform()));
|
||||
assertThat(ddl.historyTableWithSchema("foo")).isEqualTo("dbo.foo_history");
|
||||
assertThat(ddl.historyTableWithSchema("bar.foo")).isEqualTo("bar.foo_history");
|
||||
// test with reserved keywords in quotes
|
||||
assertThat(ddl.historyTableWithSchema("[select]")).isEqualTo("dbo.select_history");
|
||||
assertThat(ddl.historyTableWithSchema("\"select\"")).isEqualTo("dbo.select_history");
|
||||
assertThat(ddl.historyTableWithSchema("`select`")).isEqualTo("dbo.select_history");
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -1,11 +1,11 @@
|
||||
create table persons (
|
||||
id bigint generated by default as identity (start with 1000 increment by 40) not null,
|
||||
surname varchar(64) not null,
|
||||
name varchar(64) not null,
|
||||
constraint pk_persons primary key (id)
|
||||
create table PERSONS (
|
||||
ID bigint generated by default as identity (start with 1000 increment by 40) not null,
|
||||
SURNAME varchar(64) not null,
|
||||
NAME varchar(64) not null,
|
||||
constraint pk_persons primary key (ID)
|
||||
);
|
||||
|
||||
create table phones (
|
||||
create table PHONES (
|
||||
id bigint generated by default as identity not null,
|
||||
phone_number varchar(7) not null,
|
||||
person_id bigint not null,
|
||||
@@ -14,6 +14,6 @@ create table phones (
|
||||
);
|
||||
|
||||
-- foreign keys and indices
|
||||
create index ix_phones_person_id on phones (person_id);
|
||||
alter table phones add constraint fk_phones_person_id foreign key (person_id) references persons (id) on delete restrict on update restrict;
|
||||
create index ix_phones_person_id on PHONES (person_id);
|
||||
alter table PHONES add constraint fk_phones_person_id foreign key (person_id) references PERSONS (ID) on delete restrict on update restrict;
|
||||
|
||||
+8
-8
@@ -1,11 +1,11 @@
|
||||
create table persons (
|
||||
id bigint generated by default as identity (start with 1000 increment by 40) not null,
|
||||
surname varchar(64) not null,
|
||||
name varchar(64) not null,
|
||||
constraint pk_persons primary key (id)
|
||||
create table PERSONS (
|
||||
ID bigint generated by default as identity (start with 1000 increment by 40) not null,
|
||||
SURNAME varchar(64) not null,
|
||||
NAME varchar(64) not null,
|
||||
constraint pk_persons primary key (ID)
|
||||
);
|
||||
|
||||
create table phones (
|
||||
create table PHONES (
|
||||
id bigint generated by default as identity not null,
|
||||
phone_number varchar(7) not null,
|
||||
person_id bigint not null,
|
||||
@@ -14,6 +14,6 @@ create table phones (
|
||||
);
|
||||
|
||||
-- foreign keys and indices
|
||||
create index ix_phones_person_id on phones (person_id);
|
||||
alter table phones add constraint fk_phones_person_id foreign key (person_id) references persons (id) on delete restrict on update restrict not valid;
|
||||
create index ix_phones_person_id on PHONES (person_id);
|
||||
alter table PHONES add constraint fk_phones_person_id foreign key (person_id) references PERSONS (ID) on delete restrict on update restrict not valid;
|
||||
|
||||
Reference in new issue
Block a user