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

This commit is contained in:
Rob Bygrave committed 2022-03-28 13:05:59 +13:00
1 parent c8bb3c7b57
commit 5c3b680c8a
8 files changed
+27 -49

No files matched your search

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