Skip to content

Commit c80d46d

Browse files
committed
Add hasIndex and dropIndexIfExists methods
1 parent 6ffda75 commit c80d46d

File tree

2 files changed

+114
-35
lines changed

2 files changed

+114
-35
lines changed

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,48 @@ public function dropIndexIfExists($indexOrColumns = null)
9999
return $this;
100100
}
101101

102+
/**
103+
* Check whether the given index exists.
104+
*
105+
* @param string|array $indexOrColumns
106+
* @return bool
107+
*/
108+
public function hasIndex($indexOrColumns = null)
109+
{
110+
$indexOrColumns = $this->transformColumns($indexOrColumns);
111+
foreach ($this->collection->listIndexes() as $index) {
112+
if (is_array($indexOrColumns) && in_array($index->getName(), $indexOrColumns)) {
113+
return true;
114+
}
115+
116+
if (is_string($indexOrColumns) && $index->getName() == $indexOrColumns) {
117+
return true;
118+
}
119+
}
120+
return false;
121+
}
122+
123+
/**
124+
* @param string|array $indexOrColumns
125+
* @return string
126+
*/
127+
protected function transformColumns($indexOrColumns)
128+
{
129+
if (is_array($indexOrColumns)) {
130+
$indexOrColumns = $this->fluent($indexOrColumns);
131+
132+
// Transform the columns to the index name.
133+
$transform = [];
134+
135+
foreach ($indexOrColumns as $column) {
136+
$transform[$column] = $column . '_1';
137+
}
138+
139+
$indexOrColumns = implode('_', $transform);
140+
}
141+
return $indexOrColumns;
142+
}
143+
102144
/**
103145
* @inheritdoc
104146
*/
@@ -238,41 +280,6 @@ public function sparse_and_unique($columns = null, $options = [])
238280
return $this;
239281
}
240282

241-
/**
242-
* Check whether the given index exists.
243-
*
244-
* @param string|array $indexOrColumns
245-
* @return bool
246-
*/
247-
public function hasIndex($indexOrColumns = null)
248-
{
249-
$indexOrColumns = $this->transformColumns($indexOrColumns);
250-
foreach ($this->collection->listIndexes() as $index) {
251-
if ($index->getName() == $indexOrColumns) {
252-
return true;
253-
}
254-
}
255-
return false;
256-
}
257-
258-
/**
259-
* @param string|array $indexOrColumns
260-
* @return string|array
261-
*/
262-
private function transformColumns($indexOrColumns)
263-
{
264-
if (is_array($indexOrColumns)) {
265-
$indexOrColumns = $this->fluent($indexOrColumns);
266-
// Transform the columns to the index name.
267-
$transform = [];
268-
foreach ($indexOrColumns as $column) {
269-
$transform[$column] = $column . '_1';
270-
}
271-
$indexOrColumns = join('_', $transform);
272-
}
273-
return $indexOrColumns;
274-
}
275-
276283
/**
277284
* Allow fluent columns.
278285
*

tests/SchemaTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Jenssegers\Mongodb\Schema\Blueprint;
4+
35
class SchemaTest extends TestCase
46
{
57
public function tearDown(): void
@@ -144,6 +146,76 @@ public function testDropIndex()
144146
$this->assertFalse($index);
145147
}
146148

149+
public function testDropIndexIfExists()
150+
{
151+
Schema::collection('newcollection', function (Blueprint $collection) {
152+
$collection->unique('uniquekey');
153+
$collection->dropIndexIfExists('uniquekey_1');
154+
});
155+
156+
$index = $this->getIndex('newcollection', 'uniquekey');
157+
$this->assertEquals(null, $index);
158+
159+
Schema::collection('newcollection', function (Blueprint $collection) {
160+
$collection->unique('uniquekey');
161+
$collection->dropIndexIfExists(['uniquekey']);
162+
});
163+
164+
$index = $this->getIndex('newcollection', 'uniquekey');
165+
$this->assertEquals(null, $index);
166+
167+
Schema::collection('newcollection', function (Blueprint $collection) {
168+
$collection->index(['field_a', 'field_b']);
169+
});
170+
171+
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
172+
$this->assertNotNull($index);
173+
174+
Schema::collection('newcollection', function (Blueprint $collection) {
175+
$collection->dropIndexIfExists(['field_a', 'field_b']);
176+
});
177+
178+
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
179+
$this->assertFalse($index);
180+
181+
Schema::collection('newcollection', function (Blueprint $collection) {
182+
$collection->index(['field_a', 'field_b'], 'custom_index_name');
183+
});
184+
185+
$index = $this->getIndex('newcollection', 'custom_index_name');
186+
$this->assertNotNull($index);
187+
188+
Schema::collection('newcollection', function (Blueprint $collection) {
189+
$collection->dropIndexIfExists('custom_index_name');
190+
});
191+
192+
$index = $this->getIndex('newcollection', 'custom_index_name');
193+
$this->assertFalse($index);
194+
}
195+
196+
public function testHasIndex()
197+
{
198+
$instance = $this;
199+
200+
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
201+
$collection->index('myhaskey1');
202+
$instance->assertTrue($collection->hasIndex('myhaskey1_1'));
203+
$instance->assertFalse($collection->hasIndex('myhaskey1'));
204+
});
205+
206+
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
207+
$collection->index('myhaskey2');
208+
$instance->assertTrue($collection->hasIndex(['myhaskey2']));
209+
$instance->assertFalse($collection->hasIndex(['myhaskey2_1']));
210+
});
211+
212+
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
213+
$collection->index(['field_a', 'field_b']);
214+
$instance->assertTrue($collection->hasIndex(['field_a_1_field_b']));
215+
$instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1']));
216+
});
217+
}
218+
147219
public function testBackground()
148220
{
149221
Schema::collection('newcollection', function ($collection) {

0 commit comments

Comments
 (0)