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:
- collection
- hasCollection
- index and dropIndex
- unique and dropUnique
- unique
- background, sparse, expire (MongoDB specific)
Read more about the schema builder on http://laravel.com/docs/schema
......
......@@ -28,7 +28,7 @@ class Blueprint {
* Specify an index for the collection.
*
* @param string|array $columns
* @param array $otions
* @param array $otions
* @return bool
*/
public function index($columns, $options = array())
......@@ -55,23 +55,46 @@ class Blueprint {
* Specify a unique index for the collection.
*
* @param string|array $columns
* @param string $name
* @return bool
*/
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.
*
* @param string|array $index
* Specify a non blocking index for the collection.
*
* @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
*/
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 {
});
$index = $this->getIndex('newcollection', 'mykey');
$this->assertEquals(1, $index);
$this->assertEquals(1, $index['key']['mykey']);
}
public function testUnique()
......@@ -45,7 +45,52 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
});
$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)
......@@ -54,11 +99,7 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
foreach ($collection->getIndexInfo() as $index)
{
if (isset($index['key'][$name]))
{
if (isset($index['unique'])) return 'unique';
return $index['key'][$name];
}
if (isset($index['key'][$name])) return $index;
}
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