From 200f0ad20f48c3a4d7d6731b38a92d29cfb7d847 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Sun, 16 Dec 2018 21:55:04 -0800 Subject: [PATCH] Make sql.php exit status more informative Add a --status option which sets nonzero exit status for a query returning or updating no rows. This is useful for shell scripts which want to do simple existence checks (e.g. 'unless' conditions in Puppet) or need to do a single-row change. Change-Id: I3988c4fb24251dd8b33ac85aed9d0ae03eb0f910 --- maintenance/sql.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/maintenance/sql.php b/maintenance/sql.php index dba4a223a7..6a4cf0426b 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -42,6 +42,8 @@ class MwSql extends Maintenance { $this->addOption( 'query', 'Run a single query instead of running interactively', false, true ); $this->addOption( 'json', 'Output the results as JSON instead of PHP objects' ); + $this->addOption( 'status', 'Return successful exit status only if the query succeeded ' + . '(selected or altered rows), otherwise 1 for errors, 2 for no rows' ); $this->addOption( 'cluster', 'Use an external cluster by name', false, true ); $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true ); @@ -108,8 +110,11 @@ class MwSql extends Maintenance { if ( $this->hasOption( 'query' ) ) { $query = $this->getOption( 'query' ); - $this->sqlDoQuery( $db, $query, /* dieOnError */ true ); + $res = $this->sqlDoQuery( $db, $query, /* dieOnError */ true ); wfWaitForSlaves(); + if ( $this->hasOption( 'status' ) ) { + exit( $res ? 0 : 2 ); + } return; } @@ -128,6 +133,7 @@ class MwSql extends Maintenance { $newPrompt = '> '; $prompt = $newPrompt; $doDie = !Maintenance::posix_isatty( 0 ); + $res = 1; while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) { if ( !$line ) { # User simply pressed return key @@ -148,17 +154,26 @@ class MwSql extends Maintenance { readline_add_history( $wholeLine . ';' ); readline_write_history( $historyFile ); } - $this->sqlDoQuery( $db, $wholeLine, $doDie ); + $res = $this->sqlDoQuery( $db, $wholeLine, $doDie ); $prompt = $newPrompt; $wholeLine = ''; } wfWaitForSlaves(); + if ( $this->hasOption( 'status' ) ) { + exit( $res ? 0 : 2 ); + } } + /** + * @param IDatabase $db + * @param string $line The SQL text of the query + * @param bool $dieOnError + * @return int|null Number of rows selected or updated, or null if the query was unsuccessful. + */ protected function sqlDoQuery( IDatabase $db, $line, $dieOnError ) { try { $res = $db->query( $line ); - $this->sqlPrintResult( $res, $db ); + return $this->sqlPrintResult( $res, $db ); } catch ( DBQueryError $e ) { if ( $dieOnError ) { $this->fatalError( $e ); @@ -166,17 +181,19 @@ class MwSql extends Maintenance { $this->error( $e ); } } + return null; } /** * Print the results, callback for $db->sourceStream() * @param ResultWrapper|bool $res * @param IDatabase $db + * @return int|null Number of rows selected or updated, or null if the query was unsuccessful. */ public function sqlPrintResult( $res, $db ) { if ( !$res ) { // Do nothing - return; + return null; } elseif ( is_object( $res ) ) { $out = ''; $rows = []; @@ -190,6 +207,7 @@ class MwSql extends Maintenance { $out = 'Query OK, 0 row(s) affected'; } $this->output( $out . "\n" ); + return count( $rows ); } else { $affected = $db->affectedRows(); if ( $this->hasOption( 'json' ) ) { @@ -197,6 +215,7 @@ class MwSql extends Maintenance { } else { $this->output( "Query OK, $affected row(s) affected\n" ); } + return $affected; } } -- 2.20.1