Unverified Commit 3f588096 authored by Simon Schaufelberger's avatar Simon Schaufelberger Committed by GitHub

Add hasIndex and dropIndexIfExists methods

parent 4ef3483e
......@@ -78,21 +78,24 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
*/
public function dropIndex($indexOrColumns = null)
{
if (is_array($indexOrColumns)) {
$indexOrColumns = $this->fluent($indexOrColumns);
$indexOrColumns = $this->transformColumns($indexOrColumns);
// Transform the columns to the index name.
$transform = [];
$this->collection->dropIndex($indexOrColumns);
foreach ($indexOrColumns as $column) {
$transform[$column] = $column . '_1';
}
return $this;
}
$indexOrColumns = join('_', $transform);
/**
* Indicate that the given index should be dropped, but do not fail if it didn't exist.
*
* @param string|array $indexOrColumns
* @return Blueprint
*/
public function dropIndexIfExists($indexOrColumns = null)
{
if ($this->hasIndex($indexOrColumns)) {
$this->dropIndex($indexOrColumns);
}
$this->collection->dropIndex($indexOrColumns);
return $this;
}
......@@ -235,6 +238,41 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
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.
*
......
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