From 92dfe0d1fd3614faf3f7f7bb76bf4c85ab54ac98 Mon Sep 17 00:00:00 2001 From: suarezgpablo Date: Thu, 3 Oct 2024 11:40:08 +0200 Subject: [PATCH] Added script creation and execution for the split of an attribute --- .../dat/bmk/testMindsV3SplitColumn-script.cql | 6 +++ .../dat/inp/testMindsV3SplitColumn-initDB.cql | 4 ++ .../giis/modevo/migration/script/Insert.java | 7 ++- .../giis/modevo/migration/script/Script.java | 46 +++++++++++++++- .../modevo/migration/script/ScriptText.java | 52 +++++++++++++++++-- .../giis/modevo/migration/script/Select.java | 3 ++ .../script/execution/ScriptExecution.java | 34 +++++++----- .../test4giis/script/TestExecutionScript.java | 4 ++ .../testMindsV3SplitColumn-schemaChange.xmi | 6 +-- .../model/datamigration/MigrationTable.java | 12 +++++ .../java/giis/modevo/model/schema/Column.java | 10 ++++ .../java/giis/modevo/model/schema/Table.java | 3 ++ .../model/schemaevolution/AddColumn.java | 2 +- .../model/schemaevolution/JoinColumn.java | 4 +- .../model/schemaevolution/SchemaChange.java | 5 +- .../model/schemaevolution/SplitColumn.java | 4 +- 16 files changed, 172 insertions(+), 30 deletions(-) create mode 100644 modevo-script/dat/bmk/testMindsV3SplitColumn-script.cql create mode 100644 modevo-script/dat/inp/testMindsV3SplitColumn-initDB.cql diff --git a/modevo-script/dat/bmk/testMindsV3SplitColumn-script.cql b/modevo-script/dat/bmk/testMindsV3SplitColumn-script.cql new file mode 100644 index 0000000..a4ff92a --- /dev/null +++ b/modevo-script/dat/bmk/testMindsV3SplitColumn-script.cql @@ -0,0 +1,6 @@ +FOR $1,$2 = SELECT parent_guid, id FROM comments WHERE parent_guid>'f' ALLOW FILTERING +FOR $3,$4 = SELECT parent_guid, id FROM comments WHERE parent_guid>'p' ALLOW FILTERING +FOR $5,$6 = SELECT parent_guid, id FROM comments WHERE parent_guid<'p' ALLOW FILTERING +INSERT INTO comments(parent_guid_c1, id) VALUES ($1, $2); +INSERT INTO comments(parent_guid_c2, id) VALUES ($3, $4); +INSERT INTO comments(parent_guid_c3, id) VALUES ($5, $6); diff --git a/modevo-script/dat/inp/testMindsV3SplitColumn-initDB.cql b/modevo-script/dat/inp/testMindsV3SplitColumn-initDB.cql new file mode 100644 index 0000000..6279173 --- /dev/null +++ b/modevo-script/dat/inp/testMindsV3SplitColumn-initDB.cql @@ -0,0 +1,4 @@ +TRUNCATE "testMindsV3SplitColumn".comments; +Insert into "testMindsV3SplitColumn".comments (id, parent_guid) values ('1', '100'); +Insert into "testMindsV3SplitColumn".comments (id, parent_guid) values ('2', '200'); +Insert into "testMindsV3SplitColumn".comments (id, parent_guid) values ('3', '300'); \ No newline at end of file diff --git a/modevo-script/src/main/java/giis/modevo/migration/script/Insert.java b/modevo-script/src/main/java/giis/modevo/migration/script/Insert.java index 40df6b2..08b8b4d 100644 --- a/modevo-script/src/main/java/giis/modevo/migration/script/Insert.java +++ b/modevo-script/src/main/java/giis/modevo/migration/script/Insert.java @@ -16,9 +16,11 @@ public class Insert { private String nameNewTable; //Only used when a new table is required when adding a new column public Insert() { columnValue = new ArrayList<>(); + nameNewTable = ""; } public Insert(Table table) { this(); + this.table = table; } public Insert(Table table, For insideFor) { @@ -31,13 +33,14 @@ public Insert(Table table, For insideFor) { * @param key If there is no key, value is null * @param target */ - public void addColumnValue(Column columnSelect, Select s, String[] key, Column target) { + public ColumnValue addColumnValue(Column columnSelect, Select s, String[] key, Column target) { ColumnValue cv = new ColumnValue (); cv.setColumn(target); cv.setSelectOrigin(s); cv.setKey(key); cv.setColumnSelectOrigin(columnSelect); - this.getColumnValue().add(cv); + this.getColumnValue().add(cv); + return cv; } public String getNameNewTable() { return nameNewTable; diff --git a/modevo-script/src/main/java/giis/modevo/migration/script/Script.java b/modevo-script/src/main/java/giis/modevo/migration/script/Script.java index c087a4c..8431e0d 100644 --- a/modevo-script/src/main/java/giis/modevo/migration/script/Script.java +++ b/modevo-script/src/main/java/giis/modevo/migration/script/Script.java @@ -13,7 +13,10 @@ import giis.modevo.model.schema.Column; import giis.modevo.model.schema.Schema; import giis.modevo.model.schema.Table; +import giis.modevo.model.schemaevolution.CriteriaSplit; +import giis.modevo.model.schemaevolution.SchemaChange; import giis.modevo.model.schemaevolution.SchemaEvolution; +import giis.modevo.model.schemaevolution.SplitColumn; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @@ -33,11 +36,13 @@ public class Script { private List inserts; private ScriptText scriptText; private boolean executable; + private List forsSplit; public Script () { setExecutable(true);//default value selects = new ArrayList<>(); fors = new ArrayList<>(); inserts = new ArrayList<>(); + forsSplit = new ArrayList<>(); setForsHigherLevel(new ArrayList<>()); scriptText = new ScriptText(); } @@ -62,6 +67,9 @@ else if (mt.migrationFromOneTableNewTable(schema)) { else if (mt.migrationNewColumn(schema) && !mt.migrationFromRemovePK(se, mt)) { scripts.add(migrationNewColumn (schema, se, mt)); } + else if (mt.migrationSplitColumn(se)) { + scripts.add(migrationSplitColumn (schema, se, mt)); + } else { return new ArrayList<>(); //Scenarios not implemented } @@ -70,6 +78,42 @@ else if (mt.migrationNewColumn(schema) && !mt.migrationFromRemovePK(se, mt)) { return scripts; } + private Script migrationSplitColumn(Schema schema, SchemaEvolution se, MigrationTable mt) { + log.info("Split Column Script. Target table: %s", mt.getName()); + Script script = new Script (); + for (SchemaChange sc : se.getChanges()) { + if (sc instanceof SplitColumn spc) { + List critSplits = spc.getCs(); + String oldColumn = spc.getOldColumn(); + Table t = schema.getTable(mt.getName()); + Column oldColumnObject = t.getColumn(oldColumn); + for (CriteriaSplit critSplit : critSplits) { + For forSplit = new For (); + Select s = new Select (forSplit); + s.setTable(t); + Column copyOldColumnObject = new Column (oldColumnObject); + + s.getSearch().add(copyOldColumnObject); + s.setSplitColumn(copyOldColumnObject); + forSplit.getSelectsFor().add(s); + s.setCriteriaOperator(critSplit.getOperator()); + s.setCriteriaValue(critSplit.getValue()); + Insert insert = new Insert(schema.getTable(mt.getName()), forSplit); + insert.addColumnValue (copyOldColumnObject, s, null, critSplit.getColumn()); + for (Column c: t.getKey()) { + ColumnValue cv=insert.addColumnValue (c, s, null, c); + Column copyTarget = new Column (c); + cv.getSelectOrigin().getSearch().add(copyTarget); + cv.setColumn(copyTarget); + + } + script.getForsSplit().add(forSplit); + script.addForSelectInsert(forSplit, s, insert); + } + } + } + return script; + } private void checkIfExecutable(List