Skip to content

Commit

Permalink
batch/backupdb defaults to --single-transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Nov 18, 2024
1 parent 4e3c2e3 commit 644f3c5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
2 changes: 1 addition & 1 deletion batch/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static function make_args($argv) {
$arg = (new Getopt)->long(
"name:,n: !",
"config: !",
"dry-run,d Do not perform assignment; output CSV instead.",
"dry-run,d Do not perform assignment; output CSV instead",
"help,h !"
)->description("Perform HotCRP bulk assignments specified in the input CSV file.
Usage: php batch/assign.php [--dry-run] [FILE]")
Expand Down
66 changes: 43 additions & 23 deletions batch/backupdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class BackupDB_Batch {
/** @var bool
* @readonly */
public $tablespaces;
/** @var bool
* @readonly */
public $single_transaction;
/** @var int
* @readonly */
public $count;
Expand Down Expand Up @@ -143,6 +146,7 @@ function __construct(Dbl_ConnectionParams $cp, $arg, $getopt = null) {
$this->compress = isset($arg["z"]);
$this->schema = isset($arg["schema"]);
$this->skip_ephemeral = isset($arg["no-ephemeral"]);
$this->single_transaction = true;
$this->tablespaces = isset($arg["tablespaces"]);
$this->pc_only = isset($arg["pc"]);
$this->count = $arg["count"] ?? 1;
Expand All @@ -152,6 +156,14 @@ function __construct(Dbl_ConnectionParams $cp, $arg, $getopt = null) {
$this->throw_error("Bad option `{$arg}`");
}
$this->my_opts[] = $arg;
if ($arg === "--skip-lock-tables"
|| $arg === "--lock-tables"
|| $arg === "-l"
|| $arg === "--lock-all-tables"
|| $arg === "-x"
|| $arg === "--single-transaction") {
$this->single_transaction = false;
}
}
if (isset($arg["skip-comments"])) {
$this->my_opts[] = "--skip-comments";
Expand Down Expand Up @@ -402,8 +414,10 @@ function sversion_lockstate() {
}
}

/** @return string */
/** @param list<string> $args
* @return list<string> */
function mysqlcmd($cmd, $args) {
$a = [$cmd];
if (($this->connp->password ?? "") !== "") {
if ($this->_pwfile === null) {
$this->_pwtmp = tmpfile();
Expand All @@ -420,28 +434,35 @@ function mysqlcmd($cmd, $args) {
$this->throw_error("Cannot create temporary file");
}
}
$cmd .= " --defaults-extra-file=" . escapeshellarg($this->_pwfile);
$a[] = "--defaults-extra-file={$this->_pwfile}";
}
if (($this->connp->host ?? "localhost") !== "localhost"
&& $this->connp->host !== "") {
$cmd .= " -h " . escapeshellarg($this->connp->host);
$a[] = "-h";
$a[] = $this->connp->host;
}
if (($this->connp->user ?? "") !== "") {
$cmd .= " -u " . escapeshellarg($this->connp->user);
$a[] = "-u";
$a[] = $this->connp->user;
}
if (($this->connp->socket ?? "") !== "") {
$cmd .= " -S " . escapeshellarg($this->connp->socket);
$a[] = "-S";
$a[] = $this->connp->socket;
}
if ($cmd === "mysqldump" && !$this->tablespaces) {
$a[] = "--no-tablespaces";
}
if (!$this->tablespaces && $cmd === "mysqldump") {
$cmd .= " --no-tablespaces";
if ($cmd === "mysqldump" && $this->single_transaction) {
$a[] = "--single-transaction";
}
foreach ($this->my_opts as $opt) {
$cmd .= " " . escapeshellarg($opt);
$a[] = $opt;
}
if ($args !== "") {
$cmd .= " " . $args;
foreach ($args as $opt) {
$a[] = $opt;
}
return $cmd . " " . escapeshellarg($this->connp->name);
$a[] = $this->connp->name;
return $a;
}

private function update_maybe_ephemeral() {
Expand Down Expand Up @@ -634,12 +655,12 @@ private function transfer() {
$this->process_line($s, "\n");
}

/** @param string $cmd
/** @param list<string> $args
* @param array<int,list<string>> $descriptors
* @param array<int,resource> &$pipes
* @return resource */
private function my_proc_open($cmd, $descriptors, &$pipes) {
$proc = proc_open($cmd, $descriptors, $pipes, SiteLoader::$root, [
private function my_proc_open($args, $descriptors, &$pipes) {
$proc = proc_open(Subprocess::args_to_command($args), $descriptors, $pipes, SiteLoader::$root, [
"PATH" => getenv("PATH"), "LC_ALL" => "C"
]);
if (!$proc) {
Expand All @@ -650,7 +671,7 @@ private function my_proc_open($cmd, $descriptors, &$pipes) {

/** @return int */
private function run_restore() {
$cmd = $this->mysqlcmd("mysql", "");
$cmd = $this->mysqlcmd("mysql", []);
$pipes = [];
$proc = $this->my_proc_open($cmd, [0 => ["pipe", "rb"], 1 => ["file", "/dev/null", "w"]], $pipes);
$this->out = $pipes[0];
Expand Down Expand Up @@ -687,10 +708,9 @@ private function s3_put() {
}
}

/** @param string $args
* @param string $tables */
private function run_mysqldump_transfer($args, $tables) {
$cmd = $this->mysqlcmd("mysqldump", $args) . ($tables ? " {$tables}" : "");
/** @param string ...$args */
private function run_mysqldump_transfer(...$args) {
$cmd = $this->mysqlcmd("mysqldump", $args);
$pipes = [];
$proc = $this->my_proc_open($cmd, [["file", "/dev/null", "r"], ["pipe", "wb"]], $pipes);
$this->in = $pipes[1];
Expand All @@ -704,9 +724,9 @@ private function run_pc_only_transfer() {
$pc[] = -1;
}
$where = "contactId in (" . join(",", $pc) . ")";
$this->run_mysqldump_transfer("--where='{$where}'", "ContactInfo");
$this->run_mysqldump_transfer("", "Settings TopicArea");
$this->run_mysqldump_transfer("--where='{$where}'", "TopicInterest");
$this->run_mysqldump_transfer("--where={$where}", "ContactInfo");
$this->run_mysqldump_transfer("Settings", "TopicArea");
$this->run_mysqldump_transfer("--where={$where}", "TopicInterest");
}

private function open_streams() {
Expand Down Expand Up @@ -797,7 +817,7 @@ private function run_streams() {
} else if ($this->pc_only) {
$this->run_pc_only_transfer();
} else {
$this->run_mysqldump_transfer("", "");
$this->run_mysqldump_transfer();
}

if (!empty($this->_checked_tables)) {
Expand Down
1 change: 1 addition & 0 deletions batch/makedist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ lib/runsql.sh
lib/s3client.php
lib/s3result.php
lib/scoreinfo.php
lib/subprocess.php
lib/tagger.php
lib/text.php
lib/unicodehelper.php
Expand Down
33 changes: 33 additions & 0 deletions lib/subprocess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// subprocess.php -- Helper class for subprocess running
// Copyright (c) 2006-2024 Eddie Kohler; see LICENSE.

class Subprocess {
/** @param string $word
* @return string */
static function shell_quote_light($word) {
if (preg_match('/\A[-_.,:+\/a-zA-Z0-9][-_.,:=+\/a-zA-Z0-9~]*\z/', $word)) {
return $word;
}
return escapeshellarg($word);
}

/** @param list<string> $args
* @return string */
static function shell_quote_args($args) {
$s = [];
foreach ($args as $word) {
$s[] = self::shell_quote_light($word);
}
return join(" ", $s);
}

/** @param list<string> $args
* @return list<string>|string */
static function args_to_command($args) {
if (PHP_VERSION_ID < 70400) {
return self::shell_quote_args($args);
}
return $args;
}
}

0 comments on commit 644f3c5

Please sign in to comment.