Skip to content

Commit 4b02848

Browse files
committed
wrap normal run statements in actual one off transactions
references #64
1 parent e14d10a commit 4b02848

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ services:
2424
- readreplica1
2525
expose:
2626
- 9000
27+
env_file:
28+
- .env
2729
neo4j:
2830
networks:
2931
- neo4j
@@ -42,6 +44,9 @@ services:
4244
environment:
4345
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
4446
- NEO4J_AUTH=neo4j/test
47+
- NEO4J_dbms_security_allow__csv__import__from__file__urls=true
48+
volumes:
49+
- ./tests/resources:/import
4550
core1:
4651
image: neo4j:4.3-enterprise
4752
healthcheck:
@@ -60,6 +65,8 @@ services:
6065
ports:
6166
- "7475:7474"
6267
- "7688:7687"
68+
volumes:
69+
- ./tests/resources:/import
6370
environment:
6471
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
6572
- NEO4J_AUTH=neo4j/test
@@ -70,6 +77,7 @@ services:
7077
- NEO4J_causalClustering_initialDiscoveryMembers=core1:5000,core2:5000,core3:5000
7178
- NEO4J_causal__clustering_disable__middleware__logging=false
7279
- NEO4J_dbms_connectors_default__advertised__address=core1
80+
- NEO4J_dbms_security_allow__csv__import__from__file__urls=true
7381
core2:
7482
image: neo4j:4.3-enterprise
7583
healthcheck:
@@ -94,6 +102,9 @@ services:
94102
- NEO4J_causalClustering_raftAdvertisedAddress=core2:7000
95103
- NEO4J_causalClustering_initialDiscoveryMembers=core1:5000,core2:5000,core3:5000
96104
- NEO4J_dbms_connectors_default__advertised__address=core2
105+
- NEO4J_dbms_security_allow__csv__import__from__file__urls=true
106+
volumes:
107+
- ./tests/resources:/import
97108

98109
core3:
99110
image: neo4j:4.3-enterprise
@@ -119,6 +130,9 @@ services:
119130
- NEO4J_causalClustering_raftAdvertisedAddress=core3:7000
120131
- NEO4J_causalClustering_initialDiscoveryMembers=core1:5000,core2:5000,core3:5000
121132
- NEO4J_dbms_connectors_default__advertised__address=core3
133+
- NEO4J_dbms_security_allow__csv__import__from__file__urls=true
134+
volumes:
135+
- ./tests/resources:/import
122136

123137
readreplica1:
124138
image: neo4j:4.3-enterprise
@@ -144,3 +158,6 @@ services:
144158
- NEO4J_causalClustering_raftAdvertisedAddress=readreplica1:7000
145159
- NEO4J_causalClustering_initialDiscoveryMembers=core1:5000,core2:5000,core3:5000
146160
- NEO4J_dbms_connectors_default__advertised__address=readreplica1
161+
- NEO4J_dbms_security_allow__csv__import__from__file__urls=true
162+
volumes:
163+
- ./tests/resources:/import

src/Bolt/Session.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
2424
use Laudis\Neo4j\Contracts\FormatterInterface;
2525
use Laudis\Neo4j\Contracts\SessionInterface;
26+
use Laudis\Neo4j\Contracts\TransactionInterface;
2627
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
2728
use Laudis\Neo4j\Databags\Neo4jError;
2829
use Laudis\Neo4j\Databags\SessionConfiguration;
@@ -70,7 +71,7 @@ public function __construct(
7071

7172
public function runStatements(iterable $statements, ?TransactionConfiguration $config = null): CypherList
7273
{
73-
return $this->openTransaction()->commit($statements);
74+
return $this->beginInstantTransaction()->runStatements($statements);
7475
}
7576

7677
public function openTransaction(iterable $statements = null, ?TransactionConfiguration $config = null): UnmanagedTransactionInterface
@@ -110,8 +111,7 @@ public function transaction(callable $tsxHandler, ?TransactionConfiguration $con
110111
public function beginTransaction(?iterable $statements = null, ?TransactionConfiguration $config = null): UnmanagedTransactionInterface
111112
{
112113
try {
113-
$bolt = new Bolt($this->pool->acquire($this->uri, $this->config->getAccessMode(), $this->auth));
114-
$this->auth->authenticateBolt($bolt, $this->uri, $this->userAgent);
114+
$bolt = $this->acquireBolt();
115115

116116
$begin = $bolt->begin(['db' => $this->config->getDatabase()]);
117117

@@ -131,4 +131,20 @@ public function beginTransaction(?iterable $statements = null, ?TransactionConfi
131131

132132
return $tsx;
133133
}
134+
135+
/**
136+
* @return UnmanagedTransactionInterface<T>
137+
*/
138+
private function beginInstantTransaction(): TransactionInterface
139+
{
140+
return new BoltUnmanagedTransaction($this->config->getDatabase(), $this->formatter, $this->acquireBolt());
141+
}
142+
143+
private function acquireBolt(): Bolt
144+
{
145+
$bolt = new Bolt($this->pool->acquire($this->uri, $this->config->getAccessMode(), $this->auth));
146+
$this->auth->authenticateBolt($bolt, $this->uri, $this->userAgent);
147+
148+
return $bolt;
149+
}
134150
}

tests/Integration/BoltDriverIntegrationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace Laudis\Neo4j\Tests\Integration;
1515

16+
use Bolt\error\ConnectException;
1617
use Exception;
1718
use Laudis\Neo4j\Bolt\BoltDriver;
18-
use Laudis\Neo4j\Exception\Neo4jException;
1919
use PHPUnit\Framework\TestCase;
2020

2121
final class BoltDriverIntegrationTest extends TestCase
@@ -49,7 +49,7 @@ public function testValidUrl(): void
4949
public function testInvalidIp(): void
5050
{
5151
$driver = BoltDriver::create('bolt://neo4j:[email protected]');
52-
$this->expectException(Neo4jException::class);
52+
$this->expectException(ConnectException::class);
5353
$driver->createSession()->run('RETURN 1');
5454
}
5555

@@ -59,7 +59,7 @@ public function testInvalidIp(): void
5959
public function testInvalidSocket(): void
6060
{
6161
$driver = BoltDriver::create('bolt://neo4j:[email protected]');
62-
$this->expectException(Neo4jException::class);
62+
$this->expectException(ConnectException::class);
6363
$driver->createSession()->run('RETURN 1');
6464
}
6565
}

tests/Integration/ComplexQueryTests.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Laudis\Neo4j\Tests\Integration;
1515

1616
use Generator;
17+
use function getenv;
1718
use InvalidArgumentException;
1819
use Laudis\Neo4j\Contracts\FormatterInterface;
1920
use Laudis\Neo4j\Formatter\BasicFormatter;
@@ -218,4 +219,23 @@ public function testPathReturnType(string $alias): void
218219
['x' => 'z'],
219220
], $result->get('p'));
220221
}
222+
223+
/**
224+
* @dataProvider connectionAliases
225+
*/
226+
public function testPeriodicCommit(string $alias): void
227+
{
228+
if (getenv('TESTING_ENVIRONMENT') !== 'local') {
229+
self::markTestSkipped('Only local environment has access to local files');
230+
}
231+
232+
$this->client->run(<<<CYPHER
233+
USING PERIODIC COMMIT 10
234+
LOAD CSV FROM 'file:///csv-example.csv' AS line
235+
MERGE (n:File {name: line[0]});
236+
CYPHER, [], $alias);
237+
238+
$result = $this->client->run('MATCH (n:File) RETURN count(n) AS count');
239+
self::assertEquals(20, $result->first()->get('count'));
240+
}
221241
}

tests/resources/csv-example.csv

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
1
2+
2
3+
3
4+
4
5+
5
6+
6
7+
7
8+
8
9+
9
10+
10
11+
11
12+
12
13+
13
14+
14
15+
15
16+
16
17+
17
18+
18
19+
19
20+
20

0 commit comments

Comments
 (0)