Commit 75ddccee authored by Carsten  Rose's avatar Carsten Rose
Browse files

Fixes #9813. During QFQ database update, skip errors like 'Error 1060 - Duplicate Column'.

parent 20626e7b
Pipeline #3101 passed with stages
in 1 minute and 58 seconds
...@@ -1372,6 +1372,7 @@ const QUERY_TYPE_SELECT = 'type: select,show,describe,explain'; ...@@ -1372,6 +1372,7 @@ const QUERY_TYPE_SELECT = 'type: select,show,describe,explain';
const QUERY_TYPE_INSERT = 'type: insert'; const QUERY_TYPE_INSERT = 'type: insert';
const QUERY_TYPE_UPDATE = 'type: update,replace,delete'; const QUERY_TYPE_UPDATE = 'type: update,replace,delete';
const QUERY_TYPE_CONTROL = 'type: set'; const QUERY_TYPE_CONTROL = 'type: set';
const QUERY_TYPE_FAILED = 'type: query failed';
//Regexp //Regexp
//const REGEXP_DATE_INT = '^\d{4}-\d{2}-\d{2}$'; //const REGEXP_DATE_INT = '^\d{4}-\d{2}-\d{2}$';
......
...@@ -181,6 +181,7 @@ class Database { ...@@ -181,6 +181,7 @@ class Database {
* @param string $specificMessage * @param string $specificMessage
* @param array $keys * @param array $keys
* @param array $stat DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS * @param array $stat DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
* @param array $skipMySqlError
* *
* @return array|int * @return array|int
* SELECT | SHOW | DESCRIBE | EXPLAIN: see $mode * SELECT | SHOW | DESCRIBE | EXPLAIN: see $mode
...@@ -190,7 +191,7 @@ class Database { ...@@ -190,7 +191,7 @@ class Database {
* @throws \DbException * @throws \DbException
* @throws \UserFormException * @throws \UserFormException
*/ */
public function sql($sql, $mode = ROW_REGULAR, array $parameterArray = array(), $specificMessage = '', array &$keys = array(), array &$stat = array()) { public function sql($sql, $mode = ROW_REGULAR, array $parameterArray = array(), $specificMessage = '', array &$keys = array(), array &$stat = array(), array $skipMySqlError = array()) {
$queryType = ''; $queryType = '';
$result = array(); $result = array();
$this->closeMysqliStmt(); $this->closeMysqliStmt();
...@@ -205,7 +206,7 @@ class Database { ...@@ -205,7 +206,7 @@ class Database {
$specificMessage .= " "; $specificMessage .= " ";
} }
$count = $this->prepareExecute($sql, $parameterArray, $queryType, $stat, $specificMessage); $count = $this->prepareExecute($sql, $parameterArray, $queryType, $stat, $specificMessage, $skipMySqlError);
if ($count === false) { if ($count === false) {
throw new \DbException($specificMessage . "No idea why this error happens - please take some time and check the problem.", ERROR_DB_GENERIC_CHECK); throw new \DbException($specificMessage . "No idea why this error happens - please take some time and check the problem.", ERROR_DB_GENERIC_CHECK);
...@@ -339,23 +340,29 @@ class Database { ...@@ -339,23 +340,29 @@ class Database {
* Returns the number of selected rows (SELECT, SHOW, ..) or the affected rows (UPDATE, INSERT). $stat contains * Returns the number of selected rows (SELECT, SHOW, ..) or the affected rows (UPDATE, INSERT). $stat contains
* appropriate num_rows, insert_id or rows_affected. * appropriate num_rows, insert_id or rows_affected.
* *
* In case of an error, throw an exception.
* mysqli error code listed in $skipErrno[] do not throw an error.
*
* @param string $sql SQL statement with prepared statement variable. * @param string $sql SQL statement with prepared statement variable.
* @param array $parameterArray parameter array for prepared statement execution. * @param array $parameterArray parameter array for prepared statement execution.
* @param string $queryType returns QUERY_TYPE_SELECT | QUERY_TYPE_UPDATE | QUERY_TYPE_INSERT, depending on * @param string $queryType returns QUERY_TYPE_SELECT | QUERY_TYPE_UPDATE | QUERY_TYPE_INSERT, depending on
* the query. * the query.
* @param array $stat DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS * @param array $stat DB_NUM_ROWS | DB_INSERT_ID | DB_AFFECTED_ROWS
*
* @param string $specificMessage * @param string $specificMessage
* @param array $skipErrno
*
* @return int|mixed * @return int|mixed
* @throws \CodeException * @throws \CodeException
* @throws \DbException * @throws \DbException
* @throws \UserFormException * @throws \UserFormException
*/ */
private function prepareExecute($sql, array $parameterArray, &$queryType, array &$stat, $specificMessage = '') { private function prepareExecute($sql, array $parameterArray, &$queryType, array &$stat, $specificMessage = '', array $skipErrno = array()) {
// Only log a modify type statement here if sqlLogMode is (at least) modifyAll // Only log a modify type statement here if sqlLogMode is (at least) modifyAll
// If sqlLogMode is modify, log the statement after it has been executed and we know if there are affected rows. // If sqlLogMode is modify, log the statement after it has been executed and we know if there are affected rows.
$sqlLogMode = $this->isSqlModify($sql) ? SQL_LOG_MODE_MODIFY_ALL : SQL_LOG_MODE_ALL; $sqlLogMode = $this->isSqlModify($sql) ? SQL_LOG_MODE_MODIFY_ALL : SQL_LOG_MODE_ALL;
$errno = 0;
$result = 0; $result = 0;
$stat = array(); $stat = array();
$errorMsg[ERROR_MESSAGE_TO_USER] = empty($specificMessage) ? 'SQL error' : $specificMessage; $errorMsg[ERROR_MESSAGE_TO_USER] = empty($specificMessage) ? 'SQL error' : $specificMessage;
...@@ -372,34 +379,52 @@ class Database { ...@@ -372,34 +379,52 @@ class Database {
$this->dbLog($sqlLogMode, $sql, $parameterArray); $this->dbLog($sqlLogMode, $sql, $parameterArray);
if (false === ($this->mysqli_stmt = $this->mysqli->prepare($sql))) { if (false === ($this->mysqli_stmt = $this->mysqli->prepare($sql))) {
$this->dbLog(SQL_LOG_MODE_ERROR, $sql, $parameterArray);
$errorMsg[ERROR_MESSAGE_TO_DEVELOPER] = $this->getSqlHint($sql, $this->mysqli->error);
$errorMsg[ERROR_MESSAGE_OS] = '[ mysqli: ' . $this->mysqli->errno . ' ] ' . $this->mysqli->error;
throw new \DbException(json_encode($errorMsg), ERROR_DB_PREPARE); if ($skipErrno === array() && false === array_search($this->mysqli->errno, $skipErrno)) {
$this->dbLog(SQL_LOG_MODE_ERROR, $sql, $parameterArray);
$errorMsg[ERROR_MESSAGE_TO_DEVELOPER] = $this->getSqlHint($sql, $this->mysqli->error);
$errorMsg[ERROR_MESSAGE_OS] = '[ mysqli: ' . $this->mysqli->errno . ' ] ' . $this->mysqli->error;
throw new \DbException(json_encode($errorMsg), ERROR_DB_PREPARE);
} else {
$errno = $this->mysqli->errno;
}
} }
if (count($parameterArray) > 0) { if (count($parameterArray) > 0) {
if (false === $this->prepareBindParam($parameterArray)) { if (false === $this->prepareBindParam($parameterArray)) {
$this->dbLog(SQL_LOG_MODE_ERROR, $sql, $parameterArray); if ($skipErrno !== array() && false === array_search($this->mysqli->errno, $skipErrno)) {
$errorMsg[ERROR_MESSAGE_TO_DEVELOPER] = $this->getSqlHint($sql, $this->mysqli->error); $this->dbLog(SQL_LOG_MODE_ERROR, $sql, $parameterArray);
$errorMsg[ERROR_MESSAGE_OS] = '[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error; $errorMsg[ERROR_MESSAGE_TO_DEVELOPER] = $this->getSqlHint($sql, $this->mysqli->error);
$errorMsg[ERROR_MESSAGE_OS] = '[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error;
throw new \DbException(json_encode($errorMsg), ERROR_DB_BIND); throw new \DbException(json_encode($errorMsg), ERROR_DB_BIND);
} else {
$errno = $this->mysqli->errno;
}
} }
} }
if (false === $this->mysqli_stmt->execute()) { if (false === $this->mysqli_stmt->execute()) {
$this->dbLog(SQL_LOG_MODE_ERROR, $sql, $parameterArray); if ($skipErrno !== array() && false === array_search($this->mysqli->errno, $skipErrno)) {
$errorMsg[ERROR_MESSAGE_TO_DEVELOPER] = $this->getSqlHint($sql, $this->mysqli->error); $this->dbLog(SQL_LOG_MODE_ERROR, $sql, $parameterArray);
$errorMsg[ERROR_MESSAGE_OS] = '[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error; $errorMsg[ERROR_MESSAGE_TO_DEVELOPER] = $this->getSqlHint($sql, $this->mysqli->error);
$errorMsg[ERROR_MESSAGE_OS] = '[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error;
throw new \DbException(json_encode($errorMsg), ERROR_DB_EXECUTE); throw new \DbException(json_encode($errorMsg), ERROR_DB_EXECUTE);
} else {
$errno = $this->mysqli->errno;
}
} }
$msg = ''; $msg = '';
$count = 0; $count = 0;
$command = strtoupper(explode(' ', $sql, 2)[0]); if ($errno === 0) {
$command = strtoupper(explode(' ', $sql, 2)[0]);
} else {
$command = 'FAILED';
}
switch ($command) { switch ($command) {
case 'SELECT': case 'SELECT':
case 'SHOW': case 'SHOW':
...@@ -444,6 +469,12 @@ class Database { ...@@ -444,6 +469,12 @@ class Database {
$count = $stat[DB_AFFECTED_ROWS]; $count = $stat[DB_AFFECTED_ROWS];
$msg = ''; $msg = '';
break; break;
case 'FAILED':
$queryType = QUERY_TYPE_FAILED;
$stat[DB_AFFECTED_ROWS] = 0;
$count = -1;
$msg = '[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error;
break;
default: default:
// Unknown command: treat it as a control command // Unknown command: treat it as a control command
......
...@@ -373,6 +373,9 @@ class DatabaseUpdate { ...@@ -373,6 +373,9 @@ class DatabaseUpdate {
* @throws \UserFormException * @throws \UserFormException
*/ */
private function dbUpdateStatements($old, $new) { private function dbUpdateStatements($old, $new) {
$dummy = array();
if ($new == '' || $old === false || $old === null) { if ($new == '' || $old === false || $old === null) {
return; return;
} }
...@@ -391,7 +394,9 @@ class DatabaseUpdate { ...@@ -391,7 +394,9 @@ class DatabaseUpdate {
if ($apply) { if ($apply) {
// Play Statements // Play Statements
foreach ($sqlStatements as $sql) { foreach ($sqlStatements as $sql) {
$this->db->sql($sql, ROW_REGULAR, array(), "Apply updates to QFQ database. Installed version: $old. New QFQ version: $new"); $this->db->sql($sql, ROW_REGULAR, array(),
"Apply updates to QFQ database. Installed version: $old. New QFQ version: $new",
$dummy, $dummy, [1060] /* duplicate column name */);
} }
// Remember already applied updates - in case something breaks and the update has to be repeated. // Remember already applied updates - in case something breaks and the update has to be repeated.
$this->setDatabaseVersion($new); $this->setDatabaseVersion($new);
......
...@@ -63,6 +63,7 @@ abstract class AbstractDatabaseTest extends TestCase { ...@@ -63,6 +63,7 @@ abstract class AbstractDatabaseTest extends TestCase {
* @throws \DbException * @throws \DbException
* @throws \UserFormException * @throws \UserFormException
* @throws \UserReportException * @throws \UserReportException
* @throws \Exception
*/ */
protected function setUp() { protected function setUp() {
......
...@@ -115,6 +115,7 @@ class DatabaseFunction extends AbstractDatabaseTest { ...@@ -115,6 +115,7 @@ class DatabaseFunction extends AbstractDatabaseTest {
* @throws \DbException * @throws \DbException
* @throws \UserFormException * @throws \UserFormException
* @throws \UserReportException * @throws \UserReportException
* @throws \Exception
*/ */
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
......
...@@ -15,6 +15,9 @@ require_once(__DIR__ . '/AbstractDatabaseTest.php'); ...@@ -15,6 +15,9 @@ require_once(__DIR__ . '/AbstractDatabaseTest.php');
class DatabaseTest extends AbstractDatabaseTest { class DatabaseTest extends AbstractDatabaseTest {
/** /**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testFetchAll() { public function testFetchAll() {
$allRows = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id LIMIT 2'); $allRows = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person ORDER BY id LIMIT 2');
...@@ -25,7 +28,9 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -25,7 +28,9 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* * @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testFetchAllEmpty() { public function testFetchAllEmpty() {
$allRows = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person WHERE id=0 ORDER BY id'); $allRows = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT * FROM Person WHERE id=0 ORDER BY id');
...@@ -35,6 +40,7 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -35,6 +40,7 @@ class DatabaseTest extends AbstractDatabaseTest {
/** /**
* @throws \CodeException * @throws \CodeException
* @throws \DbException
* @throws \UserFormException * @throws \UserFormException
*/ */
public function testQuerySimple() { public function testQuerySimple() {
...@@ -142,6 +148,7 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -142,6 +148,7 @@ class DatabaseTest extends AbstractDatabaseTest {
/** /**
* @throws \CodeException * @throws \CodeException
* @throws \DbException
* @throws \UserFormException * @throws \UserFormException
*/ */
public function testQuerySimpleParameter() { public function testQuerySimpleParameter() {
...@@ -212,7 +219,24 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -212,7 +219,24 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* Check to skip mysqli errno 1060
* *
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
public function testSkipErrno() {
$dummy = array();
$result = $this->dbArray[DB_INDEX_DEFAULT]->sql("ALTER TABLE `Person` ADD `name` CHAR(16) NOT NULL AFTER `id`",
ROW_REGULAR, array(), '', $dummy, $dummy, [1060]);
$this->assertEquals('-1', $result);
}
/**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testGetSetValueList() { public function testGetSetValueList() {
$valueList = $this->dbArray[DB_INDEX_DEFAULT]->getEnumSetValueList('Person', 'gender'); $valueList = $this->dbArray[DB_INDEX_DEFAULT]->getEnumSetValueList('Person', 'gender');
...@@ -225,6 +249,9 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -225,6 +249,9 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testSelectReturn() { public function testSelectReturn() {
$dummy = array(); $dummy = array();
...@@ -240,6 +267,9 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -240,6 +267,9 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testShowReturn() { public function testShowReturn() {
$dummy = array(); $dummy = array();
...@@ -259,6 +289,9 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -259,6 +289,9 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testExplainReturn() { public function testExplainReturn() {
$dummy = array(); $dummy = array();
...@@ -273,6 +306,9 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -273,6 +306,9 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testInsertReturn() { public function testInsertReturn() {
$dummy = array(); $dummy = array();
...@@ -289,6 +325,9 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -289,6 +325,9 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testUpdateReturn() { public function testUpdateReturn() {
$dummy = array(); $dummy = array();
...@@ -305,6 +344,7 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -305,6 +344,7 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* @throws \Exception
*/ */
public function testDeleteReturn() { public function testDeleteReturn() {
$dummy = array(); $dummy = array();
...@@ -325,6 +365,7 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -325,6 +365,7 @@ class DatabaseTest extends AbstractDatabaseTest {
/** /**
* @throws \Exception
*/ */
public function testReplaceReturn() { public function testReplaceReturn() {
$dummy = array(); $dummy = array();
...@@ -371,7 +412,9 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -371,7 +412,9 @@ class DatabaseTest extends AbstractDatabaseTest {
} }
/** /**
* * @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/ */
public function testSqlKeys() { public function testSqlKeys() {
$keys = array(); $keys = array();
...@@ -461,6 +504,7 @@ class DatabaseTest extends AbstractDatabaseTest { ...@@ -461,6 +504,7 @@ class DatabaseTest extends AbstractDatabaseTest {
* @throws \DbException * @throws \DbException
* @throws \UserFormException * @throws \UserFormException
* @throws \UserReportException * @throws \UserReportException
* @throws \Exception
*/ */
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment