-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.php
More file actions
61 lines (49 loc) · 1.55 KB
/
migrate.php
File metadata and controls
61 lines (49 loc) · 1.55 KB
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
<?php
/**
* Database migration CLI runner.
*
* Usage:
* php migrate.php — show status and apply all pending migrations
* php migrate.php status — show status only, do not apply
*
* Run after every git pull that includes new migration files.
*/
if (php_sapi_name() !== 'cli') {
http_response_code(403);
exit("This script must be run from the command line.\n");
}
require(__DIR__ . '/functions/autoload.php');
$command = $argv[1] ?? 'apply';
$current = $Migration->get_current_version();
$latest = $Migration->get_latest_version();
$pending = $Migration->get_pending();
$pending_count = count($pending);
echo "DB version : {$current}\n";
echo "Latest : {$latest}\n";
echo "Pending : {$pending_count}\n";
if ($pending_count > 0) {
echo "\nPending migrations:\n";
foreach ($pending as $f) {
echo " - {$f}\n";
}
}
if ($command === 'status' || $pending_count === 0) {
echo ($pending_count === 0 ? "\nDatabase is up to date." : "\nRun without 'status' to apply.") . "\n";
exit(0);
}
// Apply
echo "\nApplying {$pending_count} migration(s)...\n";
$results = $Migration->apply_all();
$exit_code = 0;
foreach ($results as $r) {
if ($r['success']) {
echo " [OK] " . $r['file'] . "\n";
} else {
echo " [FAIL] " . $r['file'] . " — " . $r['error'] . "\n";
$exit_code = 1;
}
}
$applied = count(array_filter($results, fn($r) => $r['success']));
$new_version = $Migration->get_current_version();
echo "\nDone. {$applied}/{$pending_count} applied. DB version now: {$new_version}\n";
exit($exit_code);