Skip to content

Commit

Permalink
Merge pull request #48 from graphfoundation/3.3-accessLastTxId
Browse files Browse the repository at this point in the history
3.3 make previous tx id accessible to triggers
  • Loading branch information
bradnussbaum authored Feb 14, 2020
2 parents 7d91e9e + 70be072 commit 0c8bc4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/apoc/trigger/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.graphdb.event.TransactionEventHandler;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.impl.core.GraphProperties;
import org.neo4j.kernel.impl.core.NodeManager;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
//import org.neo4j.procedure.Mode;
import org.neo4j.logging.Log;
import org.neo4j.procedure.*;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
Expand Down Expand Up @@ -225,6 +227,19 @@ private void executeTriggers(TransactionData txData, String phase) {
if (phase.equals( "after" ))
{
params.putAll( txDataCollector( txData, phase, (Map<String,Object>) data.get( "config" ) ) );

try
{
Field f = txData.getClass().getDeclaredField( "transaction" );
f.setAccessible( true );
KernelTransaction kernelTransaction = (KernelTransaction) f.get( txData );
params.put( "lastTxId", kernelTransaction.lastTransactionIdWhenStarted() );
f.setAccessible( false );
}
catch ( NoSuchFieldException | IllegalAccessException e )
{
log.error( "Failed to get last transaction id: " + e.getMessage() );
}
}
if( ( (Map<String,Object>) data.get( "config" )).get( "params" ) != null)
{
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/apoc/trigger/TriggerDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public void testTriggerData_TransactionId() throws Exception {
});
}

@Test
public void testTriggerData_lastTxId() {
db.execute("CALL apoc.trigger.add('test','WITH {createdNodes} AS createdNodes, {lastTxId} AS lastTxId UNWIND {createdNodes} AS n SET n.testProp = lastTxId',{phase: 'after'}, { uidKeys: ['uid'], params: {}})").close();
db.execute("CREATE (f:Foo {name:'Michael'})").close();
TestUtil.testCall(db, "MATCH (f:Foo) RETURN f", (row) -> {
assertEquals(true, ((Node)row.get("f")).hasProperty("testProp"));
assertNotEquals( "``:``:`0`", ((Node)row.get("f")).getProperty( "testProp") );
});
}

@Test
public void testTriggerData_CommitTime() throws Exception {
db.execute("CALL apoc.trigger.add('test','WITH {createdNodes} AS createdNodes, {txData} AS txData UNWIND {createdNodes} AS n SET n.testProp = txData." + COMMIT_TIME + "',{phase: 'after'}, { uidKeys: ['uid'], params: {}})").close();
Expand Down

0 comments on commit 0c8bc4a

Please sign in to comment.