From 825e994444fcdb73d62710d0d934e93a227bd30f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 15 Sep 2014 17:26:26 +0200 Subject: Added eventlog class, improved db-update machanism. A warning will be shown in the main menu bar if the database schema needs to be updated. --- apis/update.inc.php | 105 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 31 deletions(-) (limited to 'apis/update.inc.php') diff --git a/apis/update.inc.php b/apis/update.inc.php index 3c4acfb2..3c8984b6 100644 --- a/apis/update.inc.php +++ b/apis/update.inc.php @@ -1,6 +1,6 @@ = $targetVersion) die('Up to date :-)'); -$function = 'update_' . $currentVersion; +while ($currentVersion < $targetVersion) { -if (!function_exists($function)) - die("Don't know how to update from version $currentVersion to $targetVersion :-("); + $function = 'update_' . $currentVersion; -if (!$function()) - die("Update from $currentVersion to $targetVersion failed! :-("); + if (!function_exists($function)) + die("Don't know how to update from version $currentVersion to $targetVersion :-("); -$currentVersion++; + if (!$function()) + die("Update from $currentVersion to $targetVersion failed! :-("); -$ret = Database::exec("INSERT INTO property (name, value) VALUES ('webif-version', :version) ON DUPLICATE KEY UPDATE value = VALUES(value)", array('version' => $currentVersion), false); -if ($ret === false) - die('Writing version information back to DB failed. Next update will probably break.'); + $currentVersion++; -if ($currentVersion < $targetVersion) { - Header('Location: api.php?do=update&random=' . mt_rand()); - die("Updated to $currentVersion - press F5 to continue"); + $ret = Database::exec("INSERT INTO property (name, value) VALUES ('webif-version', :version) ON DUPLICATE KEY UPDATE value = VALUES(value)", array('version' => $currentVersion), false); + if ($ret === false) + die('Writing version information back to DB failed. Next update will probably break.'); + + if ($currentVersion < $targetVersion) { + echo("Updated to $currentVersion...\n"); + } } -die("Updated to $currentVersion"); +Message::addSuccess('db-update-done'); +Util::redirect('index.php?do=Main'); +// The update functions. Number at the end refers to current version, the function will update to the next version // ####################### // ##### 2014-05-28 // Add dateline field to property table @@ -91,29 +95,68 @@ function update_3() { $res = Database::simpleQuery("DESCRIBE setting", array(), false); if ($res !== false) { - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - switch ($row['Field']) { - case 'de': - case 'en': - case 'pt': - case 'description': - Database::exec("ALTER TABLE setting DROP {$row['Field']}"); - break; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + switch ($row['Field']) { + case 'de': + case 'en': + case 'pt': + case 'description': + Database::exec("ALTER TABLE setting DROP {$row['Field']}"); + break; + } } } - } $res = Database::simpleQuery("DESCRIBE cat_setting", array(), false); if ($res !== false) { - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - switch ($row['Field']) { - case 'de': - case 'en': - case 'pt': - case 'name': - Database::exec("ALTER TABLE cat_setting DROP {$row['Field']}"); - break; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + switch ($row['Field']) { + case 'de': + case 'en': + case 'pt': + case 'name': + Database::exec("ALTER TABLE cat_setting DROP {$row['Field']}"); + break; + } } } + return true; +} + +// ####################### +// ##### 2014-08-18 +// Remove description column from permission table, add eventlog table +function update_4() +{ + $res = Database::simpleQuery("DESCRIBE permission", array(), false); + if ($res !== false) { + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + switch ($row['Field']) { + case 'description': + Database::exec("ALTER TABLE permission DROP {$row['Field']}"); + break; + } + } + } + $res = Database::simpleQuery("show tables", array(), false); + $found = false; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if ($row['Tables_in_openslx'] !== 'eventlog') + continue; + $found = true; + break; + } + if ($found === false) { + // create table + Database::exec("CREATE TABLE `eventlog` ( + `logid` int(10) unsigned NOT NULL AUTO_INCREMENT, + `dateline` int(10) unsigned NOT NULL, + `logtypeid` varchar(30) NOT NULL, + `description` varchar(255) NOT NULL, + PRIMARY KEY (`logid`), + KEY `dateline` (`dateline`), + KEY `logtypeid` (`logtypeid`,`dateline`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + "); } return true; } -- cgit v1.2.3-55-g7522