Skip to content

Commit 9bc56d0

Browse files
committed
implement once method
1 parent 868a427 commit 9bc56d0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Schema.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public function hasSpace($name)
6868
return array_key_exists($name, $this->names);
6969
}
7070

71+
public function once($name, $callback)
72+
{
73+
$key = 'once' . $name;
74+
75+
$rows = $this->mapper->find('_schema', ['key' => $key]);
76+
if(!count($rows)) {
77+
$this->mapper->create('_schema', ['key' => $key]);
78+
return $callback($this->mapper);
79+
}
80+
}
81+
7182
public function reset()
7283
{
7384
$this->names = $this->mapper->getClient()->evaluate("

tests/SchemaTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
<?php
22

3+
use Tarantool\Mapper\Mapper;
34
use Tarantool\Mapper\Space;
45

56
class SchemaTest extends TestCase
67
{
8+
public function testOnce()
9+
{
10+
$mapper = $this->createMapper();
11+
$this->clean($mapper);
12+
13+
$schema = $mapper->getSchema();
14+
15+
$test = $schema->createSpace('test');
16+
$test->addProperty('name', 'str');
17+
$test->createIndex('name');
18+
19+
$flag = $mapper->findOne('_schema', ['key' => 'onceinsert']);
20+
if($flag) {
21+
$mapper->remove($flag);
22+
}
23+
24+
$iterations = 2;
25+
while($iterations--) {
26+
$schema->once('insert', function(Mapper $mapper) {
27+
$mapper->create('test', ['name' => 'example row']);
28+
});
29+
}
30+
$this->assertCount(1, $mapper->find('test'));
31+
}
32+
733
public function testSystemMeta()
834
{
935
$mapper = $this->createMapper();

0 commit comments

Comments
 (0)