Skip to content

Commit 3873b49

Browse files
committed
nullable index configuration #27
1 parent d7f465b commit 3873b49

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/Space.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,30 @@ public function createIndex($config): self
163163
}
164164

165165
foreach ($config['fields'] as $property) {
166+
$isNullable = false;
167+
if (is_array($property)) {
168+
if (!array_key_exists('property', $property)) {
169+
throw new Exception("Invalid property configuration");
170+
}
171+
if (array_key_exists('is_nullable', $property)) {
172+
$isNullable = $property['is_nullable'];
173+
}
174+
$property = $property['property'];
175+
}
176+
if ($this->isPropertyNullable($property) != $isNullable) {
177+
$this->setPropertyNullable($property, $isNullable);
178+
}
166179
if (!$this->getPropertyType($property)) {
167180
throw new Exception("Unknown property $property", 1);
168181
}
169-
$options['parts'][] = $this->getPropertyIndex($property) + 1;
170-
$options['parts'][] = $this->getPropertyType($property);
171-
$this->setPropertyNullable($property, false);
182+
$part = [
183+
'field' => $this->getPropertyIndex($property) + 1,
184+
'type' => $this->getPropertyType($property),
185+
];
186+
if ($this->isPropertyNullable($property)) {
187+
$part['is_nullable'] = true;
188+
}
189+
$options['parts'][] = $part;
172190
}
173191

174192
$name = array_key_exists('name', $config) ? $config['name'] : implode('_', $config['fields']);

tests/SchemaTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,48 @@
77

88
class SchemaTest extends TestCase
99
{
10+
public function testNullableIndexes()
11+
{
12+
$mapper = $this->createMapper();
13+
$this->clean($mapper);
14+
15+
$space = $mapper->getSchema()->createSpace('example', [
16+
'if_not_exists' => true,
17+
'engine' => 'memtx',
18+
'properties' => [
19+
'id' => 'unsigned',
20+
'field1' => 'string',
21+
'field2' => 'string',
22+
'field3' => 'unsigned',
23+
'field4' => 'unsigned',
24+
'field5' => 'unsigned',
25+
'field6' => 'unsigned',
26+
],
27+
])
28+
->setPropertyNullable('field5')
29+
->addIndex([
30+
'fields' => 'id',
31+
'if_not_exists' => true,
32+
'sequence' => true,
33+
'name' => 'index_1'
34+
])
35+
->addIndex([
36+
'fields' => [
37+
'field2',
38+
[
39+
'property' => 'field5',
40+
'is_nullable' => true,
41+
],
42+
],
43+
'unique' => false,
44+
'if_not_exists' => true,
45+
'name' => 'index_2',
46+
]);
47+
48+
$this->assertFalse($space->isPropertyNullable('field2'));
49+
$this->assertTrue($space->isPropertyNullable('field5'));
50+
}
51+
1052
public function testDynamicIndexCreation()
1153
{
1254
$mapper = $this->createMapper();

0 commit comments

Comments
 (0)