S2OJ/web/app/models/Upgrader.php

141 lines
3.4 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
class Upgrader {
public static function upgraderRoot() {
2022-11-06 02:26:21 +00:00
return UOJContext::documentRoot() . '/app/upgrade';
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
public static function runSQL($filename) {
2022-11-06 02:26:21 +00:00
passthru('mysql ' . escapeshellarg(UOJConfig::$data['database']['database'])
. ' -u ' . escapeshellarg(UOJConfig::$data['database']['username'])
. ' --password=' . escapeshellarg(UOJConfig::$data['database']['password'])
. ' -h ' . escapeshellarg(UOJConfig::$data['database']['host'])
. ' -P ' . escapeshellarg(UOJConfig::$data['database']['port'])
. '<' . escapeshellarg($filename), $ret);
2016-07-18 16:39:37 +00:00
if ($ret !== 0) {
2022-11-06 02:26:21 +00:00
die("run sql failed: " . HTML::escape($filename) . "\n");
2016-07-18 16:39:37 +00:00
}
}
public static function runShell($cmd) {
passthru("$cmd", $ret);
if ($ret !== 0) {
2022-11-06 02:26:21 +00:00
die("run shell failed: " . HTML::escape($cmd) . "\n");
2016-07-18 16:39:37 +00:00
}
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
public static function transaction($fun) {
if (!DB::checkTableExists('upgrades')) {
2022-11-06 02:26:21 +00:00
echo "error: table upgrades does NOT exist!\n";
}
DB::lock(['upgrades' => DB::WLOCK], $fun);
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
public static function getStatus($name) {
2022-11-06 02:26:21 +00:00
$u = DB::selectFirst([
"select * from upgrades",
"where", ["name" => $name]
]);
2016-07-18 16:39:37 +00:00
if ($u) {
return $u['status'];
} else {
return 'down';
}
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
public static function upgrade($name, $type) {
if ($type != 'up' && $type != 'down') {
die("invalid upgrade type\n");
}
2022-11-06 02:26:21 +00:00
echo $type . ' ' . HTML::escape($name) . ': ';
$dir = self::upgraderRoot() . '/' . $name;
2016-07-18 16:39:37 +00:00
if (!is_dir($dir)) {
die("invalid upgrade name\n");
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
if (self::getStatus($name) == $type) {
echo "OK\n";
return;
}
2022-11-06 02:26:21 +00:00
if (is_file($dir . '/upgrade.php')) {
$fun = include $dir . '/upgrade.php';
2016-07-18 16:39:37 +00:00
$fun($type);
}
2022-11-06 02:26:21 +00:00
if (is_file($dir . '/' . $type . '.sql')) {
self::runSQL($dir . '/' . $type . '.sql');
}
if (is_file($dir . '/upgrade.php')) {
$fun = include $dir . '/upgrade.php';
$fun("{$type}_after_sql");
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
if (is_file($dir . '/upgrade.sh')) {
self::runShell('/bin/bash' . ' ' . escapeshellarg($dir . '/upgrade.sh') . ' ' . $type);
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
DB::insert([
"insert into upgrades",
"(name, status, updated_at)",
"values", DB::tuple([$name, $type, DB::now()]),
"on duplicate key update", ["status" => $type, "updated_at" => DB::now()]
]);
2016-07-18 16:39:37 +00:00
echo "DONE\n";
}
public static function up($name) {
self::upgrade($name, 'up');
}
public static function down($name) {
self::upgrade($name, 'down');
}
public static function refresh($name) {
self::upgrade($name, 'down');
self::upgrade($name, 'up');
}
public static function remove($name) {
self::upgrade($name, 'down');
2022-11-06 02:26:21 +00:00
DB::delete([
"delete from upgrades",
"where", ["name" => $name]
]);
2016-07-18 16:39:37 +00:00
}
public static function removeAll() {
$names = [];
foreach (DB::selectAll("select * from upgrades") as $u) {
$names[] = $u['name'];
}
natsort($names);
foreach (array_reverse($names) as $name) {
self::upgrade($name, 'down');
}
DB::delete("delete from upgrades");
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
public static function upgradeToLatest() {
$names = array_filter(scandir(self::upgraderRoot()), function ($name) {
2022-11-06 02:26:21 +00:00
return is_dir(self::upgraderRoot() . '/' . $name) && preg_match('/^\d+_[0-9a-zA-Z\._]+$/', $name);
2016-07-18 16:39:37 +00:00
});
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
natsort($names);
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
$names_table = [];
foreach ($names as $name) {
$names_table[$name] = true;
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
$dres = DB::selectAll("select * from upgrades");
foreach ($dres as $u) {
if (!isset($names_table[$u['name']])) {
2022-11-06 02:26:21 +00:00
die('missing: ' . HTML::escape($name) . "\n");
2016-07-18 16:39:37 +00:00
}
}
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
foreach ($names as $name) {
self::up($name);
}
}
}