-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_migrations.php
57 lines (49 loc) · 1.6 KB
/
run_migrations.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
<?php
use Core\DB;
use Core\H;
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
// load configuration and helper functions
require_once(ROOT . DS . 'config' . DS . 'config.php');
$isCli = php_sapi_name() == 'cli';
if(!RUN_MIGRATIONS_FROM_BROWSER && !$isCli) die('restricted');
function autoload($className){
$classAry = explode('\\',$className);
$class = array_pop($classAry);
$subPath = strtolower(implode(DS,$classAry));
$path = ROOT . DS . $subPath . DS . $class . '.php';
if(file_exists($path)){
require_once($path);
}
}
spl_autoload_register('autoload');
$db = DB::getInstance();
$migrationTable = $db->query("SHOW TABLES LIKE 'migrations'")->results();
$previousMigs = [];
$migrationsRun = [];
if(!empty($migrationTable)){
$query = $db->query("SELECT migration FROM migrations")->results();
foreach($query as $q){
$previousMigs[] = $q->migration;
}
}
// get all files
$migrations = glob('migrations'.DS.'*.php');
foreach($migrations as $fileName){
$klass = str_replace('migrations'.DS,'',$fileName);
$klass = str_replace('.php','',$klass);
if(!in_array($klass,$previousMigs)){
$klassNamespace = 'Migrations\\'.$klass;
$mig = new $klassNamespace($isCli);
$mig->up();
$db->insert('migrations',['migration'=>$klass]);
$migrationsRun[] = $klassNamespace;
}
}
if(sizeof($migrationsRun) == 0){
if($isCli){
echo "\e[0;37;42m\n\n"." No new migrations to run.\n\e[0m\n";
} else {
echo '<p style="color:#006600;">No new migrations to run.</p>';
}
}