commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
error_reporting(E_ALL & ~E_NOTICE); |
|
3 |
defined('IN_IA') or exit('Access Denied'); |
|
4 |
require __DIR__ . '/vendor/jdorn/sql-formatter/lib/SqlFormatter.php'; |
|
5 |
ini_set('max_execution_time', 1800); |
|
6 |
|
|
7 |
/*** |
|
8 |
* 记录日志 |
|
9 |
* @param $content |
|
10 |
* @return bool |
|
11 |
*/ |
|
12 |
function upgrade_log($content) |
|
13 |
{ |
|
14 |
$logFile = __DIR__ . '/upgrade.log'; |
|
15 |
$handle = fopen($logFile, 'a'); |
|
16 |
if (!$handle) { |
|
17 |
return false; |
|
18 |
} |
|
19 |
$nowDateTime = date('Y-m-d H:i:s'); |
|
20 |
$string = <<<EOF |
|
21 |
{$nowDateTime}: |
|
22 |
---------> |
|
23 |
{$content} |
|
24 |
<--------- |
|
25 |
|
|
26 |
|
|
27 |
EOF; |
|
28 |
$result = fwrite($handle, $string); |
|
29 |
fclose($handle); |
|
30 |
return $result ? true : false; |
|
31 |
} |
|
32 |
|
|
33 |
/** |
|
34 |
* 执行SQL |
|
35 |
* @param string $sql 要运行的SQL |
|
36 |
* @param bool $split 自动拆分SQL |
|
37 |
* @param bool $continueOnError 遇到错误继续执行 |
|
38 |
* @throws Exception |
|
39 |
*/ |
|
40 |
function sql_execute($sql, $split = true, $continueOnError = true) |
|
41 |
{ |
|
42 |
if ($split) { |
|
43 |
$list = SqlFormatter::splitQuery($sql); |
|
44 |
} else { |
|
45 |
$list = [$sql]; |
|
46 |
} |
|
47 |
foreach ($list as $item) { |
|
48 |
try { |
|
49 |
set_error_handler(function ($errno, $errstr) { |
|
50 |
upgrade_log($errstr); |
|
51 |
}); |
|
52 |
pdo_run($item); |
|
53 |
restore_error_handler(); |
|
54 |
} catch (Exception $exception) { |
|
55 |
upgrade_log($exception->getMessage()); |
|
56 |
if (!$continueOnError) { |
|
57 |
throw $exception; |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* 检查数据表是否存在 |
|
65 |
* @param $tableName |
|
66 |
* @return bool |
|
67 |
*/ |
|
68 |
function sql_table_exists($tableName) |
|
69 |
{ |
|
70 |
$sql = "SHOW TABLES LIKE '{$tableName}';"; |
|
71 |
$result = pdo_fetch($sql); |
|
72 |
if (is_array($result) && count($result)) { |
|
73 |
return true; |
|
74 |
} else { |
|
75 |
return false; |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
$dbConfigFile = __DIR__ . '/config/db.php'; |
|
80 |
if (!file_exists($dbConfigFile)) { |
|
81 |
file_put_contents($dbConfigFile, <<<EOF |
|
82 |
<?php |
|
83 |
function getWe7DbConfig() |
|
84 |
{ |
|
85 |
require __DIR__ . '/../../../data/config.php'; |
|
86 |
if (!empty(\$config['db']['master'])) { |
|
87 |
\$we7DbConfig = \$config['db']['master']; |
|
88 |
} else { |
|
89 |
\$we7DbConfig = \$config['db']; |
|
90 |
} |
|
91 |
return [ |
|
92 |
'host' => \$we7DbConfig['host'], |
|
93 |
'port' => \$we7DbConfig['port'], |
|
94 |
'dbname' => \$we7DbConfig['database'], |
|
95 |
'username' => \$we7DbConfig['username'], |
|
96 |
'password' => \$we7DbConfig['password'], |
|
97 |
]; |
|
98 |
} |
|
99 |
|
|
100 |
\$we7DbConfig = getWe7DbConfig(); |
|
101 |
return [ |
|
102 |
'host' => \$we7DbConfig['host'], |
|
103 |
'port' => \$we7DbConfig['port'], |
|
104 |
'dbname' => \$we7DbConfig['dbname'], |
|
105 |
'username' => \$we7DbConfig['username'], |
|
106 |
'password' => \$we7DbConfig['password'], |
|
107 |
'tablePrefix' => 'zjhj_bd_', |
|
108 |
]; |
|
109 |
|
|
110 |
EOF |
|
111 |
); |
|
112 |
} |
|
113 |
|
|
114 |
$optionTableName = 'zjhj_bd_option'; |
|
115 |
|
|
116 |
try { |
|
117 |
if (sql_table_exists($optionTableName)) { |
|
118 |
$result = pdo_fetch("SELECT * FROM `{$optionTableName}` WHERE `name`='version' ORDER BY `id` DESC LIMIT 1;"); |
|
119 |
if (is_array($result) && count($result)) { |
|
120 |
$currentVersion = isset($result['value']) ? json_decode($result['value']) : null; |
|
121 |
} else { |
|
122 |
$currentVersion = null; |
|
123 |
} |
|
124 |
} else { |
|
125 |
$currentVersion = null; |
|
126 |
} |
|
127 |
} catch (Exception $exception) { |
|
128 |
$currentVersion = null; |
|
129 |
} |
|
130 |
$isNewInstall = $currentVersion ? false : true; |
|
131 |
$currentVersion = $currentVersion ? $currentVersion : '0.0.0'; |
|
132 |
$lastVersion = $currentVersion; |
|
133 |
|
|
134 |
// 运行各个版本的升级脚本 |
|
135 |
$versions = require __DIR__ . '/versions.php'; |
|
136 |
foreach ($versions as $v => $f) { |
|
137 |
$lastVersion = $v; |
|
138 |
if (version_compare($v, $currentVersion) > 0) { |
|
139 |
if ($f instanceof \Closure) { |
|
140 |
$f(); |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
if (sql_table_exists($optionTableName)) { // 结束,更新数据库标记的版本号 |
|
146 |
$nowDateTime = date('Y-m-d H:i:s'); |
|
147 |
if ($isNewInstall) { |
|
148 |
$updateVersionSql = <<<EOF |
|
149 |
INSERT INTO `{$optionTableName}` (`mall_id`, `mch_id`, `group`, `name`, `value`, `created_at`, `updated_at`) VALUES |
|
150 |
(0, 0, '', 'version', '\"{$lastVersion}\"', '{$nowDateTime}', '{$nowDateTime}'); |
|
151 |
EOF; |
|
152 |
} else { |
|
153 |
$updateVersionSql = <<<EOF |
|
154 |
UPDATE `{$optionTableName}` SET |
|
155 |
`value` = '\"{$lastVersion}\"', |
|
156 |
`updated_at` = '{$nowDateTime}' |
|
157 |
WHERE `name` = 'version'; |
|
158 |
EOF; |
|
159 |
} |
|
160 |
sql_execute($updateVersionSql); |
|
161 |
} |