-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.php
114 lines (94 loc) · 4.27 KB
/
migrate.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
<?php
session_start();
date_default_timezone_set('Asia/Manila');
include('./core/env.php');
$env = new Env();
$env->init();
include('./config/constant.php');
include('./core/render.php');
include('./core/database.php');
include('./core/utils.php');
include('./core/guard.php');
$db = new Database($config);
$db->connect();
$migration_table = $db->parse_table("migration");
$query = $db->db->query("SELECT * FROM information_schema.tables WHERE TABLE_NAME = '{$migration_table}' and TABLE_SCHEMA = '".$config['DB_NAME']."' ");
$result = $query->fetchAll(PDO::FETCH_ASSOC);
$response = $result;
if (empty($response)) {
$query = $db->db->prepare("CREATE TABLE `{$migration_table}` (timestamp varchar(50) not null)");
$query->execute();
}
$migrations = glob('./migrations/*.sql', GLOB_BRACE);
if (!empty($migrations)) {
array_multisort(
array_map( 'filemtime', $migrations ),
SORT_NUMERIC,
SORT_ASC,
$migrations
);
$mapped = array_map(function ($value) {
return str_replace('.sql', '', basename($value));
}, $migrations);
$migrated = $db->db->query("SELECT * FROM " . $migration_table);
$migrated = $migrated->fetchAll(PDO::FETCH_ASSOC);
$mapped_migrated = array_map(function ($value) {
return $value['timestamp'];
}, $migrated);
$diffs = array_diff($mapped, $mapped_migrated);
if (!empty($diffs)) {
foreach ($diffs as $diff) {
try {
$sql = file_get_contents('./migrations/'.$diff.'.sql');
// PATTERNS
$create_table_pattern = '/CREATE TABLE `([^`]+)`\s*\((.*?)\);/s';
$alter_table_pattern = '/ALTER TABLE `([^`]+)`\s*(.*?);/';
$delete_table_pattern = '/DROP TABLE `([^`]+)`\s*;/';
$insert_table_pattern = '/INSERT INTO `([^`]+)`\s*\((.*?)\) VALUES \((.*?)\);/';
$pattern = '';
$mode = 'create';
if (strpos($sql, "CREATE TABLE") !== false) {
$pattern = $create_table_pattern;
} else if (strpos($sql, "ALTER TABLE") !== false) {
$pattern = $alter_table_pattern;
$mode = 'alter';
} else if (strpos($sql, "DROP TABLE") !== false) {
$pattern = $delete_table_pattern;
$mode = 'delete';
} else if (strpos($sql, "INSERT INTO") !== false) {
$pattern = $insert_table_pattern;
$mode = 'insert';
}
preg_match($pattern, $sql, $matches);
if ($mode === 'create' && count($matches) >= 3) {
$tableName = $matches[1];
// Replace the table name with the prefix
$newTableName = $db->parse_table($tableName);
$sql = preg_replace("/CREATE TABLE `$tableName`/", "CREATE TABLE `$newTableName`", $sql);
} else if ($mode === 'alter' && count($matches) >= 3) {
$tableName = $matches[1];
// Replace the table name with the prefix
$newTableName = $db->parse_table($tableName);
$sql = preg_replace("/ALTER TABLE `$tableName`/", "ALTER TABLE `$newTableName`", $sql);
} else if ($mode === 'delete' && count($matches) >= 2) {
$tableName = $matches[1];
// Replace the table name with the prefix
$newTableName = $db->parse_table($tableName);
$sql = preg_replace("/DROP TABLE `$tableName`/", "DROP TABLE `$newTableName`", $sql);
} else if ($mode === 'insert' && count($matches) >= 4) {
$tableName = $matches[1];
// Replace the table name with the prefix
$newTableName = $db->parse_table($tableName);
$sql = preg_replace("/INSERT INTO `$tableName`/", "INSERT INTO `$newTableName`", $sql);
}
$db->db->query("INSERT INTO {$migration_table} (`timestamp`) VALUES ('{$diff}')");
$db->db->query($sql);
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
}
}
redirect('/');
}
;?>