1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
<?php
namespace gipfl\DbMigration;
use DirectoryIterator;
use Exception;
use gipfl\ZfDb\Adapter\Exception\AdapterException;
use gipfl\ZfDb\Adapter\Adapter as Db;
use gipfl\ZfDb\Adapter\Pdo\Mysql;
use gipfl\ZfDb\Adapter\Pdo\Pgsql;
use InvalidArgumentException;
use Zend_Db_Adapter_Pdo_Abstract as ZfDb;
use Zend_Db_Adapter_Pdo_Mysql as ZfMysql;
use Zend_Db_Adapter_Pdo_Pgsql as ZfPgsql;
class Migrations
{
const DB_TYPE_MYSQL = 'mysql';
const DB_TYPE_POSTGRESQL = 'pgsql';
/** @var Db */
protected $db;
/** @var string mysql or pgsql */
protected $dbType;
/** @var string */
protected $schemaDirectory;
/** @var string */
protected $tableName;
/**
* Migrations constructor.
*
* @param Db|ZfDb $db
* @param string $schemaDirectory
* @param string $tableName
*/
public function __construct($db, $schemaDirectory, $tableName = 'schema_migration')
{
if (! ($db instanceof Db || $db instanceof ZfDb)) {
throw new InvalidArgumentException('$db must be an valid Zend_Db PDO adapter');
}
$this->db = $db;
if ($db instanceof Mysql || $db instanceof ZfMysql) {
$this->dbType = self::DB_TYPE_MYSQL;
} elseif ($db instanceof Pgsql || $db instanceof ZfPgsql) {
$this->dbType = self::DB_TYPE_POSTGRESQL;
} else {
throw new InvalidArgumentException(sprintf(
'Migrations are currently supported for MySQL and PostgreSQL only, got %s',
get_class($db)
));
}
$this->tableName = (string) $tableName;
$this->schemaDirectory = (string) $schemaDirectory;
}
/**
* Still unused
*
* @throws AdapterException|\Zend_Db_Adapter_Exception
*/
protected function createMigrationsTable()
{
if ($this->dbType === self::DB_TYPE_POSTGRESQL) {
$create = /** @lang text */
<<<SQL
CREATE TABLE {$this->tableName} (
schema_version SMALLINT NOT NULL,
migration_time TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY (schema_version)
);
SQL;
} else {
$create = /** @lang text */
<<<SQL
CREATE TABLE {$this->tableName} (
schema_version SMALLINT UNSIGNED NOT NULL,
migration_time DATETIME NOT NULL,
PRIMARY KEY (schema_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin;
SQL;
}
$this->db->exec($create);
}
/**
* @return int
*/
public function getLastMigrationNumber()
{
try {
$query = $this->db->select()->from(
['m' => $this->getTableName()],
['schema_version' => 'MAX(schema_version)']
);
return (int) $this->db->fetchOne($query);
} catch (Exception $e) {
return 0;
}
}
/**
* @return string
*/
protected function getTableName()
{
return $this->tableName;
}
/**
* @return bool
*/
public function hasAnyTable()
{
return count($this->db->listTables()) > 0;
}
/**
* @return bool
*/
public function hasTable($tableName)
{
return in_array($tableName, $this->db->listTables());
}
/**
* @return bool
*/
public function hasMigrationsTable()
{
return $this->hasTable($this->tableName);
}
/**
* @return bool
*/
public function hasSchema()
{
return $this->listPendingMigrations() !== [0];
}
/**
* @return bool
*/
public function hasPendingMigrations()
{
return $this->countPendingMigrations() > 0;
}
/**
* @return int
*/
public function countPendingMigrations()
{
return count($this->listPendingMigrations());
}
/**
* @return Migration[]
*/
public function getPendingMigrations()
{
$migrations = array();
foreach ($this->listPendingMigrations() as $version) {
$migrations[] = new Migration(
$version,
$this->loadMigrationFile($version)
);
}
return $migrations;
}
/**
* @return $this
*/
public function applyPendingMigrations()
{
foreach ($this->getPendingMigrations() as $migration) {
$migration->apply($this->db);
}
return $this;
}
/**
* @return int[]
*/
public function listPendingMigrations()
{
$lastMigration = $this->getLastMigrationNumber();
if ($lastMigration === 0) {
return [0];
}
return $this->listMigrationsAfter($this->getLastMigrationNumber());
}
/**
* @return int[]
*/
public function listAllMigrations()
{
$dir = $this->getMigrationsDirectory();
$versions = [];
if (! is_readable($dir)) {
return $versions;
}
foreach (new DirectoryIterator($dir) as $file) {
if ($file->isDot()) {
continue;
}
$filename = $file->getFilename();
if (preg_match('/^upgrade_(\d+)\.sql$/', $filename, $match)) {
$versions[] = (int) $match[1];
}
}
sort($versions);
return $versions;
}
/**
* @param $version
* @return false|string
*/
public function loadMigrationFile($version)
{
if ($version === 0) {
$filename = $this->getFullSchemaFile();
} else {
$filename = sprintf(
'%s/upgrade_%d.sql',
$this->getMigrationsDirectory(),
$version
);
}
return file_get_contents($filename);
}
/**
* @param $version
* @return int[]
*/
protected function listMigrationsAfter($version)
{
$filtered = [];
foreach ($this->listAllMigrations() as $available) {
if ($available > $version) {
$filtered[] = $available;
}
}
return $filtered;
}
/**
* @param ?string $subDirectory
* @return string
*/
public function getSchemaDirectory($subDirectory = null)
{
if ($subDirectory === null) {
return $this->schemaDirectory;
} else {
return $this->schemaDirectory . '/' . ltrim($subDirectory, '/');
}
}
/**
* @return string
*/
public function getMigrationsDirectory()
{
return $this->getSchemaDirectory($this->dbType . '-migrations');
}
/**
* @return string
*/
protected function getFullSchemaFile()
{
return $this->getSchemaDirectory(
$this->dbType. '.sql'
);
}
}
|