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
c80d46d0
Commit
c80d46d0
authored
Sep 06, 2019
by
Simon Schaufelberger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add hasIndex and dropIndexIfExists methods
parent
6ffda75b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
35 deletions
+114
-35
Blueprint.php
src/Jenssegers/Mongodb/Schema/Blueprint.php
+42
-35
SchemaTest.php
tests/SchemaTest.php
+72
-0
No files found.
src/Jenssegers/Mongodb/Schema/Blueprint.php
View file @
c80d46d0
...
@@ -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.
*
*
...
...
tests/SchemaTest.php
View file @
c80d46d0
<?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
)
{
...
...
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