Skip to content

Commit 441973a

Browse files
committed
sequence initializer added
1 parent 11031ec commit 441973a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Plugins/Sequence.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ private function generateValue($space)
3333

3434
$entity = $this->mapper->findOne('sequence', $space->getId());
3535
if(!$entity) {
36+
37+
$query = "return box.space.".$space->getName().".index[0]:max()";
38+
$data = $this->mapper->getClient()->evaluate($query)->getData();
39+
$max = $data ? $data[0][0] : 0;
40+
3641
$entity = $this->mapper->create('sequence', [
3742
'space' => $space->getId(),
38-
'counter' => 0,
43+
'counter' => $max,
3944
]);
4045
}
4146

tests/SequenceTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55

66
class SequenceTest extends TestCase
77
{
8+
public function testInitialization()
9+
{
10+
$mapper = $this->createMapper();
11+
$mapper->addPlugin(new Sequence($mapper));
12+
$this->clean($mapper);
13+
14+
$this->assertCount(1, $mapper->getPlugins());
15+
16+
$person = $mapper->getSchema()->createSpace('person');
17+
$person->addProperty('id', 'unsigned');
18+
$person->addProperty('email', 'str');
19+
$person->createIndex('id');
20+
21+
$mapper->create('person', [1, '[email protected]']);
22+
$mapper->create('person', [2, '[email protected]']);
23+
$mapper->create('person', [3, '[email protected]']);
24+
25+
$pasha = $mapper->create('person', 'pasha');
26+
$this->assertSame($pasha->id, 4);
27+
}
828
public function testPluginInstance()
929
{
1030
$mapper = $this->createMapper();

0 commit comments

Comments
 (0)