patch sql -> cli

This commit is contained in:
2021-04-06 20:59:55 +02:00
parent 186083a315
commit ebdece7144
4 changed files with 102 additions and 127 deletions

157
cli.php
View File

@@ -44,76 +44,117 @@ function printHelp() {
function handleDatabase($argv) {
$action = $argv[2] ?? "";
switch ($action) {
case 'migrate':
$class = $argv[3] ?? null;
if (!$class) {
die("Usage: cli.php db migrate <class name>\n");
if ($action === "migrate") {
$class = $argv[3] ?? null;
if (!$class) {
die("Usage: cli.php db migrate <class name>\n");
}
$class = str_replace('/', '\\', $class);
$className = "\\Configuration\\$class";
$classPath = getClassPath($className);
if (!file_exists($classPath) || !is_readable($classPath)) {
die("Database script file does not exist or is not readable\n");
}
include_once $classPath;
$obj = new $className();
if (!($obj instanceof DatabaseScript)) {
die("Not a database script\n");
}
$db = connectDatabase();
$queries = $obj->createQueries($db);
foreach ($queries as $query) {
if (!$query->execute($db)) {
die($db->getLastError());
}
}
$db->close();
} else if ($action === "export" || $action === "import") {
// database config
$config = getDatabaseConfig();
$dbType = $config->getProperty("type") ?? null;
$user = $config->getLogin();
$password = $config->getPassword();
$database = $config->getProperty("database");
$host = $config->getHost();
$port = $config->getPort();
// subprocess config
$env = [];
$options = array_slice($argv, 3);
$dataOnly = in_array("--data-only", $options) || in_array("-d", $options);
$descriptorSpec = [STDIN, STDOUT, STDOUT];
$inputData = null;
// argument config
if ($action === "import") {
$file = $argv[3] ?? null;
if (!$file) {
die("Usage: cli.php db import <path>\n");
}
$class = str_replace('/', '\\', $class);
$className = "\\Configuration\\$class";
$classPath = getClassPath($className);
if (!file_exists($classPath) || !is_readable($classPath)) {
die("Database script file does not exist or is not readable\n");
if (!file_exists($file) || !is_readable($file)) {
die("File not found or not readable\n");
}
include_once $classPath;
$obj = new $className();
if (!($obj instanceof DatabaseScript)) {
die("Not a database script\n");
}
$inputData = file_get_contents($file);
}
$db = connectDatabase();
$queries = $obj->createQueries($db);
foreach ($queries as $query) {
if (!$query->execute($db)) {
die($db->getLastError());
if ($dbType === "mysql") {
$command_args = ["-u", $user, '-h', $host, '-P', $port, "--password=$password"];
if ($action === "export") {
$command_bin = "mysqldump";
if ($dataOnly) {
$command_args[] = "--skip-triggers";
$command_args[] = "--compact";
$command_args[] = "--no-create-info";
}
}
$db->close();
break;
case 'export':
$config = getDatabaseConfig();
$dbType = $config->getProperty("type") ?? null;
$user = $config->getLogin();
$password = $config->getPassword();
$database = $config->getProperty("database");
$host = $config->getHost();
$port = $config->getPort();
$env = [];
$output = $argv[3] ?? null;
$descriptorSpec = [STDIN, STDOUT, STDOUT];
if ($dbType === "mysql") {
$command = ["mysqldump", "-u", $user, '-h', $host, '-P', $port, "--password=$password"];
if ($database) {
$command[] = $database;
}
} else if ($dbType === "postgres") {
$command = ["pg_dump", "-U", $user, '-h', $host, '-p', $port];
if ($database) {
$command[] = $database;
}
$env["PGPASSWORD"] = $password;
} else if ($action === "import") {
$command_bin = "mysql";
$descriptorSpec[0] = ["pipe", "r"];
} else {
die("Unsupported database type\n");
die("Unsupported action\n");
}
} else if ($dbType === "postgres") {
$env["PGPASSWORD"] = $password;
$command_args = ["-U", $user, '-h', $host, '-p', $port];
if ($action === "export") {
$command_bin = "/usr/bin/pg_dump";
if ($dataOnly) {
$command_args[] = "--data-only";
}
} else if ($action === "import") {
$command_bin = "/usr/bin/psql";
$descriptorSpec[0] = ["pipe", "r"];
} else {
die("Unsupported action\n");
}
if ($output) {
$descriptorSpec[1] = ["file", $output, "w"];
} else {
die("Unsupported database type\n");
}
if ($database) {
$command_args[] = $database;
}
$command = array_merge([$command_bin], $command_args);
$process = proc_open($command, $descriptorSpec, $pipes, null, $env);
if (is_resource($process)) {
if ($action === "import" && $inputData && count($pipes) > 0) {
fwrite($pipes[0], $inputData);
fclose($pipes[0]);
}
$process = proc_open($command, $descriptorSpec, $pipes, null, $env);
proc_close($process);
break;
default:
die("Usage: cli.php db <import|export|migrate>\n");
}
}
}