-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-db-migrator.php
326 lines (295 loc) · 8.37 KB
/
simple-db-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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/*
* ॐ Om Brahmarppanam ॐ
*
* simple-db-migrator.php
* Created at: Thu Jul 20 2022 19:34:40 GMT+0530 (GMT+05:30)
*
* Copyright 2022 Harish Karumuthil <harish2704@gmail.com>
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*
*/
$dbConf = ["dsn" => "sqlite:./migrationtest.db", "user" => null, "password" => null];
// $dbConf = [ "dsn" => "mysql:host=172.20.1.3;dbname=migrationtest", "user" => "root", "password" => "xxxxxx", ];
// $dbConf = [ "dsn" => "pgsql:host=172.17.0.2;dbname=migrationtest", "user" => "postgres", "password" => "xxxxxx", ];
$MIGRAION_ROOT = __DIR__ . "/migrations";
$L;
class Logger
{
private $levels = ["error", "warning", "info", "log", "debug"];
public function __construct($level)
{
$this->level = $level;
}
private function _log($level, $args)
{
echo date("D M d, Y G:i") . " [$level] : " . implode(", ", $args) . "\n";
}
public function __call($name, $arguments)
{
$targetLevel = array_search($name, $this->levels);
if ($targetLevel !== false && $targetLevel <= $this->level) {
$this->_log($name, $arguments);
}
}
}
class MigrationItem
{
/**
* @param $v {int} Version number
*/
public function __construct($v)
{
global $MIGRAION_ROOT;
$this->v = $v;
$this->upFile = sprintf("%s/up/%03d.sql", $MIGRAION_ROOT, $v);
$this->downFile = sprintf("%s/down/%03d.sql", $MIGRAION_ROOT, $v);
}
public function getSQL($fname)
{
return file_get_contents($fname);
}
public function getUpSQL()
{
return $this->getSQL($this->upFile);
}
public function getDownSql()
{
return $this->getSQL($this->downFile);
}
}
class Migrator
{
public function __construct()
{
global $dbConf, $L;
$this->db = new PDO($dbConf["dsn"], $dbConf["user"], $dbConf["password"]);
$this->L = $L;
}
private function runSQLTransaction($sql)
{
$this->L->debug("Runing SQL");
$this->L->debug($sql);
$res = $this->db->exec("BEGIN;\n" . $sql . "\nCOMMIT;");
}
/*
* Get array of pending version numbers
* @return int[]
*/
public function getPendingMigrations()
{
$lastRanMigration = $this->getLastRanMigration();
$availableMigrations = $this->getAvailableMigrations();
if ($lastRanMigration == 0) {
return $availableMigrations;
}
$lastMigrationIdx = array_search($lastRanMigration, $availableMigrations);
if ($lastMigrationIdx === null) {
throw new Exception(
"Inconsistent state: Last migration is missing in filesystem"
);
}
return array_slice($availableMigrations, $lastMigrationIdx + 1);
}
/*
* Get array of available verion numbers
* @return int[]
*/
private function getAvailableMigrations()
{
global $MIGRAION_ROOT;
$files = scandir("$MIGRAION_ROOT/up");
$out = [];
foreach ($files as $fname) {
$match = [];
$matches = preg_match('/^([0-9]*).sql$/', $fname, $match);
if ($matches > 0) {
$out[] = (int) $match[1];
}
}
sort($out);
return $out;
}
/*
* @return int
*/
private function getLastRanMigration()
{
try {
$result = $this->db
->query(
"SELECT version from db_migrations order by version desc limit 1",
PDO::FETCH_ASSOC
)
->fetchAll();
} catch (Exception $e) {
$this->L->error("db_migrations table doesn't exists. Please run setup");
throw $e;
}
if ($result) {
return $result[0]["version"];
}
return 0;
}
public function runUp()
{
$this->L->warning("Running up");
$pendingMigrations = $this->getPendingMigrations();
$this->L->warning("Pending migrations " . json_encode($pendingMigrations));
foreach ($pendingMigrations as $migrationV) {
$this->L->warning("Running migration " . $migrationV);
$migrationItem = new MigrationItem($migrationV);
$sql = $migrationItem->getUpSQL();
$this->runSQLTransaction($sql);
$this->db
->prepare(
"INSERT INTO db_migrations
(version, created_at, up_sql, down_sql) VALUES (?, ?, ?, ?)"
)
->execute([
$migrationV,
date("Y-m-d H:i:s"),
$sql,
$migrationItem->getDownSql(),
]);
}
$this->L->warning("executed all pending migrations");
}
public function setup()
{
try {
$result = $this->db
->query(
"SELECT version from db_migrations order by version desc limit 1",
PDO::FETCH_ASSOC
)
->fetchAll();
} catch (Exception $e) {
$this->L->warning("db_migrations table doesn't exists.");
$this->L->info("Creating db_migrations table ...");
return $this->db->query("
CREATE TABLE db_migrations (
version int NOT NULL,
created_at VARCHAR(20) DEFAULT NULL,
up_sql text DEFAULT NULL,
down_sql text DEFAULT NULL,
PRIMARY KEY (version)
)");
}
$this->L->warning("db_migrations table already exists. Skipping setup");
}
/*
* @return string
*/
private function getDownSqlFromDb($v)
{
$res = $this->db
->query(
"select down_sql from db_migrations where version = $v",
PDO::FETCH_ASSOC
)
->fetchAll();
return $res[0]["down_sql"];
}
public function runDown()
{
$this->L->warning("Rolling back last migration ...");
$lastRanMigration = $this->getLastRanMigration();
if (!$lastRanMigration) {
throw new Exception("There is no migration to rollback");
}
$this->L->warning("last migration is $lastRanMigration");
$migrationItem = new MigrationItem($lastRanMigration);
$downSqlFromDisk = $migrationItem->getDownSql();
$downSqlFromDb = $this->getDownSqlFromDb($lastRanMigration);
if ($downSqlFromDisk != $downSqlFromDb) {
$this->L->error(
"rollback sql stored in db does not match with the sql in filesystem"
);
$this->L->error("SQL from db");
$this->L->error($downSqlFromDb);
$this->L->error("SQL from filesystem");
$this->L->error($downSqlFromDisk);
$this->L->error("Please manually fix this error and run again");
throw new Exception(
"rollback sql stored in db does not match with the sql in filesystem"
);
}
$this->runSQLTransaction($downSqlFromDisk);
$this->db
->prepare("DELETE FROM db_migrations WHERE version = ?")
->execute([$lastRanMigration]);
$this->L->warning("Rollback completed");
}
function currentStatus()
{
$lastRanMigration = $this->getLastRanMigration();
$this->L->warning("last migration is $lastRanMigration");
$pendingMigrations = $this->getPendingMigrations();
$this->L->warning("Pending migrations " . json_encode($pendingMigrations));
}
}
class Application
{
public function __construct()
{
$this->opts = getopt("ldsvh", ["list", "down", "setup", "verbose", "help"]);
$this->logLevel = 1;
$verbose = $this->getOption("verbose");
if ($verbose) {
if ($verbose === true) {
$verbose = [$verbose];
}
$this->logLevel += count($verbose);
}
}
private function getOption($name)
{
foreach ([$name[0], $name] as $k) {
if (isset($this->opts[$k])) {
return $this->opts[$k] === false ? true : $this->opts[$k];
}
}
}
function help()
{
echo "Description:
A simple database migration tool
Usage:
php simple-db-migrator.php [options]
Options:
-l, --list Show the current status of applied migrations
-s, --setup Create db_migrations table in db and run all pending migrations
-d, --down Roll back last migration
-h, --help Display help for the given command. When no command is given display help for the db:migrate command
-v|vv|vvv, --verbose Increase the verbosity of messages: 1-3 => info,log,debug
";
}
function execute()
{
global $L;
if ($this->getOption("help")) {
return $this->help();
}
$L = new Logger($this->logLevel);
$L->info("Starting migrator");
$migrator = new Migrator();
if ($this->getOption("list")) {
$migrator->currentStatus();
return;
}
if ($this->getOption("setup")) {
$migrator->setup();
}
if ($this->getOption("down")) {
$migrator->runDown();
} else {
$migrator->runUp();
}
return 0;
}
}
(new Application())->execute();