Skip to content

Commit b1489a8

Browse files
Merge pull request #138 from Zeyckler/newMongoDbAdapter
New MongoDb adapter
2 parents 0c6bb92 + 770a363 commit b1489a8

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"mockery/mockery": "*@dev"
2525
},
2626
"suggest": {
27-
"pimple/pimple": "Pimple allows to bootstrap phpmig really easily."
27+
"pimple/pimple": "Pimple allows to bootstrap phpmig really easily.",
28+
"mongodb/mongodb": "This library is required for the MongoDb adapter"
2829
},
2930
"autoload": {
3031
"psr-0": {

src/Phpmig/Adapter/MongoDb.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Phpmig\Adapter;
4+
5+
use MongoDB\Database;
6+
use Phpmig\Migration\Migration;
7+
8+
/**
9+
* @author Carlos Barrero https://github.com/Zeyckler
10+
*/
11+
class MongoDb implements AdapterInterface
12+
{
13+
14+
/**
15+
* @var Database
16+
*/
17+
protected $connection = null;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $tableName = null;
23+
24+
/**
25+
* @param Database $connection
26+
* @param string $tableName
27+
*/
28+
public function __construct(Database $connection, $tableName)
29+
{
30+
$this->connection = $connection;
31+
$this->tableName = $tableName;
32+
}
33+
34+
/**
35+
* Fetch all
36+
*
37+
* @return array
38+
*/
39+
public function fetchAll()
40+
{
41+
$cursor = $this->connection->selectCollection($this->tableName)->find(
42+
array(),
43+
array('$project' => array('version' => 1))
44+
);
45+
46+
return array_map(
47+
function ($document) {
48+
return $document['version'];
49+
},
50+
$cursor->toArray()
51+
);
52+
}
53+
54+
/**
55+
* Up
56+
*
57+
* @param Migration $migration
58+
*
59+
* @return AdapterInterface
60+
*/
61+
public function up(Migration $migration)
62+
{
63+
$document = array('version' => $migration->getVersion());
64+
$this->connection->selectCollection($this->tableName)->insertOne($document);
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Down
71+
*
72+
* @param Migration $migration
73+
*
74+
* @return AdapterInterface
75+
*/
76+
public function down(Migration $migration)
77+
{
78+
$document = array('version' => $migration->getVersion());
79+
$this->connection->selectCollection($this->tableName)->deleteOne($document);
80+
81+
return $this;
82+
}
83+
84+
85+
/**
86+
* Is the schema ready?
87+
*
88+
* @return bool
89+
*/
90+
public function hasSchema()
91+
{
92+
foreach ($this->connection->listCollections() as $collection) {
93+
if ($collection->getName() === $this->tableName) {
94+
return true;
95+
}
96+
}
97+
98+
return false;
99+
}
100+
101+
102+
/**
103+
* Create Schema
104+
*
105+
* @return AdapterInterface
106+
*/
107+
public function createSchema()
108+
{
109+
$key = array('version' => 1);
110+
$options = array('unique' => true);
111+
$this->connection->selectCollection($this->tableName)->createIndex($key, $options);
112+
113+
return $this;
114+
}
115+
}

0 commit comments

Comments
 (0)