#2004 - MariaDB History DDL using add/drop system versioning

This commit is contained in:
rob bygrave
2020-06-29 14:03:21 +12:00
parent 70ba658442
commit fc8dd18fd1
5 changed files with 63 additions and 57 deletions
@@ -25,7 +25,7 @@ public class MariaDbHistorySupport extends DbStandardHistorySupport {
*/
@Override
public String getSysPeriodLower(String tableAlias, String sysPeriod) {
return tableAlias + "." + sysPeriod + "From";
return tableAlias + ".row_start";
}
/**
@@ -33,6 +33,6 @@ public class MariaDbHistorySupport extends DbStandardHistorySupport {
*/
@Override
public String getSysPeriodUpper(String tableAlias, String sysPeriod) {
return tableAlias + "." + sysPeriod + "To";
return tableAlias + ".row_end";
}
}
@@ -14,15 +14,9 @@ import java.io.IOException;
*/
public class MariaDbHistoryDdl implements PlatformHistoryDdl {
private String systemPeriodStart;
private String systemPeriodEnd;
private PlatformDdl platformDdl;
@Override
public void configure(ServerConfig serverConfig, PlatformDdl platformDdl) {
this.systemPeriodStart = serverConfig.getAsOfSysPeriod() + "From";
this.systemPeriodEnd = serverConfig.getAsOfSysPeriod() + "To";
this.platformDdl = platformDdl;
// do nothing
}
@Override
@@ -31,50 +25,19 @@ public class MariaDbHistoryDdl 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) throws IOException {
DdlBuffer apply = writer.applyHistoryView();
final String systemTime = String.format("system_time(%s, %s),", systemPeriodStart, systemPeriodEnd);
apply.append("alter table ").append(baseTable).newLine()
.append(" add column ").append(systemPeriodStart).append(" timestamp(6) generated always as row start,").newLine()
.append(" add column ").append(systemPeriodEnd).append(" timestamp(6) generated always as row end,").newLine()
.append(" add period for ").append(systemTime).newLine()
.append(" add system versioning;").newLine();
apply.append("alter table ").append(baseTable).append(" add system versioning").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("alter table ").append(baseTable).append(" drop system versioning").endOfStatement();
}
@Override
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException {
String baseTable = dropHistoryTable.getBaseTable();
DdlBuffer apply = writer.applyHistoryView();
apply.append("-- dropping history support for ").append(baseTable).endOfStatement();
// drop default constraints
apply.append(platformDdl.alterColumnDefaultValue(baseTable, systemPeriodStart, DdlHelp.DROP_DEFAULT)).endOfStatement();
apply.append(platformDdl.alterColumnDefaultValue(baseTable, systemPeriodEnd, DdlHelp.DROP_DEFAULT)).endOfStatement();
// switch of versioning & period
apply.append("alter table ").append(baseTable).append(" set (system_versioning = off)").endOfStatement();
apply.append("alter table ").append(baseTable).append(" drop period for system_time").endOfStatement();
// now drop tables & columns
apply.append("alter table ").append(baseTable).append(" drop column ").append(systemPeriodStart).endOfStatement();
apply.append("alter table ").append(baseTable).append(" drop column ").append(systemPeriodEnd).endOfStatement();
//apply.append("IF OBJECT_ID('").append(baseTable).append("_history', 'U') IS NOT NULL drop table ").append(baseTable).append("_history").endOfStatement();
apply.end();
apply.append("alter table ").append(baseTable).append(" drop system versioning").endOfStatement();
}
@Override
@@ -84,15 +47,7 @@ public class MariaDbHistoryDdl implements PlatformHistoryDdl {
}
@Override
public void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable) throws IOException {
// SQL Server 2016 does not need triggers
DdlBuffer apply = writer.applyHistoryView();
String baseTableName = baseTable.getBaseTable();
apply.append("-- alter table ").append(baseTableName).append(" set (system_versioning = off (history_table=")
.append(getHistoryTable(baseTableName)).append("))").endOfStatement();
apply.append("-- history migration goes here").newLine();
apply.append("-- alter table ").append(baseTableName).append(" set (system_versioning = on (history_table=")
.append(getHistoryTable(baseTableName)).append("))").endOfStatement();
public void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable) {
// do nothing
}
}
@@ -505,7 +505,7 @@ public class InternalConfiguration {
private DataTimeZone initDataTimeZone() {
String tz = serverConfig.getDataTimeZone();
if (tz == null) {
if (isMySqlOrMariaDB(getPlatform())) {
if (isMySql(getPlatform())) {
return new MySqlDataTimeZone();
}
return new NoDataTimeZone();
@@ -517,8 +517,8 @@ public class InternalConfiguration {
}
}
private boolean isMySqlOrMariaDB(Platform platform) {
return platform.base() == Platform.MYSQL || platform.base() == Platform.MARIADB;
private boolean isMySql(Platform platform) {
return platform.base() == Platform.MYSQL;
}
public DataTimeZone getDataTimeZone() {