Skip to content

Commit

Permalink
Schema modfication "Join column" supported in creation of data migrat…
Browse files Browse the repository at this point in the history
…ion script (#104)

* Join column transformation into script supported

* Fix pattern repetitive
  • Loading branch information
suarezgpablo authored Nov 19, 2024
1 parent 3a86b2d commit c573542
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 12 deletions.
2 changes: 2 additions & 0 deletions modevo-script/dat/bmk/testCustomV8JoinColumn-script.cql
Original file line number Diff line number Diff line change
@@ -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);
16 changes: 16 additions & 0 deletions modevo-script/dat/inp/creationSchema.cql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions modevo-script/dat/inp/testCustomV8JoinColumn-initDB.cql
Original file line number Diff line number Diff line change
@@ -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', 'winds of winter');
Insert into "testCustomV8JoinColumn".table1 (idauthor, idbook, title, subtitle) values ('5', '6', 'frieren', 'exam arc');
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Column> sourceJoin;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,14 +69,45 @@ 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
}
}
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) {
List<Column> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +69,7 @@ private void executeInserts(Script script, List<ColumnValue> cvs, List<List<Colu
String nameTableInsert = i.getNameTable();
String statementInsertWithKeyspace = insertStatement.replace("INSERT INTO "+nameTableInsert, "INSERT INTO "+"\""+nameKeyspace+"\"."+nameTableInsert);
List<String> insertsInside = new ArrayList<>();
replaceJoinColumnVariables(statementInsertWithKeyspace, cvs); //For Join column schema changes
for (ColumnValue cv : cvs) {
statementInsertWithKeyspace=replaceVariableName (statementInsertWithKeyspace, cv, cvs);
}
Expand All @@ -84,6 +87,28 @@ private void executeInserts(Script script, List<ColumnValue> cvs, List<List<Colu
}
}
}
/**
* Replaces and concatenates all the insertions that come from a join column operation
*/
private void replaceJoinColumnVariables(String statementInsertWithKeyspace, List<ColumnValue> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns="DataMigration">
<MigrationTable name="table1" joincol="/1"/>
<JoinColumn ColTarget="completetitle" ColSource="title , subtitle" Criteria="criteria for the join"/>
<JoinColumn ColTarget="completetitle" ColSource="title , subtitle"/>
</xmi:XMI>
1 change: 1 addition & 0 deletions modevo-transform/dat/inp/CustomCM.xmi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Attribute xmi:id="a2" name = "id" entity='e2' isUnique="true"/>
<Attribute xmi:id="a3" name = "title" entity='e2'/>
<Attribute xmi:id="a4" name = "publisher" entity='e2'/>
<Attribute xmi:id="a5" name = "subtitle" entity='e2'/>

<Relationship xmi:id="r2" name ="relation1" entity1="e1" entity2="e2" cardinality1="n" cardinality2="m" />
</xmi:XMI>
2 changes: 1 addition & 1 deletion modevo-transform/dat/inp/testCustomV8JoinColumn-schema.xmi
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
nameAttribute="title"
nameEntity="Book"/>
<Column xmi:id="c4"
name="title"
name="subtitle"
tableColumn="t1"
nameAttribute="subtitle"
nameEntity="Book"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="SchemaEvolution" xsi:schemaLocation="SchemaEvolution ../../src/main/java/giis/modevo/transformations/atl/Metamodels/SchemaEvolution.ecore">
<JoinColumn
xmi:id="a1"
table="table1" targetColumn="c3" sourceColumns="c1 c2" criteria="criteria for the join"/>
table="table1" targetColumn="c3" sourceColumns="c1 c2"/>
<Column
xmi:id="c1"
nameAttribute="title"
nameEntity="Book"
name="title"/>
<Column
xmi:id="c2"
nameAttribute="subtitle"
nameEntity="Book"
name="subtitle"/>
<Column
xmi:id="c3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;

import giis.modevo.model.schema.Schema;
import giis.modevo.model.schemaevolution.JoinColumn;
import giis.modevo.model.schemaevolution.RemovePK;
import giis.modevo.model.schemaevolution.SchemaChange;
import giis.modevo.model.schemaevolution.SchemaEvolution;
Expand Down Expand Up @@ -137,6 +138,14 @@ public boolean migrationSplitColumn(SchemaEvolution se) {
}
return false;
}
public boolean migrationJoinColumn(SchemaEvolution se) {
for (SchemaChange sc : se.getChanges()) {
if (sc instanceof JoinColumn) {
return true;
}
}
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ lazy rule JoinColumnMigCol {
to
add: DataMigration!JoinColumn (
ColTarget <- j.targetColumn.name,
ColSource <- thisModule.getNamesColumn (j.sourceColumns),
Criteria <- j.criteria

ColSource <- thisModule.getNamesColumn (j.sourceColumns)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@
<eClassifiers xsi:type="ecore:EClass" name="JoinColumn">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="ColTarget" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="ColSource" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="Criteria" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
</ecore:EPackage>
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
<eStructuralFeatures xsi:type="ecore:EReference" name="sourceColumns" lowerBound="2"
upperBound="-1" eType="#//Column"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="table" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="criteria" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="RemovePK">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="table" lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
Expand Down

0 comments on commit c573542

Please sign in to comment.