|
| 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