Skip to content

Commit 4fe82c7

Browse files
authored
Merge pull request #1790 from simonschaufi/patch-1
Add hasIndex and dropIndexIfExists methods
2 parents 2d76d22 + 7eb0ad2 commit 4fe82c7

File tree

2 files changed

+120
-5
lines changed

2 files changed

+120
-5
lines changed

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,54 @@ public function primary($columns = null, $name = null, $algorithm = null, $optio
7474
* @inheritdoc
7575
*/
7676
public function dropIndex($indexOrColumns = null)
77+
{
78+
$indexOrColumns = $this->transformColumns($indexOrColumns);
79+
80+
$this->collection->dropIndex($indexOrColumns);
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* Indicate that the given index should be dropped, but do not fail if it didn't exist.
87+
*
88+
* @param string|array $indexOrColumns
89+
* @return Blueprint
90+
*/
91+
public function dropIndexIfExists($indexOrColumns = null)
92+
{
93+
if ($this->hasIndex($indexOrColumns)) {
94+
$this->dropIndex($indexOrColumns);
95+
}
96+
return $this;
97+
}
98+
99+
/**
100+
* Check whether the given index exists.
101+
*
102+
* @param string|array $indexOrColumns
103+
* @return bool
104+
*/
105+
public function hasIndex($indexOrColumns = null)
106+
{
107+
$indexOrColumns = $this->transformColumns($indexOrColumns);
108+
foreach ($this->collection->listIndexes() as $index) {
109+
if (is_array($indexOrColumns) && in_array($index->getName(), $indexOrColumns)) {
110+
return true;
111+
}
112+
113+
if (is_string($indexOrColumns) && $index->getName() == $indexOrColumns) {
114+
return true;
115+
}
116+
}
117+
return false;
118+
}
119+
120+
/**
121+
* @param string|array $indexOrColumns
122+
* @return string
123+
*/
124+
protected function transformColumns($indexOrColumns)
77125
{
78126
if (is_array($indexOrColumns)) {
79127
$indexOrColumns = $this->fluent($indexOrColumns);
@@ -85,12 +133,9 @@ public function dropIndex($indexOrColumns = null)
85133
$transform[$column] = $column . '_1';
86134
}
87135

88-
$indexOrColumns = join('_', $transform);
136+
$indexOrColumns = implode('_', $transform);
89137
}
90-
91-
$this->collection->dropIndex($indexOrColumns);
92-
93-
return $this;
138+
return $indexOrColumns;
94139
}
95140

96141
/**

tests/SchemaTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,76 @@ public function testDropIndex(): void
147147
$this->assertFalse($index);
148148
}
149149

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

0 commit comments

Comments
 (0)