Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
3f588096
Unverified
Commit
3f588096
authored
Jul 29, 2019
by
Simon Schaufelberger
Committed by
GitHub
Jul 29, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add hasIndex and dropIndexIfExists methods
parent
4ef3483e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
11 deletions
+49
-11
Blueprint.php
src/Jenssegers/Mongodb/Schema/Blueprint.php
+49
-11
No files found.
src/Jenssegers/Mongodb/Schema/Blueprint.php
View file @
3f588096
...
@@ -78,21 +78,24 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
...
@@ -78,21 +78,24 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
*/
*/
public
function
dropIndex
(
$indexOrColumns
=
null
)
public
function
dropIndex
(
$indexOrColumns
=
null
)
{
{
if
(
is_array
(
$indexOrColumns
))
{
$indexOrColumns
=
$this
->
transformColumns
(
$indexOrColumns
);
$indexOrColumns
=
$this
->
fluent
(
$indexOrColumns
);
// Transform the columns to the index name.
$this
->
collection
->
dropIndex
(
$indexOrColumns
);
$transform
=
[];
foreach
(
$indexOrColumns
as
$column
)
{
return
$this
;
$transform
[
$column
]
=
$column
.
'_1'
;
}
}
$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
;
return
$this
;
}
}
...
@@ -235,6 +238,41 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
...
@@ -235,6 +238,41 @@ 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.
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment