forked from drlippman/IMathAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator.php
141 lines (116 loc) · 4.1 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
<?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()
{
$latestVersionApplied = $this->getLatestVersionApplied();
$migrationsAppliedThisRun = 0;
$migrationFilenames = glob(__DIR__ . '/migrations/*.php');
natsort($migrationFilenames);
foreach ($migrationFilenames as $migrationFilename) {
$migrationVersion = $this->getMigrationFileVersion($migrationFilename);
if ($migrationVersion <= $latestVersionApplied) {
continue;
}
$result = $this->migrateSingle($migrationFilename);
if (!$result) {
printf("<p style='color: #ff0000;'>Migration FAILED: %s</p>\n", basename($migrationFilename));
return null;
} else {
$this->storeLatestVersionApplied($migrationVersion);
++$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($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;
}
/**
* Store the latest successfully applied migration version to the DB.
*/
private function storeLatestVersionApplied($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.
*
* @return float The last successfully applied migration version.
*/
public function getLatestVersionApplied()
{
$stm = $this->DBH->query("SELECT ver FROM imas_dbschema WHERE id=1");
$lastVersion = $stm->fetchColumn(0);
return $lastVersion;
}
/**
* Get the last available migration version in the migration directory
*
* @return float The last available migration version.
*/
public function getLatestVersionAvailable()
{
$migrationFilenames = glob(__DIR__ . '/migrations/*.php');
if (count($migrationFilenames)==0) {
return 0;
}
natsort($migrationFilenames);
$lastVersion = $this->getMigrationFileVersion($migrationFilenames[count($migrationFilenames)-1]);
return $lastVersion;
}
}