Skip to content

Commit e2ee635

Browse files
committed
create schema on demand
1 parent 7342b94 commit e2ee635

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ $schema->setup();
3434
// later if you want to cleanup lock space, use
3535
$schema->tearDown();
3636

37+
// in addition you can configure TarantoolStore to create schema on demand
38+
// pay attention, this option is false by default
39+
$store = new TarantoolStore($client, [
40+
'createSchema' => true,
41+
]);
42+
3743
```
3844

3945
# Using Store
@@ -104,5 +110,6 @@ $schema = new SchemaManager($client, [
104110
$store = new TarantoolStore($client, [
105111
'space' => 'lock',
106112
'initialTtl' => 300,
113+
'createSchema' => false,
107114
]);
108115
```

src/TarantoolStore.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class TarantoolStore implements PersistingStoreInterface
1919
use OptionalConstructor;
2020
use ExpiringStoreTrait;
2121

22+
/**
23+
* Initialize database schema if not exists
24+
*/
25+
protected bool $createSchema = false;
26+
2227
/**
2328
* Expiration delay of locks in seconds
2429
*/
@@ -70,8 +75,7 @@ public function delete(Key $key, ?string $token = null)
7075
*/
7176
public function exists(Key $key)
7277
{
73-
$data = $this->client
74-
->getSpace($this->space)
78+
$data = $this->getSpace()
7579
->select(Criteria::key([ (string) $key ]));
7680

7781
if (count($data)) {
@@ -114,8 +118,7 @@ public function save(Key $key)
114118
$this->getSpace()->insert($tuple);
115119
$this->checkNotExpired($key);
116120
} catch (RequestFailed $e) {
117-
$data = $this->client
118-
->getSpace($this->space)
121+
$data = $this->getSpace()
119122
->select(Criteria::key([ (string) $key ]));
120123

121124
if (count($data)) {
@@ -153,6 +156,15 @@ protected function getExpirationTimestamp(Key $key): float
153156

154157
protected function getSpace(): Space
155158
{
156-
return $this->client->getSpace($this->space);
159+
try {
160+
return $this->client->getSpace($this->space);
161+
} catch (RequestFailed $e) {
162+
if ($this->createSchema) {
163+
$schema = new SchemaManager($this->client, [ 'space' => $this->space ]);
164+
$schema->setup();
165+
return $this->client->getSpace($this->space);
166+
}
167+
throw $e;
168+
}
157169
}
158170
}

tests/TarantoolStoreTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Lock\Key;
1010
use Symfony\Component\Lock\PersistingStoreInterface;
1111
use Tarantool\Client\Client;
12+
use Tarantool\Client\Exception\RequestFailed;
1213
use Tarantool\Client\Schema\Criteria;
1314
use Tarantool\SymfonyLock\Cleaner;
1415
use Tarantool\SymfonyLock\SchemaManager;
@@ -238,4 +239,40 @@ public function testStoreInvalidSpaceName()
238239

239240
new TarantoolStore(Client::fromDefaults(), [ 'space' => '' ]);
240241
}
242+
243+
public function testSuccessSchemaCreation()
244+
{
245+
$host = getenv('TARANTOOL_CONNECTION_HOST');
246+
$port = getenv('TARANTOOL_CONNECTION_PORT');
247+
248+
$client = Client::fromDsn("tcp://$host:$port");
249+
$client->evaluate('box.session.su("admin")');
250+
251+
$schema = new SchemaManager($client);
252+
$schema->tearDown();
253+
254+
$store = new TarantoolStore($client, [ 'createSchema' => true ]);
255+
$store->save(new Key(uniqid(__METHOD__, true)));
256+
257+
$tuples = $this->client->getSpace('lock')->select(Criteria::key([]));
258+
$this->assertCount(1, $tuples);
259+
}
260+
261+
public function testDefaultSchemaCreationIsDisabled()
262+
{
263+
$host = getenv('TARANTOOL_CONNECTION_HOST');
264+
$port = getenv('TARANTOOL_CONNECTION_PORT');
265+
266+
$client = Client::fromDsn("tcp://$host:$port");
267+
$client->evaluate('box.session.su("admin")');
268+
269+
$schema = new SchemaManager($client);
270+
$schema->tearDown();
271+
272+
$this->expectException(RequestFailed::class);
273+
$this->expectExceptionMessage("Space 'lock' does not exist");
274+
275+
$store = new TarantoolStore($client);
276+
$store->save(new Key(uniqid(__METHOD__, true)));
277+
}
241278
}

0 commit comments

Comments
 (0)