Commit c80d46d0 authored by Simon Schaufelberger's avatar Simon Schaufelberger

Add hasIndex and dropIndexIfExists methods

parent 6ffda75b
...@@ -99,6 +99,48 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -99,6 +99,48 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
return $this; return $this;
} }
/**
* Check whether the given index exists.
*
* @param string|array $indexOrColumns
* @return bool
*/
public function hasIndex($indexOrColumns = null)
{
$indexOrColumns = $this->transformColumns($indexOrColumns);
foreach ($this->collection->listIndexes() as $index) {
if (is_array($indexOrColumns) && in_array($index->getName(), $indexOrColumns)) {
return true;
}
if (is_string($indexOrColumns) && $index->getName() == $indexOrColumns) {
return true;
}
}
return false;
}
/**
* @param string|array $indexOrColumns
* @return string
*/
protected function transformColumns($indexOrColumns)
{
if (is_array($indexOrColumns)) {
$indexOrColumns = $this->fluent($indexOrColumns);
// Transform the columns to the index name.
$transform = [];
foreach ($indexOrColumns as $column) {
$transform[$column] = $column . '_1';
}
$indexOrColumns = implode('_', $transform);
}
return $indexOrColumns;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -238,41 +280,6 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -238,41 +280,6 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
return $this; return $this;
} }
/**
* Check whether the given index exists.
*
* @param string|array $indexOrColumns
* @return bool
*/
public function hasIndex($indexOrColumns = null)
{
$indexOrColumns = $this->transformColumns($indexOrColumns);
foreach ($this->collection->listIndexes() as $index) {
if ($index->getName() == $indexOrColumns) {
return true;
}
}
return false;
}
/**
* @param string|array $indexOrColumns
* @return string|array
*/
private function transformColumns($indexOrColumns)
{
if (is_array($indexOrColumns)) {
$indexOrColumns = $this->fluent($indexOrColumns);
// Transform the columns to the index name.
$transform = [];
foreach ($indexOrColumns as $column) {
$transform[$column] = $column . '_1';
}
$indexOrColumns = join('_', $transform);
}
return $indexOrColumns;
}
/** /**
* Allow fluent columns. * Allow fluent columns.
* *
......
<?php <?php
use Jenssegers\Mongodb\Schema\Blueprint;
class SchemaTest extends TestCase class SchemaTest extends TestCase
{ {
public function tearDown(): void public function tearDown(): void
...@@ -144,6 +146,76 @@ class SchemaTest extends TestCase ...@@ -144,6 +146,76 @@ class SchemaTest extends TestCase
$this->assertFalse($index); $this->assertFalse($index);
} }
public function testDropIndexIfExists()
{
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->unique('uniquekey');
$collection->dropIndexIfExists('uniquekey_1');
});
$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->unique('uniquekey');
$collection->dropIndexIfExists(['uniquekey']);
});
$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->index(['field_a', 'field_b']);
});
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertNotNull($index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->dropIndexIfExists(['field_a', 'field_b']);
});
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
});
$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertNotNull($index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->dropIndexIfExists('custom_index_name');
});
$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertFalse($index);
}
public function testHasIndex()
{
$instance = $this;
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index('myhaskey1');
$instance->assertTrue($collection->hasIndex('myhaskey1_1'));
$instance->assertFalse($collection->hasIndex('myhaskey1'));
});
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index('myhaskey2');
$instance->assertTrue($collection->hasIndex(['myhaskey2']));
$instance->assertFalse($collection->hasIndex(['myhaskey2_1']));
});
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index(['field_a', 'field_b']);
$instance->assertTrue($collection->hasIndex(['field_a_1_field_b']));
$instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1']));
});
}
public function testBackground() public function testBackground()
{ {
Schema::collection('newcollection', function ($collection) { Schema::collection('newcollection', function ($collection) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment