1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Migrations\Repositories; |
4
|
|
|
|
5
|
|
|
use Phalcon\Db\Adapter\Pdo; |
6
|
|
|
use Yarak\Migrations\CreateMigrationsTable; |
7
|
|
|
|
8
|
|
|
class DatabaseMigrationRepository implements MigrationRepository |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The active database connection. |
12
|
|
|
* |
13
|
|
|
* @var Pdo |
14
|
|
|
*/ |
15
|
|
|
protected $connection = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Set the repository connection on the object. |
19
|
|
|
* |
20
|
|
|
* @param Pdo $connection |
21
|
|
|
*/ |
22
|
|
|
public function setConnection($connection) |
23
|
|
|
{ |
24
|
|
|
$this->connection = $connection; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return true if repository resource exists. |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function exists() |
33
|
|
|
{ |
34
|
|
|
return $this->connection->tableExists('migrations'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new repository resource. |
39
|
|
|
*/ |
40
|
|
|
public function create() |
41
|
|
|
{ |
42
|
|
|
$migration = new CreateMigrationsTable(); |
43
|
|
|
|
44
|
|
|
$migration->up($this->connection); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return the migrations that have already been ran. |
49
|
|
|
* |
50
|
|
|
* @param array $ran |
51
|
|
|
* @param int $steps |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getRan($ran = null, $steps = null) |
56
|
|
|
{ |
57
|
|
|
if ($ran !== null) { |
58
|
|
|
return $ran; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($steps === null) { |
62
|
|
|
return $this->connection->fetchAll('SELECT * FROM migrations'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$lastBatchNumber = $this->getLastBatchNumber($ran); |
66
|
|
|
|
67
|
|
|
$toRollback = $lastBatchNumber - $steps; |
68
|
|
|
|
69
|
|
|
return $this->connection |
70
|
|
|
->fetchAll("SELECT * FROM migrations WHERE batch > {$toRollback}"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get array of all migrations that have been run. |
75
|
|
|
* |
76
|
|
|
* @param array $ran |
77
|
|
|
* @param int $steps |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getRanMigrations($ran = null, $steps = null) |
82
|
|
|
{ |
83
|
|
|
$ran = $this->getRan($ran, $steps); |
84
|
|
|
|
85
|
|
|
return array_map(function ($item) { |
86
|
|
|
return $item['migration']; |
87
|
|
|
}, $ran); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get array of all migration batch nubmers that have run. |
92
|
|
|
* |
93
|
|
|
* @param array $ran |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getRanBatchNumbers($ran = null) |
98
|
|
|
{ |
99
|
|
|
$ran = $this->getRan($ran); |
100
|
|
|
|
101
|
|
|
return array_map(function ($item) { |
102
|
|
|
return $item['batch']; |
103
|
|
|
}, $ran); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the last number found in the repository. |
108
|
|
|
* |
109
|
|
|
* @param array $ran |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function getLastBatchNumber($ran = null) |
114
|
|
|
{ |
115
|
|
|
$batchNumbers = $this->getRanBatchNumbers($ran); |
116
|
|
|
|
117
|
|
|
return empty($batchNumbers) ? 0 : max($batchNumbers); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the next available batch number. |
122
|
|
|
* |
123
|
|
|
* @param array $ran |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
|
|
public function getNextBatchNumber($ran = null) |
128
|
|
|
{ |
129
|
|
|
return $this->getLastBatchNumber($ran) + 1; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Insert a new record in the repository. |
134
|
|
|
* |
135
|
|
|
* @param string $fileName |
136
|
|
|
* @param int $batch |
137
|
|
|
*/ |
138
|
|
|
public function insertRecord($fileName, $batch) |
139
|
|
|
{ |
140
|
|
|
$this->connection->insert( |
141
|
|
|
'migrations', |
142
|
|
|
[$fileName, $batch], |
143
|
|
|
['migration', 'batch'] |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Delete a record from the repository. |
149
|
|
|
* |
150
|
|
|
* @param string $fileName |
151
|
|
|
*/ |
152
|
|
|
public function deleteRecord($fileName) |
153
|
|
|
{ |
154
|
|
|
$this->connection->delete( |
155
|
|
|
'migrations', |
156
|
|
|
'migration = ?', |
157
|
|
|
[$fileName] |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|