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

Adding fluent schema builder support

parent 4c8d9084
...@@ -7,12 +7,21 @@ ...@@ -7,12 +7,21 @@
convertNoticesToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true" convertWarningsToExceptions="true"
processIsolation="false" processIsolation="false"
stopOnFailure="false" stopOnFailure="true"
syntaxCheck="false" syntaxCheck="false"
> >
<testsuites> <testsuites>
<testsuite name="Laravel MongoDB Test Suite"> <testsuite name="all">
<directory>tests/</directory> <directory>tests/</directory>
</testsuite> </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> </testsuites>
</phpunit> </phpunit>
...@@ -3,7 +3,14 @@ ...@@ -3,7 +3,14 @@
use Closure; use Closure;
use Illuminate\Database\Connection; 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. * The MongoCollection object for this blueprint.
...@@ -12,6 +19,13 @@ class Blueprint { ...@@ -12,6 +19,13 @@ class Blueprint {
*/ */
protected $collection; protected $collection;
/**
* Fluent columns
*
* @var array
*/
protected $columns = array();
/** /**
* Create a new schema blueprint. * Create a new schema blueprint.
* *
...@@ -21,6 +35,7 @@ class Blueprint { ...@@ -21,6 +35,7 @@ class Blueprint {
*/ */
public function __construct(Connection $connection, $collection) public function __construct(Connection $connection, $collection)
{ {
$this->connection = $connection;
$this->collection = $connection->getCollection($collection); $this->collection = $connection->getCollection($collection);
} }
...@@ -31,11 +46,26 @@ class Blueprint { ...@@ -31,11 +46,26 @@ class Blueprint {
* @param array $options * @param array $options
* @return bool * @return bool
*/ */
public function index($columns, $options = array()) public function index($columns = null, $options = array())
{
$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)
{ {
$result = $this->collection->ensureIndex($columns, $options); $transform[$column] = 1;
}
return (1 == (int) $result['ok']); $columns = $transform;
}
$this->collection->ensureIndex($columns, $options);
return $this;
} }
/** /**
...@@ -44,11 +74,16 @@ class Blueprint { ...@@ -44,11 +74,16 @@ class Blueprint {
* @param string|array $columns * @param string|array $columns
* @return bool * @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,9 +92,12 @@ class Blueprint { ...@@ -57,9 +92,12 @@ class Blueprint {
* @param string|array $columns * @param string|array $columns
* @return bool * @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;
} }
/** /**
...@@ -68,9 +106,12 @@ class Blueprint { ...@@ -68,9 +106,12 @@ class Blueprint {
* @param string|array $columns * @param string|array $columns
* @return bool * @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;
} }
/** /**
...@@ -79,9 +120,12 @@ class Blueprint { ...@@ -79,9 +120,12 @@ class Blueprint {
* @param string|array $columns * @param string|array $columns
* @return bool * @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;
} }
/** /**
...@@ -94,7 +138,24 @@ class Blueprint { ...@@ -94,7 +138,24 @@ class Blueprint {
*/ */
public function expire($columns, $seconds) 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 { ...@@ -104,9 +165,43 @@ class Blueprint {
*/ */
public function drop() public function drop()
{ {
$result = $this->collection->drop(); $this->collection->drop();
}
/**
* 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;
}
return (1 == (int) $result['ok']); /**
* 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 { ...@@ -51,7 +51,10 @@ class Builder extends \Illuminate\Database\Schema\Builder {
{ {
$blueprint = $this->createBlueprint($collection); $blueprint = $this->createBlueprint($collection);
return $callback($blueprint); if ($callback)
{
$callback($blueprint);
}
} }
/** /**
...@@ -75,12 +78,13 @@ class Builder extends \Illuminate\Database\Schema\Builder { ...@@ -75,12 +78,13 @@ class Builder extends \Illuminate\Database\Schema\Builder {
*/ */
public function create($collection, Closure $callback = null) public function create($collection, Closure $callback = null)
{ {
$db = $this->connection->getMongoDB(); $blueprint = $this->createBlueprint($collection);
$db->createCollection($collection);
$blueprint->create();
if ($callback) if ($callback)
{ {
return $this->collection($collection, $callback); $callback($blueprint);
} }
} }
......
...@@ -52,7 +52,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -52,7 +52,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
# Add fake host # Add fake host
$db = $app['config']['database.connections']['mongodb']; $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); $connection = new Connection($db);
$mongoclient = $connection->getMongoClient(); $mongoclient = $connection->getMongoClient();
......
...@@ -92,6 +92,22 @@ class SchemaTest extends PHPUnit_Framework_TestCase { ...@@ -92,6 +92,22 @@ class SchemaTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(60, $index['expireAfterSeconds']); $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) protected function getIndex($collection, $name)
{ {
$collection = DB::getCollection($collection); $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