Skip to content

Commit 8a50f91

Browse files
committed
Adding MongoDB specific indexing operations to Schema Builder
1 parent 8e56078 commit 8a50f91

File tree

3 files changed

+81
-16
lines changed

3 files changed

+81
-16
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ Supported operations are:
103103
- collection
104104
- hasCollection
105105
- index and dropIndex
106-
- unique and dropUnique
106+
- unique
107+
- background, sparse, expire (MongoDB specific)
107108

108109
Read more about the schema builder on http://laravel.com/docs/schema
109110

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(Connection $connection, $collection)
2828
* Specify an index for the collection.
2929
*
3030
* @param string|array $columns
31-
* @param array $otions
31+
* @param array $otions
3232
* @return bool
3333
*/
3434
public function index($columns, $options = array())
@@ -55,23 +55,46 @@ public function dropIndex($columns)
5555
* Specify a unique index for the collection.
5656
*
5757
* @param string|array $columns
58-
* @param string $name
5958
* @return bool
6059
*/
6160
public function unique($columns)
6261
{
63-
return $this->index($columns, array("unique" => true));
62+
return $this->index($columns, array('unique' => true));
6463
}
6564

6665
/**
67-
* Indicate that the given unique key should be dropped.
68-
*
69-
* @param string|array $index
66+
* Specify a non blocking index for the collection.
67+
*
68+
* @param string|array $columns
69+
* @return bool
70+
*/
71+
public function background($columns)
72+
{
73+
return $this->index($columns, array('background' => true));
74+
}
75+
76+
/**
77+
* Specify a sparse index for the collection.
78+
*
79+
* @param string|array $columns
80+
* @return bool
81+
*/
82+
public function sparse($columns)
83+
{
84+
return $this->index($columns, array('sparse' => true));
85+
}
86+
87+
/**
88+
* Specify the number of seconds after wich a document should be considered expired based,
89+
* on the given single-field index containing a date.
90+
*
91+
* @param string|array $columns
92+
* @param int $seconds
7093
* @return bool
7194
*/
72-
public function dropUnique($columns)
95+
public function expire($columns, $seconds)
7396
{
74-
return $this->dropIndex($columns);
97+
return $this->index($columns, array('expireAfterSeconds' => $seconds));
7598
}
7699

77100
/**

tests/SchemaTest.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testIndex()
3434
});
3535

3636
$index = $this->getIndex('newcollection', 'mykey');
37-
$this->assertEquals(1, $index);
37+
$this->assertEquals(1, $index['key']['mykey']);
3838
}
3939

4040
public function testUnique()
@@ -45,7 +45,52 @@ public function testUnique()
4545
});
4646

4747
$index = $this->getIndex('newcollection', 'uniquekey');
48-
$this->assertEquals('unique', $index);
48+
$this->assertEquals(1, $index['unique']);
49+
}
50+
51+
public function testDropIndex()
52+
{
53+
Schema::collection('newcollection', function($collection)
54+
{
55+
$collection->unique('uniquekey');
56+
$collection->dropIndex('uniquekey');
57+
});
58+
59+
$index = $this->getIndex('newcollection', 'uniquekey');
60+
$this->assertEquals(null, $index);
61+
}
62+
63+
public function testBackground()
64+
{
65+
Schema::collection('newcollection', function($collection)
66+
{
67+
$collection->background('backgroundkey');
68+
});
69+
70+
$index = $this->getIndex('newcollection', 'backgroundkey');
71+
$this->assertEquals(1, $index['background']);
72+
}
73+
74+
public function testSparse()
75+
{
76+
Schema::collection('newcollection', function($collection)
77+
{
78+
$collection->background('backgroundkey');
79+
});
80+
81+
$index = $this->getIndex('newcollection', 'backgroundkey');
82+
$this->assertEquals(1, $index['background']);
83+
}
84+
85+
public function testExpire()
86+
{
87+
Schema::collection('newcollection', function($collection)
88+
{
89+
$collection->expire('expirekey', 60);
90+
});
91+
92+
$index = $this->getIndex('newcollection', 'expirekey');
93+
$this->assertEquals(60, $index['expireAfterSeconds']);
4994
}
5095

5196
protected function getIndex($collection, $name)
@@ -54,11 +99,7 @@ protected function getIndex($collection, $name)
5499

55100
foreach ($collection->getIndexInfo() as $index)
56101
{
57-
if (isset($index['key'][$name]))
58-
{
59-
if (isset($index['unique'])) return 'unique';
60-
return $index['key'][$name];
61-
}
102+
if (isset($index['key'][$name])) return $index;
62103
}
63104

64105
return false;

0 commit comments

Comments
 (0)