#2612 - Incorrect DDL generation for MySql on history tables that use quoted identifiers

This commit is contained in:
Rob Bygrave
2022-03-28 13:05:59 +13:00
parent c8bb3c7b57
commit 5c3b680c8a
8 changed files with 27 additions and 49 deletions
@@ -19,10 +19,7 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
protected String sysPeriod;
protected String sysPeriodStart;
protected String sysPeriodEnd;
protected String viewSuffix;
protected String sysPeriodType = "datetime(6)";
protected String now = "now(6)";
protected String sysPeriodEndValue = "now(6)";
@@ -35,14 +32,12 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
super.configure(config, platformDdl);
this.sysPeriod = config.getAsOfSysPeriod();
this.viewSuffix = config.getAsOfViewSuffix();
this.sysPeriodStart = sysPeriod + "_start";
this.sysPeriodEnd = sysPeriod + "_end";
}
@Override
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
String baseTable = dropHistoryTable.getBaseTable();
// drop in appropriate order
@@ -55,20 +50,17 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
@Override
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
String baseTable = addHistoryTable.getBaseTable();
MTable table = writer.getTable(baseTable);
if (table == null) {
throw new IllegalStateException("MTable " + baseTable + " not found in writer? (required for history DDL)");
}
createWithHistory(writer, table);
}
@Override
public void createWithHistory(DdlWrite writer, MTable table) {
String baseTable = table.getName();
String baseTable = quote(table.getName());
addSysPeriodColumns(writer, baseTable, table.getWhenCreatedColumn());
createHistoryTable(writer.applyPostAlter(), table);
@@ -82,7 +74,6 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
dropWithHistoryView(writer.dropAll(), baseTable);
dropHistoryTable(writer.dropAll(), baseTable);
// no need to dropSysPeriodColumns as whole table will be deleted soon
}
@Override
@@ -97,8 +88,7 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
dropWithHistoryView(writer.apply(), tableName);
// here are the alter commands
createWithHistoryView(writer.applyPostAlter(), tableName);
createTriggers(writer.applyPostAlter(), tableName, columnNamesForApply(table));
createTriggers(writer.applyPostAlter(), quote(tableName), columnNamesForApply(table));
alter.setHistoryHandled();
}
}
@@ -106,9 +96,6 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
/**
* Will add a history trigger to the buffer. The config
*
* @param buffer
* @param table
*/
protected abstract void createTriggers(DdlBuffer buffer, String baseTable, List<String> columnNames);
@@ -135,7 +122,6 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
}
protected void addSysPeriodColumns(DdlWrite writer, String baseTableName, String whenCreatedColumn) {
platformDdl.alterTableAddColumn(writer, baseTableName, sysPeriodStart, sysPeriodType, now);
platformDdl.alterTableAddColumn(writer, baseTableName, sysPeriodEnd, sysPeriodType, null);
if (whenCreatedColumn != null) {
@@ -176,7 +162,6 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
* Write the column definition to the create table statement.
*/
protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type) {
String platformType = platformDdl.convert(type);
buffer.append(" ");
buffer.append(quote(columnName), 29);
@@ -184,7 +169,6 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
}
protected void createWithHistoryView(DdlBuffer apply, String baseTableName) {
apply
.append("create view ").append(historyViewName(baseTableName))
.append(" as select * from ").append(quote(baseTableName))
@@ -211,7 +195,6 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
}
protected void appendInsertIntoHistory(DdlBuffer buffer, String baseTable, List<String> columns) {
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(",");
@@ -225,7 +208,7 @@ public abstract class DbTriggerBasedHistoryDdl extends DbTableBasedHistoryDdl im
buffer.append(", ");
}
buffer.append(columnPrefix);
buffer.append(columns.get(i));
buffer.append(quote(columns.get(i)));
}
}
@@ -18,19 +18,15 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
buffer.append("drop trigger ").append(deleteTriggerName(baseTable)).endOfStatement();
}
@Override
protected void createTriggers(DdlBuffer buffer, String baseTable, List<String> columnNames) {
buffer.append("lock tables ").append(baseTable).append(" write").endOfStatement();
addBeforeUpdate(buffer, updateTriggerName(baseTable), baseTable, columnNames);
addBeforeDelete(buffer, deleteTriggerName(baseTable), baseTable, columnNames);
buffer.appendStatement("unlock tables");
}
private void addBeforeUpdate(DdlBuffer apply, String triggerName, String tableName, List<String> columnNames) {
apply
.append("delimiter $$").newLine()
.append("create trigger ").append(triggerName).append(" before update on ").append(tableName)
@@ -42,7 +38,6 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
}
private void addBeforeDelete(DdlBuffer apply, String triggerName, String tableName, List<String> columnNames) {
apply
.append("delimiter $$").newLine()
.append("create trigger ").append(triggerName).append(" before delete on ").append(tableName)
@@ -302,15 +302,15 @@ create table table_history(
sys_period_end datetime(6)
);
create view table_with_history as select * from `table` union all select * from table_history;
lock tables "table" write;
lock tables `table` write;
delimiter $$
create trigger table_history_upd before update on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."foreign");
create trigger table_history_upd before update on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`foreign`);
set NEW.sys_period_start = now(6);
end$$
delimiter $$
create trigger table_history_del before delete on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."foreign");
create trigger table_history_del before delete on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`foreign`);
end$$
unlock tables;
@@ -184,15 +184,15 @@ create trigger migtest_e_history6_history_del before delete on migtest_e_history
end$$
unlock tables;
create view table_with_history as select * from `table` union all select * from table_history;
lock tables "table" write;
lock tables `table` write;
delimiter $$
create trigger table_history_upd before update on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "select", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."select", OLD."foreign");
create trigger table_history_upd before update on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `select`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`select`, OLD.`foreign`);
set NEW.sys_period_start = now(6);
end$$
delimiter $$
create trigger table_history_del before delete on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "select", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."select", OLD."foreign");
create trigger table_history_del before delete on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `select`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`select`, OLD.`foreign`);
end$$
unlock tables;
alter table `table` add constraint uq_table_select unique (`select`);
@@ -1,6 +1,6 @@
1835064798, I__create_procs.sql
-1894322966, 1.0__initial.sql
718635669, 1.1.sql
776107385, 1.0__initial.sql
-1360065766, 1.1.sql
-1097227916, 1.2__dropsFor_1.1.sql
-1972055805, 1.3.sql
379252952, 1.4__dropsFor_1.3.sql
@@ -302,15 +302,15 @@ create table table_history(
sys_period_end datetime(6)
);
create view table_with_history as select * from `table` union all select * from table_history;
lock tables "table" write;
lock tables `table` write;
delimiter $$
create trigger table_history_upd before update on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."foreign");
create trigger table_history_upd before update on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`foreign`);
set NEW.sys_period_start = now(6);
end$$
delimiter $$
create trigger table_history_del before delete on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."foreign");
create trigger table_history_del before delete on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`foreign`);
end$$
unlock tables;
@@ -184,15 +184,15 @@ create trigger migtest_e_history6_history_del before delete on migtest_e_history
end$$
unlock tables;
create view table_with_history as select * from `table` union all select * from table_history;
lock tables "table" write;
lock tables `table` write;
delimiter $$
create trigger table_history_upd before update on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "select", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."select", OLD."foreign");
create trigger table_history_upd before update on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `select`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`select`, OLD.`foreign`);
set NEW.sys_period_start = now(6);
end$$
delimiter $$
create trigger table_history_del before delete on "table" for each row begin
insert into table_history (sys_period_start,sys_period_end,"index", "from", "to", "varchar", "select", "foreign") values (OLD.sys_period_start, now(6),OLD."index", OLD."from", OLD."to", OLD."varchar", OLD."select", OLD."foreign");
create trigger table_history_del before delete on `table` for each row begin
insert into table_history (sys_period_start,sys_period_end,`index`, `from`, `to`, `varchar`, `select`, `foreign`) values (OLD.sys_period_start, now(6),OLD.`index`, OLD.`from`, OLD.`to`, OLD.`varchar`, OLD.`select`, OLD.`foreign`);
end$$
unlock tables;
alter table `table` add constraint uq_table_select unique (`select`);
@@ -1,6 +1,6 @@
1835064798, I__create_procs.sql
955526296, 1.0__initial.sql
718635669, 1.1.sql
-1717521141, 1.0__initial.sql
-1360065766, 1.1.sql
-1097227916, 1.2__dropsFor_1.1.sql
-1972055805, 1.3.sql
379252952, 1.4__dropsFor_1.3.sql