. * * ========================= * * This script performs some administration functions with the database. * It has one required parameter, it can has follow values: `status`, `init`, `upgrade`, `drop`. * `status` - Will display the database status. * `init` - Will create all the required tables in the database. * The database must contain no tables. * `upgrade` - Will upgrade the database structure if it is necessary. * `drop` - Will remove all tables from the database. * * @category Utilities * @package DmarcSrg * @author Aleksey Andreev (liuch) * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3 */ namespace Liuch\DmarcSrg; use Liuch\DmarcSrg\Database\Database; use Liuch\DmarcSrg\Database\DatabaseUpgrader; use Liuch\DmarcSrg\Settings\SettingString; use Liuch\DmarcSrg\Exception\RuntimeException; require 'init.php'; if (php_sapi_name() !== 'cli') { echo 'Forbidden'; exit(1); } $action = $argv[1] ?? ''; $res = null; try { switch ($action) { case 'status': $res = Core::instance()->database()->state(); $tcn = 0; if (isset($res['tables'])) { foreach ($res['tables'] as &$t) { if (isset($t['exists']) && $t['exists'] === true) { ++$tcn; } } unset($t); } echo 'Version: ', ($res['version'] ?? 'n/a'), PHP_EOL; echo 'Tables: ', $tcn, PHP_EOL; break; case 'init': $res = Core::instance()->database()->initDb(); break; case 'upgrade': $cur_ver = (new SettingString('version'))->value(); if ($cur_ver === '') { $cur_ver = 'n/a'; } $db = Core::instance()->database(); echo "Current version: {$cur_ver}", PHP_EOL; echo 'Required version: ', $db::REQUIRED_VERSION, PHP_EOL; if ($cur_ver !== $db::REQUIRED_VERSION) { $db->getMapper('upgrader')->go($db::REQUIRED_VERSION); $res = [ 'error_code' => 0, 'message' => 'Upgrated successfully' ]; } else { $res = [ 'error_code' => 0, 'message' => 'No upgrade required' ]; } break; case 'drop': $res = Core::instance()->database()->cleanDb(); break; default: echo "Usage: {$argv[0]} status|init|upgrade|drop", PHP_EOL; exit(1); } } catch (RuntimeException $e) { $res = ErrorHandler::exceptionResult($e); } $error = ($res['error_code'] ?? 0) ? 'Error! ' : ''; echo "Message: {$error}{$res['message']}", PHP_EOL; if (isset($res['debug_info'])) { $debug_info = $res['debug_info']; echo "Debug info:", PHP_EOL; echo '-----', PHP_EOL; echo "[{$debug_info['code']}]", PHP_EOL; echo '-----', PHP_EOL; echo "{$debug_info['content']}", PHP_EOL; } exit(0);