Skip to content

Commit

Permalink
Added script creation and execution for the split of an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
suarezgpablo committed Oct 3, 2024
1 parent f751cf0 commit d94fccf
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 23 deletions.
6 changes: 6 additions & 0 deletions modevo-script/dat/bmk/testMindsV3SplitColumn-script.cql
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 4 additions & 0 deletions modevo-script/dat/inp/testMindsV3SplitColumn-initDB.cql
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,11 +36,13 @@ public class Script {
private List<Insert> inserts;
private ScriptText scriptText;
private boolean executable;
private List<For> 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();
}
Expand All @@ -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
}
Expand All @@ -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<CriteriaSplit> 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<Script> scripts) {
for (Script s: scripts) {
for (Insert i: s.getInserts()) {
Expand Down Expand Up @@ -261,7 +305,7 @@ private Select insertSelect(Column c, Table from, String[] keyColumnFrom, Schema

public boolean inList(Column c, List<ColumnValue> columnValues) {
for (ColumnValue cv : columnValues) {
if (cv.getColumn().equals(c)) {
if (cv.getColumn().equalsValues(c)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ public String writeScript(Script s) {
return "Not executable script because there are key columns that cannot be inserted";
}
StringBuilder sb = new StringBuilder();
For forNested = s.getFors().get(0);
while (forNested != null) {
writeSyntaxFor (sb, forNested, s);
forNested = forNested.getNestedFor();
if (s.getForsSplit().isEmpty()) {
For forNested = s.getFors().get(0);
while (forNested != null) {
writeSyntaxFor (sb, forNested, s);
forNested = forNested.getNestedFor();
}
}
else { //When there is a split
List<For> forsSplit = s.getForsSplit();
for (For forSplit:forsSplit) {
writeSyntaxFor (sb, forSplit, s);
}
}
writeSyntaxInsert (sb, s);
log.info("Script screated");
Expand Down Expand Up @@ -53,8 +61,16 @@ private void writeSyntaxInsert(StringBuilder sb, Script s) {
if (cv.getVariableName()==null) {
cv.setVariableName(cv.getColumn().getVariableName());
}
if (cv.getVariableName()==null) {
cv.setVariableName(cv.getColumnSelectOrigin().getVariableName());
}
String nameColumn = cv.getColumn().getName();
String nameVariable=cv.getSelectOrigin().findNameVariable (cv.getColumn().getNameAttribute(), cv.getColumn().getNameEntity());
if (nameVariable == null) {
Column columnOrigin = cv.getSelectOrigin().getSplitColumn();
nameVariable=columnOrigin.getVariableName();

}
namesColumns.append(nameColumn);
insertPlaceholders.append(nameVariable);
}
Expand Down Expand Up @@ -99,6 +115,9 @@ private String buildSelect(Select s, StringBuilder sb, Script se) {
counterVariables++;
}
columns.append(" FROM ").append(s.getTable().getName());
if (!se.getForsSplit().isEmpty()) {
textCriteriaSplit (s, columns);
}
for (int j=0; j<s.getWhere().size();j++) {
if (j==0) {
columns.append(" WHERE ");
Expand All @@ -121,6 +140,31 @@ private String buildSelect(Select s, StringBuilder sb, Script se) {

}

private void textCriteriaSplit(Select s, StringBuilder columns) {
columns.append(" WHERE ");
String nameColumn = s.getSplitColumn().getName();
columns.append(nameColumn);
String criteriaOperator = s.getCriteriaOperator();
switch (criteriaOperator) {
case "eq":
columns.append("=");
break;
case "g":
columns.append(">");
break;
case "l":
columns.append("<");
break;
case "ge":
columns.append(">=");
break;
case "le":
columns.append("<=");
break;
}
columns.append("'"+s.getCriteriaValue()+"'");
columns.append(" ALLOW FILTERING");
}
private String getNameVariableColumn(Column ce, Script s) {
String nameAttribute = ce.getNameAttribute();
String nameEntity = ce.getNameEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class Select {
private List<ColumnValue> whereValue;
private List<Column> where;
private String selectStatement;
private String criteriaOperator;
private String criteriaValue;
private Column splitColumn;
private For insideFor; //when the SELECT is inside a FOR
private For loopFor; //when the FOR iterates over the SELECT results

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,28 @@ public void execute(Script script, CassandraConnection c, String nameKeyspace) {
return;
}
List<For> highLevelFors = script.getForsHigherLevel();
List<For> splitFors = script.getForsSplit();
for (For highFor : highLevelFors) {
List<Select> highLevelSelects = highFor.getSelectsFor();
Select s = highLevelSelects.get(0); //For now there is only one select at most, change into a list when there will be more.
String nameTable = s.getTable().getName();
String selectStatementWithKeyspace = s.getSelectStatement().replace(FROM+nameTable,FROM+ "\""+nameKeyspace+"\"."+nameTable);
ResultSet rs = c.executeStatement(selectStatementWithKeyspace);
Iterator<Row> resultIt = rs.iterator();
while (resultIt.hasNext()) {
Row rw = resultIt.next();
List<ColumnValue> cvs = getColumnValuesSearchRow(rw, s);
List<Select> selectsInside = highFor.getSelectsInsideFor();
List<List<ColumnValue>> cvsFromWhere = executeSelects (cvs, selectsInside, c, nameKeyspace);
executeInserts (script, cvs, cvsFromWhere, highFor, c, nameKeyspace);
}
executionFor (highFor, script, c, nameKeyspace);
}
for (For splitFor : splitFors) {
executionFor (splitFor, script, c, nameKeyspace);
}

}
private void executionFor (For forToExecute, Script script, CassandraConnection c, String nameKeyspace) {
List<Select> highLevelSelects = forToExecute.getSelectsFor();
Select s = highLevelSelects.get(0); //For now there is only one select at most, change into a list when there will be more.
String nameTable = s.getTable().getName();
String selectStatementWithKeyspace = s.getSelectStatement().replace(FROM+nameTable,FROM+ "\""+nameKeyspace+"\"."+nameTable);
ResultSet rs = c.executeStatement(selectStatementWithKeyspace);
Iterator<Row> resultIt = rs.iterator();
while (resultIt.hasNext()) {
Row rw = resultIt.next();
List<ColumnValue> cvs = getColumnValuesSearchRow(rw, s);
List<Select> selectsInside = forToExecute.getSelectsInsideFor();
List<List<ColumnValue>> cvsFromWhere = executeSelects (cvs, selectsInside, c, nameKeyspace);
executeInserts (script, cvs, cvsFromWhere, forToExecute, c, nameKeyspace);
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public void testThingsBoardV13NewColumnKeyInTable() {
testScript(name.getMethodName(), connection);
}
@Test
public void testMindsV3SplitColumn () {
testScript(name.getMethodName(), connection);
}
@Test
public void testMindsV9NewTableMigrationFromOneTable () {
testScript(name.getMethodName(), connection);

Check failure on line 69 in modevo-script/src/test/java/test4giis/script/TestExecutionScript.java

View workflow job for this annotation

GitHub Actions / test-result-script

TestExecutionScript.testMindsV9NewTableMigrationFromOneTable

Cannot invoke "Object.equals(Object)" because the return value of "giis.modevo.model.schema.Column.getTable()" is null
Raw output
java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because the return value of "giis.modevo.model.schema.Column.getTable()" is null
	at giis.modevo.model.schema.Column.equalsValues(Column.java:57)
	at giis.modevo.migration.script.Script.inList(Script.java:308)
	at giis.modevo.migration.script.Script.checkIfExecutable(Script.java:122)
	at giis.modevo.migration.script.Script.createScript(Script.java:77)
	at giis.modevo.migration.script.MainScript.createScriptAndText(MainScript.java:19)
	at test4giis.script.TestExecutionScript.createExecuteScript(TestExecutionScript.java:158)
	at test4giis.script.TestExecutionScript.testScript(TestExecutionScript.java:154)
	at test4giis.script.TestExecutionScript.testMindsV9NewTableMigrationFromOneTable(TestExecutionScript.java:69)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:316)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:240)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:214)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:155)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
	at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
xmi:id="cs1"
column="c1"
value="f"
operator="gt"/>
operator="g"/>
<CriteriaScplit
xmi:id="cs2"
column="c2"
value="p"
operator="gt"/>
operator="g"/>
<CriteriaScplit
xmi:id="cs3"
column="c3"
value="p"
operator="lt"/>
operator="l"/>
</xmi:XMI>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import giis.modevo.model.schemaevolution.RemovePK;
import giis.modevo.model.schemaevolution.SchemaChange;
import giis.modevo.model.schemaevolution.SchemaEvolution;
import giis.modevo.model.schemaevolution.SplitColumn;
import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -125,6 +126,17 @@ public boolean migrationFromRemovePK(SchemaEvolution se, MigrationTable mt) {
}
return false;
}
/**
* Checks if the migration script contains a split of a column
*/
public boolean migrationSplitColumn(SchemaEvolution se) {
for (SchemaChange sc : se.getChanges()) {
if (sc instanceof SplitColumn) {
return true;
}
}
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ public Column(Column c) {
this.ck = c.ck;
this.setNameAttribute(c.nameAttribute);
this.table = c.table;
this.dataType = c.dataType;
this.nameEntity = c.nameEntity;
this.variableName = c.variableName;

}

public Column(String nameTable) {
this.name = nameTable;
}

public boolean equalsValues (Column c) {
if (c.getName().equals(this.getName()) && c.getTable().equals(this.getTable()) && c.getNameAttribute().equals(this.getNameAttribute()) && c.getNameEntity().equals(this.getNameEntity())) {
return true;
}
return false;
}

}

0 comments on commit d94fccf

Please sign in to comment.