-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakemigration
executable file
·71 lines (56 loc) · 1.77 KB
/
makemigration
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
#!/usr/bin/php
<?php
set_include_path(__DIR__);
require_once 'app/core/migration/Migration.php';
require_once 'libs/dbStruct.php';
$db = Database::get_instance();
// Get the model
if (count($argv)==1) {
die("\tInvalid Syntax\n\tUsage: makemigration model_name\n\n");
}
$model = $argv[1];
// TODO: Check if valid model
require_once "app/models/$model.php";
$model = basename($model);
$obj = new $model();
$final_sql = "";
// If the model does not exist, create it
if (!$obj->exists()) {
$sql = $obj->get_create_table_sql();
$final_sql = $sql;
}
// If it does, get last sql and new sql and
// compare them for upgrade sql
else {
$table = $obj->get_table_name();
$result = $db->query_with_error("SHOW CREATE TABLE $table");
if ($row = $result->fetch_assoc()) {
$last_sql = $row["Create Table"];
$current_sql = $obj->get_create_table_sql();
$updater = new dbStructUpdater();
$res = $updater->getUpdates($last_sql, $current_sql, true);
$final_sql = $res;
}
}
if (trim($final_sql) == '') {
echo "No new changes detected in the schema!\n";
} else {
// check if last migration and this one are same
$version = Migration::get_migration_version($model);
$last_migration_sql = Migration::get_migration($model, $version);
if ($last_migration_sql == $final_sql) {
echo "No new changes detected.\n";
echo "However you may have unapplied migrations.\n";
die();
}
$version = $version+1;
$output_file = "migrations/" . $model . "_" . $version . ".sql";
echo "$final_sql\n";
echo "Saving to ... $output_file\n";
if (!is_dir(ROOTDIR."/migrations")) {
mkdir(ROOTDIR."/migrations");
}
file_put_contents(ROOTDIR."/$output_file", $final_sql);
echo "Done !\n";
}
?>