Skip to content

Commit

Permalink
Migrate LoadCSVIT to CALL {} IN TRANSACTIONS for newer server (#1200)
Browse files Browse the repository at this point in the history
This applies to server versions above 4.4.
  • Loading branch information
injectives authored Apr 1, 2022
1 parent 350435c commit 05b50b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
28 changes: 19 additions & 9 deletions driver/src/test/java/org/neo4j/driver/integration/LoadCSVIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.util.DatabaseExtension;
import org.neo4j.driver.util.Neo4jSettings;
import org.neo4j.driver.util.ParallelizableIT;
Expand All @@ -51,15 +51,25 @@ void shouldLoadCSV() throws Throwable
String csvFileUrl = createLocalIrisData( session );

// When
Result result = session.run(
"USING PERIODIC COMMIT 40\n" +
"LOAD CSV WITH HEADERS FROM $csvFileUrl AS l\n" +
"MATCH (c:Class {name: l.class_name})\n" +
"CREATE (s:Sample {sepal_length: l.sepal_length, sepal_width: l.sepal_width, petal_length: l.petal_length, petal_width: l.petal_width})\n" +
String query = neo4j.isNeo4j44OrEarlier() ?
"USING PERIODIC COMMIT 40\n" +
"LOAD CSV WITH HEADERS FROM $csvFileUrl AS l\n" +
"MATCH (c:Class {name: l.class_name})\n" +
"CREATE (s:Sample {sepal_length: l.sepal_length, sepal_width: l.sepal_width, petal_length: l.petal_length, petal_width: l.petal_width})\n" +

"CREATE (c)<-[:HAS_CLASS]-(s) " +
"RETURN count(*) AS c"
:
"LOAD CSV WITH HEADERS FROM $csvFileUrl AS l\n" +
"CALL {\n" +
"WITH l\n" +
"MATCH (c:Class {name: l.class_name})\n" +
"CREATE (s:Sample {sepal_length: l.sepal_length, sepal_width: l.sepal_width, petal_length: l.petal_length, petal_width: l.petal_width})\n" +
"CREATE (c)<-[:HAS_CLASS]-(s)" +
"} IN TRANSACTIONS\n" +
"RETURN count(*) AS c";

"CREATE (c)<-[:HAS_CLASS]-(s) " +
"RETURN count(*) AS c",
parameters( "csvFileUrl", csvFileUrl ) );
Result result = session.run( query, parameters( "csvFileUrl", csvFileUrl ) );

// Then
assertThat( result.next().get( "c" ).asInt(), equalTo( 150 ) );
Expand Down
26 changes: 17 additions & 9 deletions driver/src/test/java/org/neo4j/driver/util/DatabaseExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,29 @@ public void stopDb()
runner.stopNeo4j();
}

public boolean isNeo4j44OrEarlier()
{
return isNeo4jVersionOrEarlier( 4, 4 );
}

public boolean isNeo4j43OrEarlier()
{
try ( Session session = driver().session() )
{
String neo4jVersion = session.readTransaction( tx -> tx.run( "CALL dbms.components() YIELD versions " +
"RETURN versions[0] AS version" ).single().get( "version" ).asString() );
String[] versions = neo4jVersion.split( "\\." );
int major = parseInt( versions[0] );
int minor = parseInt( versions[1] );
return major <= 4 && minor <= 3;
}
return isNeo4jVersionOrEarlier( 4, 3 );
}

public void dumpLogs()
{
runner.dumpDebugLog();
}

private boolean isNeo4jVersionOrEarlier( int major, int minor )
{
try ( Session session = driver().session() )
{
String neo4jVersion = session.readTransaction( tx -> tx.run( "CALL dbms.components() YIELD versions " +
"RETURN versions[0] AS version" ).single().get( "version" ).asString() );
String[] versions = neo4jVersion.split( "\\." );
return parseInt( versions[0] ) <= major && parseInt( versions[1] ) <= minor;
}
}
}

0 comments on commit 05b50b0

Please sign in to comment.