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
4fe82c77
Unverified
Commit
4fe82c77
authored
Oct 03, 2019
by
Jens Segers
Committed by
GitHub
Oct 03, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1790 from simonschaufi/patch-1
Add hasIndex and dropIndexIfExists methods
parents
2d76d22a
7eb0ad2e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
120 additions
and
5 deletions
+120
-5
Blueprint.php
src/Jenssegers/Mongodb/Schema/Blueprint.php
+50
-5
SchemaTest.php
tests/SchemaTest.php
+70
-0
No files found.
src/Jenssegers/Mongodb/Schema/Blueprint.php
View file @
4fe82c77
...
...
@@ -74,6 +74,54 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
* @inheritdoc
*/
public
function
dropIndex
(
$indexOrColumns
=
null
)
{
$indexOrColumns
=
$this
->
transformColumns
(
$indexOrColumns
);
$this
->
collection
->
dropIndex
(
$indexOrColumns
);
return
$this
;
}
/**
* 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
);
}
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
);
...
...
@@ -85,12 +133,9 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
$transform
[
$column
]
=
$column
.
'_1'
;
}
$indexOrColumns
=
join
(
'_'
,
$transform
);
$indexOrColumns
=
implode
(
'_'
,
$transform
);
}
$this
->
collection
->
dropIndex
(
$indexOrColumns
);
return
$this
;
return
$indexOrColumns
;
}
/**
...
...
tests/SchemaTest.php
View file @
4fe82c77
...
...
@@ -147,6 +147,76 @@ class SchemaTest extends TestCase
$this
->
assertFalse
(
$index
);
}
public
function
testDropIndexIfExists
()
:
void
{
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
()
:
void
{
$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
()
:
void
{
Schema
::
collection
(
'newcollection'
,
function
(
$collection
)
{
...
...
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