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
30b3e929
Commit
30b3e929
authored
Aug 25, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding MongoDB specific indexing operations to Schema Builder
parent
b45d3034
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
16 deletions
+81
-16
README.md
README.md
+2
-1
Blueprint.php
src/Jenssegers/Mongodb/Schema/Blueprint.php
+31
-8
SchemaTest.php
tests/SchemaTest.php
+48
-7
No files found.
README.md
View file @
30b3e929
...
@@ -103,7 +103,8 @@ Supported operations are:
...
@@ -103,7 +103,8 @@ Supported operations are:
-
collection
-
collection
-
hasCollection
-
hasCollection
-
index and dropIndex
-
index and dropIndex
-
unique and dropUnique
-
unique
-
background, sparse, expire (MongoDB specific)
Read more about the schema builder on http://laravel.com/docs/schema
Read more about the schema builder on http://laravel.com/docs/schema
...
...
src/Jenssegers/Mongodb/Schema/Blueprint.php
View file @
30b3e929
...
@@ -28,7 +28,7 @@ class Blueprint {
...
@@ -28,7 +28,7 @@ class Blueprint {
* Specify an index for the collection.
* Specify an index for the collection.
*
*
* @param string|array $columns
* @param string|array $columns
* @param array $otions
* @param array
$otions
* @return bool
* @return bool
*/
*/
public
function
index
(
$columns
,
$options
=
array
())
public
function
index
(
$columns
,
$options
=
array
())
...
@@ -55,23 +55,46 @@ class Blueprint {
...
@@ -55,23 +55,46 @@ class Blueprint {
* Specify a unique index for the collection.
* Specify a unique index for the collection.
*
*
* @param string|array $columns
* @param string|array $columns
* @param string $name
* @return bool
* @return bool
*/
*/
public
function
unique
(
$columns
)
public
function
unique
(
$columns
)
{
{
return
$this
->
index
(
$columns
,
array
(
"unique"
=>
true
));
return
$this
->
index
(
$columns
,
array
(
'unique'
=>
true
));
}
}
/**
/**
* Indicate that the given unique key should be dropped.
* Specify a non blocking index for the collection.
*
*
* @param string|array $index
* @param string|array $columns
* @return bool
*/
public
function
background
(
$columns
)
{
return
$this
->
index
(
$columns
,
array
(
'background'
=>
true
));
}
/**
* Specify a sparse index for the collection.
*
* @param string|array $columns
* @return bool
*/
public
function
sparse
(
$columns
)
{
return
$this
->
index
(
$columns
,
array
(
'sparse'
=>
true
));
}
/**
* Specify the number of seconds after wich a document should be considered expired based,
* on the given single-field index containing a date.
*
* @param string|array $columns
* @param int $seconds
* @return bool
* @return bool
*/
*/
public
function
dropUnique
(
$column
s
)
public
function
expire
(
$columns
,
$second
s
)
{
{
return
$this
->
dropIndex
(
$columns
);
return
$this
->
index
(
$columns
,
array
(
'expireAfterSeconds'
=>
$seconds
)
);
}
}
/**
/**
...
...
tests/SchemaTest.php
View file @
30b3e929
...
@@ -34,7 +34,7 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
...
@@ -34,7 +34,7 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
});
});
$index
=
$this
->
getIndex
(
'newcollection'
,
'mykey'
);
$index
=
$this
->
getIndex
(
'newcollection'
,
'mykey'
);
$this
->
assertEquals
(
1
,
$index
);
$this
->
assertEquals
(
1
,
$index
[
'key'
][
'mykey'
]
);
}
}
public
function
testUnique
()
public
function
testUnique
()
...
@@ -45,7 +45,52 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
...
@@ -45,7 +45,52 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
});
});
$index
=
$this
->
getIndex
(
'newcollection'
,
'uniquekey'
);
$index
=
$this
->
getIndex
(
'newcollection'
,
'uniquekey'
);
$this
->
assertEquals
(
'unique'
,
$index
);
$this
->
assertEquals
(
1
,
$index
[
'unique'
]);
}
public
function
testDropIndex
()
{
Schema
::
collection
(
'newcollection'
,
function
(
$collection
)
{
$collection
->
unique
(
'uniquekey'
);
$collection
->
dropIndex
(
'uniquekey'
);
});
$index
=
$this
->
getIndex
(
'newcollection'
,
'uniquekey'
);
$this
->
assertEquals
(
null
,
$index
);
}
public
function
testBackground
()
{
Schema
::
collection
(
'newcollection'
,
function
(
$collection
)
{
$collection
->
background
(
'backgroundkey'
);
});
$index
=
$this
->
getIndex
(
'newcollection'
,
'backgroundkey'
);
$this
->
assertEquals
(
1
,
$index
[
'background'
]);
}
public
function
testSparse
()
{
Schema
::
collection
(
'newcollection'
,
function
(
$collection
)
{
$collection
->
background
(
'backgroundkey'
);
});
$index
=
$this
->
getIndex
(
'newcollection'
,
'backgroundkey'
);
$this
->
assertEquals
(
1
,
$index
[
'background'
]);
}
public
function
testExpire
()
{
Schema
::
collection
(
'newcollection'
,
function
(
$collection
)
{
$collection
->
expire
(
'expirekey'
,
60
);
});
$index
=
$this
->
getIndex
(
'newcollection'
,
'expirekey'
);
$this
->
assertEquals
(
60
,
$index
[
'expireAfterSeconds'
]);
}
}
protected
function
getIndex
(
$collection
,
$name
)
protected
function
getIndex
(
$collection
,
$name
)
...
@@ -54,11 +99,7 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
...
@@ -54,11 +99,7 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
foreach
(
$collection
->
getIndexInfo
()
as
$index
)
foreach
(
$collection
->
getIndexInfo
()
as
$index
)
{
{
if
(
isset
(
$index
[
'key'
][
$name
]))
if
(
isset
(
$index
[
'key'
][
$name
]))
return
$index
;
{
if
(
isset
(
$index
[
'unique'
]))
return
'unique'
;
return
$index
[
'key'
][
$name
];
}
}
}
return
false
;
return
false
;
...
...
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