Commit 30b3e929 authored by Jens Segers's avatar Jens Segers

Adding MongoDB specific indexing operations to Schema Builder

parent b45d3034
...@@ -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
......
...@@ -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($columns) public function expire($columns, $seconds)
{ {
return $this->dropIndex($columns); return $this->index($columns, array('expireAfterSeconds' => $seconds));
} }
/** /**
......
...@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment