From 4455a2b21de8d8573aa0084734a21cd37df87009 Mon Sep 17 00:00:00 2001 From: suarezgpablo Date: Mon, 18 Nov 2024 16:43:38 +0100 Subject: [PATCH] Join column transformation into script supported --- .../dat/bmk/testCustomV8JoinColumn-script.cql | 2 ++ modevo-script/dat/inp/creationSchema.cql | 16 +++++++++ .../dat/inp/testCustomV8JoinColumn-initDB.cql | 4 +++ .../modevo/migration/script/ColumnValue.java | 4 ++- .../giis/modevo/migration/script/Script.java | 35 ++++++++++++++++++- .../modevo/migration/script/ScriptText.java | 10 ++++-- .../script/execution/ScriptExecution.java | 25 +++++++++++++ .../test4giis/script/TestExecutionScript.java | 4 +++ modevo-transform/dat/inp/CustomCM.xmi | 1 + .../dat/inp/testCustomV8JoinColumn-schema.xmi | 2 +- .../testCustomV8JoinColumn-schemaChange.xmi | 4 +++ .../model/datamigration/MigrationTable.java | 9 +++++ 12 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 modevo-script/dat/bmk/testCustomV8JoinColumn-script.cql create mode 100644 modevo-script/dat/inp/testCustomV8JoinColumn-initDB.cql diff --git a/modevo-script/dat/bmk/testCustomV8JoinColumn-script.cql b/modevo-script/dat/bmk/testCustomV8JoinColumn-script.cql new file mode 100644 index 0000000..c807393 --- /dev/null +++ b/modevo-script/dat/bmk/testCustomV8JoinColumn-script.cql @@ -0,0 +1,2 @@ +FOR $1,$2,$3,$4 = SELECT title, subtitle, idAuthor, idBook FROM table1 +INSERT INTO table1(completetitle, idAuthor, idBook) VALUES ($1+$2, $3, $4); diff --git a/modevo-script/dat/inp/creationSchema.cql b/modevo-script/dat/inp/creationSchema.cql index 1b7fe28..7b87c78 100644 --- a/modevo-script/dat/inp/creationSchema.cql +++ b/modevo-script/dat/inp/creationSchema.cql @@ -170,6 +170,22 @@ CREATE TABLE "testCustomV7SplitTable".table1copied2 ( PRIMARY KEY (idauthor) ); +-- Export of keyspace testCustomV8JoinColumn +CREATE KEYSPACE "testCustomV8JoinColumn" +WITH replication = { + 'class' : 'SimpleStrategy', + 'replication_factor' : 1 +}; +CREATE TABLE "testCustomV8JoinColumn".table1 ( + idauthor text, + idbook text, + title text, + subtitle text, + completetitle text, + PRIMARY KEY (idauthor, idbook) +) WITH CLUSTERING ORDER BY ( idbook ASC ); + + -- Export of keyspace testMindsV10NewTableMigrationFromOneTable CREATE KEYSPACE "testMindsV10NewTableMigrationFromOneTable" WITH durable_writes = true diff --git a/modevo-script/dat/inp/testCustomV8JoinColumn-initDB.cql b/modevo-script/dat/inp/testCustomV8JoinColumn-initDB.cql new file mode 100644 index 0000000..9b7a263 --- /dev/null +++ b/modevo-script/dat/inp/testCustomV8JoinColumn-initDB.cql @@ -0,0 +1,4 @@ +TRUNCATE "testCustomV8JoinColumn".table1; +Insert into "testCustomV8JoinColumn".table1 (idauthor, idbook, title, subtitle) values ('1', '2', 'lotr', 'rotk'); +Insert into "testCustomV8JoinColumn".table1 (idauthor, idbook, title, subtitle) values ('3', '4', 'asoiaf', 'agot'); +Insert into "testCustomV8JoinColumn".table1 (idauthor, idbook, title, subtitle) values ('5', '6', 'frieren', 'exam arc'); \ No newline at end of file diff --git a/modevo-script/src/main/java/giis/modevo/migration/script/ColumnValue.java b/modevo-script/src/main/java/giis/modevo/migration/script/ColumnValue.java index cc4bf44..0a9fe52 100644 --- a/modevo-script/src/main/java/giis/modevo/migration/script/ColumnValue.java +++ b/modevo-script/src/main/java/giis/modevo/migration/script/ColumnValue.java @@ -1,5 +1,7 @@ package giis.modevo.migration.script; +import java.util.List; + import giis.modevo.model.schema.Column; import lombok.Getter; import lombok.Setter; @@ -10,5 +12,5 @@ public class ColumnValue { private String value; //String generic type, actual type in DB could be different private String variableName; private Column columnSelectOrigin; - + private List sourceJoin; } 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 e749be9..186abe5 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 @@ -14,6 +14,7 @@ import giis.modevo.model.schema.Schema; import giis.modevo.model.schema.Table; import giis.modevo.model.schemaevolution.CriteriaSplit; +import giis.modevo.model.schemaevolution.JoinColumn; import giis.modevo.model.schemaevolution.SchemaChange; import giis.modevo.model.schemaevolution.SchemaEvolution; import giis.modevo.model.schemaevolution.SplitColumn; @@ -68,6 +69,9 @@ else if (mt.migrationNewColumn(schema) && !mt.migrationFromRemovePK(se, mt)) { else if (mt.migrationSplitColumn(se)) { scripts.add(migrationSplitColumn (schema, se, mt)); } + else if (mt.migrationJoinColumn(se)) { + scripts.add(migrationJoinColumn (schema, se, mt)); + } else { return new ArrayList<>(); //Scenarios not implemented } @@ -75,7 +79,36 @@ else if (mt.migrationSplitColumn(se)) { checkIfExecutable(scripts); return scripts; } - + private Script migrationJoinColumn(Schema schema, SchemaEvolution se, MigrationTable mt) { + log.info("Join Column Script. Target table: %s", mt.getName()); + Script script = new Script (); + for (SchemaChange sc : se.getChanges()) { + if (sc instanceof JoinColumn spc) { + String separator = spc.getCriteria(); + List sourceColumns = spc.getSourceColumns(); + Column newColumn = spc.getC(); + Table t = schema.getTable(mt.getName()); + t.getKey(); + For forJoin = new For (); + Select s = new Select (); + forJoin.getSelectsFor().add(s); + s.setTable(t); + s.getSearch().addAll(sourceColumns); + Insert insert = new Insert(schema.getTable(mt.getName()), forJoin); + ColumnValue cv = insert.addColumnValue (newColumn, s, null, newColumn); + cv.setSourceJoin(sourceColumns); + for (Column c: t.getKey()) { + ColumnValue cvSelect=insert.addColumnValue (c, s, null, c); + Column copyTarget = new Column (c); + s.getSearch().add(copyTarget); + cvSelect.setColumn(copyTarget); + } + script.addForSelectInsert(forJoin, s, insert); + script.getForsHigherLevel().add(forJoin); + } + } + return script; + } /** * Creates the script needed when a column is splitted in two or more columns. */ diff --git a/modevo-script/src/main/java/giis/modevo/migration/script/ScriptText.java b/modevo-script/src/main/java/giis/modevo/migration/script/ScriptText.java index 97f90df..0c87207 100644 --- a/modevo-script/src/main/java/giis/modevo/migration/script/ScriptText.java +++ b/modevo-script/src/main/java/giis/modevo/migration/script/ScriptText.java @@ -62,11 +62,17 @@ private void writeSyntaxInsert(StringBuilder sb, Script s) { String nameColumn = cv.getColumn().getName(); Select selectOrigin = s.findSelect (cv); String nameVariable=selectOrigin.findNameVariable (cv.getColumn().getNameAttribute(), cv.getColumn().getNameEntity()); - if (nameVariable == null) { + namesColumns.append(nameColumn); + if (cv.getSourceJoin() != null) { + Column c1 = cv.getSourceJoin().get(0); + Column c2 = cv.getSourceJoin().get(1); + insertPlaceholders.append(c1.getVariableName()+ "+" + c2.getVariableName()); + continue; + } + else if (nameVariable == null) { Column columnOrigin = selectOrigin.getSplitColumn(); nameVariable=columnOrigin.getVariableName(); } - namesColumns.append(nameColumn); insertPlaceholders.append(nameVariable); } insertSB.append(namesColumns).append(") VALUES (").append(insertPlaceholders).append(");\n"); diff --git a/modevo-script/src/main/java/giis/modevo/migration/script/execution/ScriptExecution.java b/modevo-script/src/main/java/giis/modevo/migration/script/execution/ScriptExecution.java index abf2ac3..ddb9637 100644 --- a/modevo-script/src/main/java/giis/modevo/migration/script/execution/ScriptExecution.java +++ b/modevo-script/src/main/java/giis/modevo/migration/script/execution/ScriptExecution.java @@ -3,6 +3,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.datastax.oss.driver.api.core.cql.ColumnDefinition; import com.datastax.oss.driver.api.core.cql.ColumnDefinitions; @@ -67,6 +69,7 @@ private void executeInserts(Script script, List cvs, List insertsInside = new ArrayList<>(); + replaceJoinColumnVariables(statementInsertWithKeyspace, cvs); //For Join column schema changes for (ColumnValue cv : cvs) { statementInsertWithKeyspace=replaceVariableName (statementInsertWithKeyspace, cv, cvs); } @@ -84,6 +87,28 @@ private void executeInserts(Script script, List cvs, List cvs) { + Pattern pattern = Pattern.compile("\\$(\\d+)(\\+\\$(\\d+))*"); //Obtains all the joins that exist + Matcher matcher = pattern.matcher(statementInsertWithKeyspace); + while (matcher.find()) { + String match = matcher.group(); + Pattern patternSingleVariable = Pattern.compile("\\$(\\d+)"); //Obtains each source value to be joined + Matcher matcherSingleVariable = patternSingleVariable.matcher(match); + StringBuilder sb = new StringBuilder(); + while (matcherSingleVariable.find()) { + String variableName = matcherSingleVariable.group(); + for (ColumnValue cv : cvs) { + if (cv.getVariableName().equals(variableName)) { + sb.append(cv.getValue()); + } + } + } + statementInsertWithKeyspace=statementInsertWithKeyspace.replace(match, sb.toString()); + } + } /** * Creates a list of statements with its values replaced by the values included in the list. */ diff --git a/modevo-script/src/test/java/test4giis/script/TestExecutionScript.java b/modevo-script/src/test/java/test4giis/script/TestExecutionScript.java index 81c640c..7e625a4 100644 --- a/modevo-script/src/test/java/test4giis/script/TestExecutionScript.java +++ b/modevo-script/src/test/java/test4giis/script/TestExecutionScript.java @@ -128,6 +128,10 @@ public void testCustomV6CopyTable () { public void testCustomV7SplitTable () { testScript(name.getMethodName(), connection); } + @Test + public void testCustomV8JoinColumn () { + testScript(name.getMethodName(), connection); + } public static void executeCQLFile(String path){ try{ // Open the file that is the first diff --git a/modevo-transform/dat/inp/CustomCM.xmi b/modevo-transform/dat/inp/CustomCM.xmi index 0d1817c..1393d3a 100644 --- a/modevo-transform/dat/inp/CustomCM.xmi +++ b/modevo-transform/dat/inp/CustomCM.xmi @@ -8,6 +8,7 @@ + \ No newline at end of file diff --git a/modevo-transform/dat/inp/testCustomV8JoinColumn-schema.xmi b/modevo-transform/dat/inp/testCustomV8JoinColumn-schema.xmi index 34673d4..56ae546 100644 --- a/modevo-transform/dat/inp/testCustomV8JoinColumn-schema.xmi +++ b/modevo-transform/dat/inp/testCustomV8JoinColumn-schema.xmi @@ -21,7 +21,7 @@ nameAttribute="title" nameEntity="Book"/> diff --git a/modevo-transform/dat/inp/testCustomV8JoinColumn-schemaChange.xmi b/modevo-transform/dat/inp/testCustomV8JoinColumn-schemaChange.xmi index b615f71..ee53435 100644 --- a/modevo-transform/dat/inp/testCustomV8JoinColumn-schemaChange.xmi +++ b/modevo-transform/dat/inp/testCustomV8JoinColumn-schemaChange.xmi @@ -5,9 +5,13 @@ table="table1" targetColumn="c3" sourceColumns="c1 c2" criteria="criteria for the join"/>