-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle-dbstruct.php
More file actions
executable file
·142 lines (123 loc) · 4.39 KB
/
handle-dbstruct.php
File metadata and controls
executable file
·142 lines (123 loc) · 4.39 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
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
#!/usr/bin/php
<?PHP
error_reporting((E_ALL | E_STRICT));
error_reporting(error_reporting() ^ E_NOTICE);
require_once("global.inc.php");
require_once("config.localonly.inc.php");
$params = getParams();
// application init
chdir(__DIR__ . "/$webRoot");
echo "\nCWD=" . getcwd() . "\n\n";
$skipLogonCheck = true;
require_once("php/init.inc.php");
set_exception_handler(null);
Config::init();
if (class_exists("Dbw")) {
Dbw::openDbAliasId($dbDefAliasId);
$conn = Dbw::$conn;
$dbName = Config::$dbDefs[$dbDefAliasId]['dbName'];
}
else { // fallback to pre12
$dbDef = Config::$dbDefs[$dbDefAliasId];
Db::init($dbDef['dbName'], $dbDef['dbUser'], $dbDef['dbPass'], $dbDef['dbAttributes']);
$conn = Db::getConn();
list($dbDriver, $driverSpecificPart) = explode(':', $dbDef['dbName'], 2);
$tmpParts = explode(';', $driverSpecificPart);
$dsnParts = array();
foreach ($tmpParts as $tmpPart) {
list($key, $value) = explode("=", $tmpPart, 2);
$dsnParts[$key] = $value;
}
$dbName = $dsnParts['dbname'];
}
$structer = new OgerDbStructMysql($conn, $dbName);
$dbStruct = $structer->getDbStruct();
$structer->setParams(array("dry-run" => true,
"log-level" => $structer::LOG_NOTICE,
"echo-log" => true));
$strucTpl = array();
if (file_exists($dbStructFileName)) {
$strucTpl = include($dbStructFileName);
}
echo "\n***************************************************\n";
echo "* Database: " . $dbName . "\n";
echo "* Struct file: dbStructFileName\n";
echo "***************************************************\n";
if ($params['reverse']) {
echo "*** Following must be changed in the database to be in sync with the struct file:\n";
}
else {
echo "*** Following must be changed in the struct file to be in sync with the database:\n";
// ATTENTION: the normal mode for this script is the reverse mode for the struct checker !!!
$structer->startReverseMode($strucTpl);
}
$structer->forceDbStruct($strucTpl);
if ($structer->changeCount) {
echo "\n";
if ($params['apply']) {
if ($params['reverse']) { // structfile -> db
$structer2 = new OgerDbStructMysql($conn, $dbName);
$structer->setParams(array("dry-run" => false,
"log-level" => $structer::LOG_NOTICE,
"echo-log" => true));
$structer2->setParams(array());
$structer2->updateDbStruct($strucTpl);
$structer2->reorderDbStruct();
}
else { // db -> structfile
echo "Write structure file.\n";
file_put_contents($dbStructFileName, "<?PHP\n return\n" . $structer->formatDbStruct($dbStruct) . "\n;\n?>\n");
$pass = Config::$dbDefs[$dbDefAliasId]['pass'];
$pass = ($pass ? "-p$pass" : "");
$cmd = "mysqldump -u " . Config::$dbDefs[$dbDefAliasId]['user'] .
" $pass" .
" " . $dbName .
" --no-data > $dbStructDumpName";
echo "Dump database structure ($cmd).\n";
passthru($cmd);
}
}
else {
echo "Dry-run. Nothing changed.\n";
}
}
else {
echo "Nothing to do. Already up to date.\n";
}
if ($params['apply'] && !$params['reverse']) {
echo "\n\n***************************************************\n";
echo "*** Update models:\n";
$tplFile = "$appJsRoot/model/template";
foreach ($dbStruct['TABLES'] as $table) {
$tableName = $table['TABLE_META']['TABLE_NAME'];
$modelName = "$appJsName.model." . ucfirst($tableName);
$modelFile = "$appJsRoot/model/" . ucfirst($tableName) . ".js";
echo "* $modelName\n";
if (file_exists($modelFile)) {
$content = file_get_contents($modelFile);
}
else {
if (!file_exists($tplFile)) {
echo " Skip create model (missing template).\n";
continue;
}
$content = file_get_contents($tplFile);
}
$fieldList = "";
foreach ($table['COLUMNS'] as $column) {
$fieldList .= ($fieldList ? ", " : "") . "'" . $column['COLUMN_NAME'] . "'";
}
$search = "^\s*//\s*fields: *";
if (preg_match("|$search|m", $content, $matches)) {
$content = preg_replace("/###MODEL_NAME###/", $modelName, $content);
$replace = $matches[0] . (substr($matches[0], -1) == " " ? "" : " ") . "[ $fieldList ],";
$content = preg_replace("|$search.*$|m", $replace, $content);
}
else {
$content .= "\n$replace";
}
echo " Write $modelFile\n";
file_put_contents($modelFile, $content);
}
} // eo model files
echo "\n";