Commit 80b01111 authored by Jens Segers's avatar Jens Segers

Adding fluent schema builder support

parent 4c8d9084
......@@ -7,12 +7,21 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnFailure="true"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Laravel MongoDB Test Suite">
<testsuite name="all">
<directory>tests/</directory>
</testsuite>
<testsuite name="schema">
<directory>tests/SchemaTest.php</directory>
</testsuite>
<testsuite name="seeder">
<directory>tests/SeederTest.php</directory>
</testsuite>
<testsuite name="cache">
<directory>tests/CacheTest.php</directory>
</testsuite>
</testsuites>
</phpunit>
......@@ -3,7 +3,14 @@
use Closure;
use Illuminate\Database\Connection;
class Blueprint {
class Blueprint extends \Illuminate\Database\Schema\Blueprint {
/**
* The MongoConnection object for this blueprint.
*
* @var MongoConnection
*/
protected $connection;
/**
* The MongoCollection object for this blueprint.
......@@ -12,6 +19,13 @@ class Blueprint {
*/
protected $collection;
/**
* Fluent columns
*
* @var array
*/
protected $columns = array();
/**
* Create a new schema blueprint.
*
......@@ -21,6 +35,7 @@ class Blueprint {
*/
public function __construct(Connection $connection, $collection)
{
$this->connection = $connection;
$this->collection = $connection->getCollection($collection);
}
......@@ -31,11 +46,26 @@ class Blueprint {
* @param array $options
* @return bool
*/
public function index($columns, $options = array())
public function index($columns = null, $options = array())
{
$result = $this->collection->ensureIndex($columns, $options);
$columns = $this->fluent($columns);
// Columns are passed as a default array
if (is_array($columns) && is_int(key($columns)))
{
// Transform the columns to the required array format
$transform = array();
foreach ($columns as $column)
{
$transform[$column] = 1;
}
$columns = $transform;
}
return (1 == (int) $result['ok']);
$this->collection->ensureIndex($columns, $options);
return $this;
}
/**
......@@ -44,11 +74,16 @@ class Blueprint {
* @param string|array $columns
* @return bool
*/
public function dropIndex($columns)
public function dropIndex($columns = null)
{
$result = $this->collection->deleteIndex($columns);
$columns = $this->fluent($columns);
foreach ($columns as $column)
{
$this->collection->deleteIndex($column);
}
return (1 == (int) $result['ok']);
return $this;
}
/**
......@@ -57,44 +92,70 @@ class Blueprint {
* @param string|array $columns
* @return bool
*/
public function unique($columns)
public function unique($columns = null)
{
return $this->index($columns, array('unique' => true));
$columns = $this->fluent($columns);
$this->index($columns, array('unique' => true));
return $this;
}
/**
* Specify a non blocking index for the collection.
*
*
* @param string|array $columns
* @return bool
*/
public function background($columns)
public function background($columns = null)
{
return $this->index($columns, array('background' => true));
$columns = $this->fluent($columns);
$this->index($columns, array('background' => true));
return $this;
}
/**
* Specify a sparse index for the collection.
*
*
* @param string|array $columns
* @return bool
*/
public function sparse($columns)
public function sparse($columns = null)
{
return $this->index($columns, array('sparse' => true));
$columns = $this->fluent($columns);
$this->index($columns, array('sparse' => true));
return $this;
}
/**
* 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 expire($columns, $seconds)
{
return $this->index($columns, array('expireAfterSeconds' => $seconds));
$columns = $this->fluent($columns);
$this->index($columns, array('expireAfterSeconds' => $seconds));
return $this;
}
/**
* Indicate that the table needs to be created.
*
* @return bool
*/
public function create()
{
$collection = $this->collection->getName();
// Ensure the collection is created
$db = $this->connection->getMongoDB();
$db->createCollection($collection);
}
/**
......@@ -104,9 +165,43 @@ class Blueprint {
*/
public function drop()
{
$result = $this->collection->drop();
$this->collection->drop();
}
return (1 == (int) $result['ok']);
/**
* Add a new column to the blueprint.
*
* @param string $type
* @param string $name
* @param array $parameters
* @return Blueprint
*/
protected function addColumn($type, $name, array $parameters = array())
{
$this->fluent($name);
return $this;
}
/**
* Allow fluent columns
*
* @param string|array $columns
* @return string|array
*/
protected function fluent($columns = null)
{
if (is_null($columns))
{
return $this->columns;
}
else if (is_string($columns))
{
return $this->columns = array($columns);
}
else
{
return $this->columns = $columns;
}
}
}
......@@ -51,7 +51,10 @@ class Builder extends \Illuminate\Database\Schema\Builder {
{
$blueprint = $this->createBlueprint($collection);
return $callback($blueprint);
if ($callback)
{
$callback($blueprint);
}
}
/**
......@@ -75,12 +78,13 @@ class Builder extends \Illuminate\Database\Schema\Builder {
*/
public function create($collection, Closure $callback = null)
{
$db = $this->connection->getMongoDB();
$db->createCollection($collection);
$blueprint = $this->createBlueprint($collection);
$blueprint->create();
if ($callback)
{
return $this->collection($collection, $callback);
$callback($blueprint);
}
}
......@@ -93,7 +97,7 @@ class Builder extends \Illuminate\Database\Schema\Builder {
public function drop($collection)
{
$blueprint = $this->createBlueprint($collection);
return $blueprint->drop();
}
......@@ -108,4 +112,4 @@ class Builder extends \Illuminate\Database\Schema\Builder {
return new Blueprint($this->connection, $collection);
}
}
\ No newline at end of file
}
......@@ -52,7 +52,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
# Add fake host
$db = $app['config']['database.connections']['mongodb'];
$db['host'] = array($db['host'], '1.2.3.4');
$db['host'] = array($db['host'], '0.0.0.0');
$connection = new Connection($db);
$mongoclient = $connection->getMongoClient();
......
......@@ -92,6 +92,22 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(60, $index['expireAfterSeconds']);
}
public function testFluent()
{
Schema::collection('newcollection', function($collection)
{
$collection->string('email')->index();
$collection->string('token')->index();
$collection->timestamp('created_at');
});
$index = $this->getIndex('newcollection', 'email');
$this->assertEquals(1, $index['key']['email']);
$index = $this->getIndex('newcollection', 'token');
$this->assertEquals(1, $index['key']['token']);
}
protected function getIndex($collection, $name)
{
$collection = DB::getCollection($collection);
......
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