-
Notifications
You must be signed in to change notification settings - Fork 99
/
migrator.php
177 lines (149 loc) · 5.45 KB
/
migrator.php
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
<?php
/**
* Execute all PHP files found in the migrations/ directory in natural order.
*/
class Migrator
{
private $DBH;
private $isInitialDBsetup;
public function __construct($DBH, $isDBsetup)
{
$this->DBH = $DBH;
$this->isInitialDBsetup = $isDBsetup;
}
/**
* Execute all migration files found.
*
* @return string Version of last migration applied. Null on migration errors.
*/
public function migrateAll()
{
$latestOldStyleVersionApplied = $this->getLatestVersionAppliedOldStyle();
$appliedMigrations = $this->getAppliedMigrations();
$migrationsAppliedThisRun = 0;
$migrationFilenames = glob(__DIR__ . '/migrations/*.php');
natsort($migrationFilenames);
foreach ($migrationFilenames as $migrationFilename) {
$migrationVersion = $this->getMigrationFileVersion($migrationFilename);
if ($migrationVersion <= $latestOldStyleVersionApplied) {
continue;
} else if ($migrationVersion>156 && in_array($migrationVersion, $appliedMigrations)) {
continue;
}
$result = $this->migrateSingle($migrationFilename);
if (!$result) {
printf("<p style='color: #ff0000;'>Migration FAILED: %s</p>\n", basename($migrationFilename));
return null;
} else {
if ($migrationVersion <= 156) { //156 is the last old-style migration
$this->storeLatestVersionAppliedOldStyle($migrationVersion);
} else {
$this->storeMigrationApplied($migrationVersion, $migrationFilename);
}
++$migrationsAppliedThisRun;
}
}
if ($migrationsAppliedThisRun > 0) {
printf("<p>Successfully applied %d migrations.</p>\n", $migrationsAppliedThisRun);
}
return $this->getLatestVersionApplied();
}
/**
* Execute a single migration file.
*
* @param $migrationFilename string migration file to execute.
* @return mixed True if the migration succeeded. False if not.
*/
private function migrateSingle($migrationFilename)
{
// Allow migration scripts to access $this->DBH.
$DBH = $this->DBH;
$isDBsetup = $this->isInitialDBsetup;
printf("<p>Applying migration: %s</p>\n", basename($migrationFilename));
if (!is_readable($migrationFilename)) {
printf("<p style='color: #ff0000'>Unable to read migration file: %s</p>\n", $migrationFilename);
return false;
}
$result = require_once $migrationFilename;
if ($result !== true && $result !== false) {
printf("<p style='color: #ff0000'>Invalid result returned from migration: %s</p>\n", $migrationFilename);
return false;
}
return $result;
}
/**
* Extract and return the version number from a migration file name.
*
* @param $migrationFilename string The migration filename.
* @return string the version of the migration file.
*/
private function getMigrationFileVersion($migrationFilename)
{
$justFilename = basename($migrationFilename);
$migrationVersion = explode("_", $justFilename)[0];
return $migrationVersion;
}
/**
* Get all applied migrations (new style)
*/
private function getAppliedMigrations()
{
$stm = $this->DBH->query("SELECT id FROM imas_dbschema WHERE id>156");
return $stm->fetchAll(PDO::FETCH_COLUMN);
}
/**
* Store that the migration was applied.
*/
private function storeMigrationApplied($version, $filename)
{
$stm = $this->DBH->prepare("INSERT INTO imas_dbschema (id,ver,details) VALUES (:id,:ver,:details)");
$stm->execute(array(':id'=>$version, ':ver'=>time(), ':details'=>basename($filename)));
if (false === $stm) {
printf("<p style='color: #ff0000'>Unable to store migration version: %s</p>\n", $version);
}
}
/**
* Store the latest successfully applied migration version to the DB.
*/
private function storeLatestVersionAppliedOldStyle($version)
{
$stm = $this->DBH->prepare("UPDATE imas_dbschema SET ver=:ver WHERE id=1");
$stm->execute(array(':ver'=>$version));
if (false === $stm) {
printf("<p style='color: #ff0000'>Unable to store migration version: %s</p>\n", $version);
}
}
/**
* Get the last successfully applied migration version from the DB,
* of the older format migrations (sequential), the last of which was 156.
*
* @return float The last successfully applied migration version.
*/
public function getLatestVersionAppliedOldStyle()
{
$stm = $this->DBH->query("SELECT ver FROM imas_dbschema WHERE id=1");
$lastVersion = $stm->fetchColumn(0);
return $lastVersion;
}
/**
* Get the last successfully applied migration version from the DB,
* new format, assuming in numerical order
*
* @return float The last successfully applied migration version.
*/
public function getLatestVersionApplied()
{
$lastOldVersion = $this->getLatestVersionAppliedOldStyle();
if ($lastOldVersion<156) {
return $lastOldVersion;
} else {
$stm = $this->DBH->query("SELECT id FROM imas_dbschema WHERE ver>0 AND id>156 ORDER BY id DESC LIMIT 1");
$lastVersion = $stm->fetchColumn(0);
if ($lastVersion === false) {
return $lastOldVersion;
} else {
return $lastVersion;
}
}
}
}