Commit 7fc80c24 authored by Carsten  Rose's avatar Carsten Rose
Browse files

#3863 / DB Update Fails: Expected no record, got 2 rows: SHOW TABLE STATUS WHERE Name='Form'

Introduce new config var 'DB_UPDATE' in config.qfq.ini. Update the handling, if there exist table 'form' and 'Form'.
Manual.rst:  document the new config option DB_UPDATE.
Database.php: Typo fixed.
DatabaseUpdate.php: Iterate over all found 'Form' tables, take the newest version number. Respect config option DB_UPDATE. Fixed a problem: if in the PHP DB updates a newer DB Version is configured than the PHP source itself is, such updates are now not played anymore.
Config.php, QuickFormQuery.php, config.example.ini: handle config setting DB_UPDATE.
parent 0e75d2bd
......@@ -329,6 +329,11 @@ config.qfq.ini
+-----------------------------+-------------------------------------------------+----------------------------------------------------------------------------+
| NEW_BUTTON_GLYPH_ICON | NEW_BUTTON_GLYPH_ICON = glyphicon-plus | Default Icon for the form new button |
+-----------------------------+-------------------------------------------------+----------------------------------------------------------------------------+
| DB_UPDATE | DB_UPDATE = auto | 'auto': apply DB Updates only if there is a newer version. |
| | | 'always': apply DB Updates always, especially play formEditor.sql every |
| | | time QFQ is called - *not* recommended! |
| | | 'never': never apply DB Updates. |
+-----------------------------+-------------------------------------------------+----------------------------------------------------------------------------+
Example: *typo3conf/config.qfq.ini*
......
......@@ -86,3 +86,5 @@ WKHTMLTOPDF = /opt/wkhtmltox/bin/wkhtmltopdf
;NEW_BUTTON_TOOLTIP = new
;NEW_BUTTON_CLASS = btn btn-default navbar-btn
;NEW_BUTTON_GLYPH_ICON = glyphicon-plus
;DB_UPDATE=auto
\ No newline at end of file
......@@ -402,6 +402,11 @@ const SYSTEM_NEW_BUTTON_TOOLTIP = 'NEW_BUTTON_TOOLTIP';
const SYSTEM_NEW_BUTTON_CLASS = 'NEW_BUTTON_CLASS';
const SYSTEM_NEW_BUTTON_GLYPH_ICON = 'NEW_BUTTON_GLYPH_ICON';
const SYSTEM_DB_UPDATE = 'DB_UPDATE';
const SYSTEM_DB_UPDATE_ALWAYS = 'always';
const SYSTEM_DB_UPDATE_NEVER = 'never';
const SYSTEM_DB_UPDATE_AUTO = 'auto';
const GET_EXTRA_LENGTH_TOKEN = '_';
// Not stored in config.qfq.ini, but used in STORE_SYSTEM
......
......@@ -153,8 +153,9 @@ class QuickFormQuery {
$this->eval = new Evaluate($this->store, $this->db);
$dbUpdate = $this->store->getVar(SYSTEM_DB_UPDATE, STORE_SYSTEM);
$updateDb = new DatabaseUpdate($this->db);
$updateDb->checkNupdate();
$updateDb->checkNupdate($dbUpdate);
}
......
......@@ -182,7 +182,7 @@ class Database {
if ($count === 0)
$result = array();
else
throw new DbException($specificMessage . "Expected no record, got $count rows: $sql", ERROR_DB_TOO_MANY_ROWS);
throw new DbException($specificMessage . "Expected none record, got $count rows: $sql", ERROR_DB_TOO_MANY_ROWS);
break;
case ROW_EXPECT_1:
if ($count === 1)
......
......@@ -58,23 +58,32 @@ class DatabaseUpdate {
}
/**
* Try to read the QFQ version number from 'comment' in table 'Form'.
* In a very special situation , there might be table 'form' and 'Form' in the same Database. This should be handled
* in the way that the latest version number is the active one.
*
* @return bool|string false if there is no table 'Form' or if there is no comment set in table 'Form'.
* @throws CodeException
* @throws DbException
*/
private function getDatabaseVersion() {
$row = $this->db->sql("SHOW TABLE STATUS WHERE Name='Form'", ROW_EXPECT_0_1);
if (isset($row['Comment'])) {
$arr = explode('=', $row['Comment']);
if (count($arr) != 2 || $arr[0] != 'Version' || $arr[1] == '') {
return false;
$arr = $this->db->sql("SHOW TABLE STATUS WHERE Name='Form'", ROW_REGULAR);
$found = '';
//
foreach ($arr as $row) {
if (isset($row['Comment'])) {
$arr = explode('=', $row['Comment']);
if (count($arr) == 2 && $arr[0] == 'Version' && $arr[1] != '' AND (version_compare($arr[1], $found) == 1)) {
$found = $arr[1];
}
} else {
continue;
}
} else {
return false;
}
return $arr[1];
return $found === '' ? false : $found;
}
/**
......@@ -89,14 +98,21 @@ class DatabaseUpdate {
}
/**
*
* @param string $dbUpdate SYSTEM_DB_UPDATE_ON | SYSTEM_DB_UPDATE_OFF | SYSTEM_DB_UPDATE_AUTO
*
* @throws CodeException
*/
public function checkNupdate() {
public function checkNupdate($dbUpdate) {
if ($dbUpdate === SYSTEM_DB_UPDATE_NEVER) {
return;
}
$new = $this->getExtensionVersion();
$old = $this->getDatabaseVersion();
if ($new != $old) {
if ($dbUpdate === SYSTEM_DB_UPDATE_ALWAYS || ($dbUpdate === SYSTEM_DB_UPDATE_AUTO && $new != $old)) {
$this->dbUpdateStatements($old, $new);
$this->db->playSqlFile(__DIR__ . '/../../sql/formEditor.sql');
$this->setDatabaseVersion($new);
......@@ -137,9 +153,10 @@ class DatabaseUpdate {
$apply = false;
foreach ($updateArray as $key => $sqlStatements) {
// Search starting point to apply updates
$rc = version_compare($key, $old);
if ($rc == 1) {
// Search starting point to apply updates. Do not apply updates for $key>$new
$rc1 = version_compare($key, $old);
$rc2 = version_compare($key, $new);
if ($rc1 == 1 && $rc2 != 1) {
$apply = true;
}
......
......@@ -159,6 +159,9 @@ class Config {
Support::setIfNotSet($config, SYSTEM_ESCAPE_TYPE_DEFAULT, TOKEN_ESCAPE_SINGLE_TICK);
Support::setIfNotSet($config, SYSTEM_GFX_EXTRA_BUTTON_INFO_INLINE, '<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>');
Support::setIfNotSet($config, SYSTEM_GFX_EXTRA_BUTTON_INFO_BELOW, '<span class="glyphicon glyphicon-info-sign text-info" aria-hidden="true"></span>');
Support::setIfNotSet($config, SYSTEM_DB_UPDATE, SYSTEM_DB_UPDATE_AUTO);
return $config;
}
......
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