Skip to content

Commit 4854579

Browse files
author
codeliner
committed
Handle named indices
1 parent b466965 commit 4854579

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1",
2020
"ext-pdo": "*",
21-
"event-engine/php-persistence": "^0.1"
21+
"event-engine/php-persistence": "^0.2"
2222
},
2323
"require-dev": {
2424
"roave/security-advisories": "dev-master",

src/PostgresDocumentStore.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,48 @@ public function dropCollection(string $collectionName): void
166166
});
167167
}
168168

169+
public function hasCollectionIndex(string $collectionName, string $indexName): bool
170+
{
171+
$query = <<<EOT
172+
SELECT INDEXNAME
173+
FROM pg_indexes
174+
WHERE TABLENAME = '{$this->tableName($collectionName)}'
175+
AND INDEXNAME = '$indexName'
176+
EOT;
177+
178+
$stmt = $this->connection->prepare($query);
179+
180+
$stmt->execute();
181+
182+
$row = $stmt->fetchColumn();
183+
184+
return !!$row;
185+
}
186+
187+
/**
188+
* @param string $collectionName
189+
* @param Index $index
190+
* @throws \EventEngine\DocumentStore\Exception\RuntimeException if adding did not succeed
191+
*/
192+
public function addCollectionIndex(string $collectionName, Index $index): void
193+
{
194+
$cmd = $this->indexToSqlCmd($index, $collectionName);
195+
196+
$this->connection->prepare($cmd)->execute();
197+
}
198+
199+
/**
200+
* @param string $collectionName
201+
* @param string $indexName
202+
* @throws \EventEngine\DocumentStore\Exception\RuntimeException if dropping did not succeed
203+
*/
204+
public function dropCollectionIndex(string $collectionName, string $indexName): void
205+
{
206+
$cmd = "DROP INDEX $indexName";
207+
208+
$this->connection->prepare($cmd)->execute();
209+
}
210+
169211
/**
170212
* @param string $collectionName
171213
* @param string $docId
@@ -527,8 +569,10 @@ private function indexToSqlCmd(Index $index, string $collectionName): string
527569
throw new RuntimeException('Unsupported index type. Got ' . get_class($index));
528570
}
529571

572+
$name = $index->name() ?? '';
573+
530574
$cmd = <<<EOT
531-
CREATE $type ON {$this->tableName($collectionName)}
575+
CREATE $type $name ON {$this->tableName($collectionName)}
532576
$fields;
533577
EOT;
534578

tests/PostgresDocumentStoreTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ public function it_adds_collection_with_multi_field_index_unique(): void
139139
$this->assertStringStartsWith('CREATE UNIQUE INDEX', $indexes[1]['indexdef']);
140140
}
141141

142+
/**
143+
* @test
144+
*/
145+
public function it_handles_named_indices(): void
146+
{
147+
$collectionName = 'test_named_indices';
148+
149+
$this->documentStore->addCollection(
150+
$collectionName,
151+
FieldIndex::namedIndexForField('testidx_field_a', 'a'),
152+
MultiFieldIndex::namedIndexForFields('multitestidx_fields_a_b', ['a', 'b'])
153+
);
154+
155+
$this->assertTrue($this->documentStore->hasCollectionIndex($collectionName, 'testidx_field_a'));
156+
157+
$this->assertTrue($this->documentStore->hasCollectionIndex($collectionName, 'multitestidx_fields_a_b'));
158+
159+
$this->documentStore->dropCollectionIndex($collectionName, 'testidx_field_a');
160+
161+
$this->assertFalse($this->documentStore->hasCollectionIndex($collectionName, 'testidx_field_a'));
162+
163+
$this->documentStore->addCollectionIndex($collectionName, FieldIndex::namedIndexForField('testidx_field_b', 'b'));
164+
165+
$this->assertTrue($this->documentStore->hasCollectionIndex($collectionName, 'testidx_field_b'));
166+
}
167+
142168
/**
143169
* @test
144170
*/

0 commit comments

Comments
 (0)