Skip to content

Commit

Permalink
Migrate LoadCSVIT to CALL {} IN TRANSACTIONS for newer server
Browse files Browse the repository at this point in the history
This applies to server versions above 4.4.
  • Loading branch information
injectives committed Mar 31, 2022
1 parent 350435c commit e6aafc1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,25 @@ public void stopDb()
runner.stopNeo4j();
}

public boolean isNeo4j44OrEarlier()
{
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 <= 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() );
"RETURN versions[0] AS version" ).single().get( "version" ).asString() );
String[] versions = neo4jVersion.split( "\\." );
int major = parseInt( versions[0] );
int minor = parseInt( versions[1] );
Expand Down

0 comments on commit e6aafc1

Please sign in to comment.