Skip to content

Commit

Permalink
nullable index configuration #27
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Jan 14, 2021
1 parent d7f465b commit 3873b49
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,30 @@ public function createIndex($config): self
}

foreach ($config['fields'] as $property) {
$isNullable = false;
if (is_array($property)) {
if (!array_key_exists('property', $property)) {
throw new Exception("Invalid property configuration");
}
if (array_key_exists('is_nullable', $property)) {
$isNullable = $property['is_nullable'];
}
$property = $property['property'];
}
if ($this->isPropertyNullable($property) != $isNullable) {
$this->setPropertyNullable($property, $isNullable);
}
if (!$this->getPropertyType($property)) {
throw new Exception("Unknown property $property", 1);
}
$options['parts'][] = $this->getPropertyIndex($property) + 1;
$options['parts'][] = $this->getPropertyType($property);
$this->setPropertyNullable($property, false);
$part = [
'field' => $this->getPropertyIndex($property) + 1,
'type' => $this->getPropertyType($property),
];
if ($this->isPropertyNullable($property)) {
$part['is_nullable'] = true;
}
$options['parts'][] = $part;
}

$name = array_key_exists('name', $config) ? $config['name'] : implode('_', $config['fields']);
Expand Down
42 changes: 42 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@

class SchemaTest extends TestCase
{
public function testNullableIndexes()
{
$mapper = $this->createMapper();
$this->clean($mapper);

$space = $mapper->getSchema()->createSpace('example', [
'if_not_exists' => true,
'engine' => 'memtx',
'properties' => [
'id' => 'unsigned',
'field1' => 'string',
'field2' => 'string',
'field3' => 'unsigned',
'field4' => 'unsigned',
'field5' => 'unsigned',
'field6' => 'unsigned',
],
])
->setPropertyNullable('field5')
->addIndex([
'fields' => 'id',
'if_not_exists' => true,
'sequence' => true,
'name' => 'index_1'
])
->addIndex([
'fields' => [
'field2',
[
'property' => 'field5',
'is_nullable' => true,
],
],
'unique' => false,
'if_not_exists' => true,
'name' => 'index_2',
]);

$this->assertFalse($space->isPropertyNullable('field2'));
$this->assertTrue($space->isPropertyNullable('field5'));
}

public function testDynamicIndexCreation()
{
$mapper = $this->createMapper();
Expand Down

0 comments on commit 3873b49

Please sign in to comment.