Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/units/helpers/deployment_helper.php
Show First 20 Lines • Show All 524 Lines • ▼ Show 20 Line(s) | |||||
$sqls = str_replace("\r\n", "\n", $sqls); // convert to linux line endings | $sqls = str_replace("\r\n", "\n", $sqls); // convert to linux line endings | ||||
$no_comment_sqls = preg_replace("/#\s([^;]*?)\n/is", "# \\1;\n", $sqls); // add ";" to each comment end to ensure correct split | $no_comment_sqls = preg_replace("/#\s([^;]*?)\n/is", "# \\1;\n", $sqls); // add ";" to each comment end to ensure correct split | ||||
$sqls = explode(";\n", $no_comment_sqls . "\n"); // ensures that last sql won't have ";" in it | $sqls = explode(";\n", $no_comment_sqls . "\n"); // ensures that last sql won't have ";" in it | ||||
$sqls = array_map('trim', $sqls); | $sqls = array_map('trim', $sqls); | ||||
try { | try { | ||||
foreach ($sqls as $sql) { | foreach ( $sqls as $line_number => $sql ) { | ||||
if ( substr($sql, 0, 1) == '#' ) { | if ( substr($sql, 0, 1) == '#' ) { | ||||
// output comment as is | // output comment as is | ||||
$this->toLog($sql); | $this->toLog($sql); | ||||
echo $this->colorText($sql, 'purple') . PHP_EOL; | echo $this->colorText($sql, 'purple') . PHP_EOL; | ||||
continue; | continue; | ||||
} | } | ||||
elseif ( $sql ) { | elseif ( $sql ) { | ||||
$this->toLog($sql . ' ... ', false); | $this->toLog($sql . ' ... ', false); | ||||
$escaped_sql = $this->isCommandLine ? $sql : kUtil::escape($sql); | $escaped_sql = $this->isCommandLine ? $sql : kUtil::escape($sql); | ||||
echo mb_substr(trim(preg_replace('/(\n|\t| )+/is', ' ', $escaped_sql)), 0, self::SQL_TRIM_LENGTH) . ' ... '; | echo mb_substr(trim(preg_replace('/(\n|\t| )+/is', ' ', $escaped_sql)), 0, self::SQL_TRIM_LENGTH) . ' ... '; | ||||
$this->Conn->Query($sql); | $this->Conn->Query($sql); | ||||
$this->toLog('OK (' . $this->Conn->getAffectedRows() . ')'); | $this->toLog('OK (' . $this->Conn->getAffectedRows() . ')'); | ||||
$this->displayStatus('OK (' . $this->Conn->getAffectedRows() . ')'); | $this->displayStatus('OK (' . $this->Conn->getAffectedRows() . ')'); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
catch (Exception $e) { | catch (Exception $e) { | ||||
// consider revisions with errors applied | // consider revisions with errors applied | ||||
$this->appliedRevisions[] = $revision; | $this->appliedRevisions[] = $revision; | ||||
$this->out('========', true); | |||||
$this->toLog('========'); | |||||
foreach ( array_slice($sqls, $line_number) as $sql ) { | |||||
if ( substr($sql, 0, 1) == '#' ) { | |||||
// Output comment as is. | |||||
$this->toLog($sql); | |||||
echo $this->colorText($sql, 'purple') . PHP_EOL; | |||||
continue; | |||||
} | |||||
elseif ( $sql ) { | |||||
$this->toLog($sql); | |||||
$escaped_sql = $this->isCommandLine ? $sql : kUtil::escape($sql); | |||||
echo $escaped_sql . ';' . PHP_EOL; | |||||
} | |||||
} | |||||
$this->out('========', true); | |||||
$this->toLog('========'); | |||||
return false; | return false; | ||||
} | } | ||||
$this->appliedRevisions[] = $revision; | $this->appliedRevisions[] = $revision; | ||||
} | } | ||||
echo PHP_EOL; | echo PHP_EOL; | ||||
return true; | return true; | ||||
} | } | ||||
/** | /** | ||||
* Error handler for sql errors. | * Error handler for sql errors. | ||||
* | * | ||||
* @param integer $code Error code. | * @param integer $code Error code. | ||||
* @param string $msg Error message. | * @param string $msg Error message. | ||||
* @param string $sql SQL query. | * @param string $sql SQL query. | ||||
* | * | ||||
* @return void | * @return void | ||||
* @throws Exception When SQL error happens. | * @throws Exception When SQL error happens. | ||||
*/ | */ | ||||
public function handleSqlError($code, $msg, $sql) | public function handleSqlError($code, $msg, $sql) | ||||
{ | { | ||||
$this->toLog('FAILED' . PHP_EOL . 'SQL Error #' . $code . ': ' . $msg); | $error_msg = 'FAILED' . PHP_EOL . 'SQL Error #' . $code . ': ' . $msg; | ||||
$this->toLog($error_msg); | |||||
$this->displayStatus('FAILED' . PHP_EOL . 'SQL Error #' . $code . ': ' . $msg); | $this->displayStatus($error_msg); | ||||
$this->out('Please execute rest of SQLs in this Revision by hand and run deployment script again.', true); | |||||
$info_msg = 'Execute rest of SQLs in this Revision (show below) by hand and '; | |||||
$info_msg .= 'RUN deployment script again to complete the deployment process.'; | |||||
$this->toLog($info_msg); | |||||
echo $this->colorText($info_msg, 'red', true) . PHP_EOL; | |||||
throw new Exception($msg, $code); | throw new Exception($msg, $code); | ||||
} | } | ||||
/** | /** | ||||
* Checks if given revision was already applied | * Checks if given revision was already applied | ||||
* | * | ||||
* @param int $revision | * @param int $revision | ||||
▲ Show 20 Lines • Show All 131 Lines • Show Last 20 Lines |