Skip to content

Commit

Permalink
feat: add state to ddl as well
Browse files Browse the repository at this point in the history
  • Loading branch information
kellertobias committed Nov 29, 2023
1 parent ae963fd commit 4f923bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/zendesk/maxwell/scripting/Scripting.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public Scripting(String filename) throws IOException, ScriptException, NoSuchMet

public void invoke(RowMap row) {
if ( row instanceof HeartbeatRowMap && processHeartbeatFunc != null )
processHeartbeatFunc.call(null, new WrappedHeartbeatMap((HeartbeatRowMap) row));
processHeartbeatFunc.call(null, new WrappedHeartbeatMap((HeartbeatRowMap) row), globalJavascriptState);
else if ( row instanceof DDLMap && processDDLFunc != null )
processDDLFunc.call(null, new WrappedDDLMap((DDLMap) row));
processDDLFunc.call(null, new WrappedDDLMap((DDLMap) row), globalJavascriptState);
else if ( row instanceof RowMap && processRowFunc != null )
processRowFunc.call(null, new WrappedRowMap(row), globalJavascriptState);
}
Expand Down
33 changes: 31 additions & 2 deletions src/test/java/com/zendesk/maxwell/scripting/ScriptingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.zendesk.maxwell.replication.Position;
import com.zendesk.maxwell.row.RowMap;
import com.zendesk.maxwell.schema.ddl.DDLMap;
import com.zendesk.maxwell.schema.ddl.ResolvedTableAlter;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand All @@ -26,7 +28,7 @@ private <T> T getPrivateFieldOrFail(Object obj, String fieldName) throws Excepti
@Test
public void TestScripting() throws Exception {
// Write a simple scripting file
Scripting scripting = new Scripting("src/test/resources/scripting/test.js");
Scripting scripting = new Scripting("src/test/resources/scripting/test-set-state.js");

// String type, String database, String table, Long timestampMillis, List<String> pkColumns, Position position, Position nextPosition, String rowQuery
RowMap row = new RowMap(
Expand All @@ -48,10 +50,37 @@ public void TestScripting() throws Exception {
assertEquals(globalJavascriptState.get("mykey"), "myvalue");
}

@Test
public void TestScriptingDDL() throws Exception {
// Write a simple scripting file
Scripting scripting = new Scripting("src/test/resources/scripting/test-set-state.js");

// String type, String database, String table, Long timestampMillis, List<String> pkColumns, Position position, Position nextPosition, String rowQuery
RowMap row = new DDLMap(
new ResolvedTableAlter(),
123L,
"INSERT INTO mytable VALUES (1, 2, 3)",
new Position(null, 0),
new Position(null, 0),
13L
);


// Access the private globalJavascriptState field
LinkedHashMap<String, Object> globalJavascriptState =
getPrivateFieldOrFail(scripting, "globalJavascriptState");

globalJavascriptState.put("number", "1");

scripting.invoke(row);

assertEquals(globalJavascriptState.get("number"), "2");
}

@Test
public void DontFailIfScriptHasNoStateParameter() throws Exception {
// Write a simple scripting file
Scripting scripting = new Scripting("src/test/resources/scripting/test-no-state.js");
Scripting scripting = new Scripting("src/test/resources/scripting/test-no-state-suppress-row.js");

// String type, String database, String table, Long timestampMillis, List<String> pkColumns, Position position, Position nextPosition, String rowQuery
RowMap row = new RowMap(
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/scripting/test-set-state.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function process_row(r, state) {
state.put("mykey", "myvalue");
}

function process_ddl(ddl, state) {
var num = parseInt(state.get("number"));
state.put("number", Number(num + 1).toFixed(0));
}

0 comments on commit 4f923bf

Please sign in to comment.