From c9d1d7888f7a3913360b93a13bbf9ee4693beb8e Mon Sep 17 00:00:00 2001 From: Pepijn Over Date: Wed, 9 Apr 2014 21:47:42 +0200 Subject: [PATCH] changing database version-check so if no changes have been made you dont have to run install.php again --- src/bootstrap.php | 7 +++--- src/includes/functions.inc.php | 16 ++++++++++++-- src/psm/Util/Install/Installer.class.php | 27 +++++++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/bootstrap.php b/src/bootstrap.php index 49675eb9..2d338091 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -113,10 +113,9 @@ if(defined('PSM_INSTALL') && PSM_INSTALL) { die(); } // config load OK, make sure database version is up to date - $version_db = psm_get_conf('version'); - - if(version_compare(PSM_VERSION, $version_db, '>')) { - die('Your database is for an older version, please click here to update your database to the latest version.'); + $installer = new \psm\Util\Install\Installer($db); + if($installer->isUpgradeRequired()) { + die('Your database is for an older version and requires an upgrade, please click here to update your database to the latest version.'); } } diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index 7da63612..89c02fd7 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -153,7 +153,9 @@ function psm_load_conf() { } /** - * Update a config setting + * Update a config setting. + * + * If the key does not exist yet it will be created. * @global \psm\Service\Database $db * @param string $key * @param string $value @@ -161,11 +163,21 @@ function psm_load_conf() { function psm_update_conf($key, $value) { global $db; - $db->save( + $result = $db->save( PSM_DB_PREFIX.'config', array('value' => $value), array('key' => $key) ); + // save returns the # rows updated, if 0, key doenst exist yet + if($result === 0) { + $db->save( + PSM_DB_PREFIX . 'config', + array( + 'key' => $key, + 'value' => $value, + ) + ); + } $GLOBALS['sm_config'][$key] = $value; } diff --git a/src/psm/Util/Install/Installer.class.php b/src/psm/Util/Install/Installer.class.php index 4cc272cc..424d66e5 100644 --- a/src/psm/Util/Install/Installer.class.php +++ b/src/psm/Util/Install/Installer.class.php @@ -63,6 +63,30 @@ class Installer { $this->logger = $logger; } + /** + * Check if an upgrade is required for the current version. + * @return boolean + * @see upgrade() + */ + public function isUpgradeRequired() { + $version_db = psm_get_conf('version'); + + if(version_compare(PSM_VERSION, $version_db, '==')) { + // version is up to date + return false; + } + + // different DB version, check if the version requires any changes + // @todo this is currently a manual check for each version, similar to upgrade().. not a clean way + if(version_compare($version_db, '3.0.0', '<')) { + return true; + } else { + // change database version to current version so this check won't be required next time + psm_update_conf('version', PSM_VERSION); + } + return false; + } + /** * Log a message to the logger callable (if any) * @param string|array $msg @@ -229,6 +253,7 @@ class Installer { * Populate the tables and perform upgrades if necessary * @param string $version_from * @param string $version_to + * @see isUpgradeRequired() */ public function upgrade($version_from, $version_to) { if(version_compare($version_from, '2.1.0', '<')) { @@ -239,7 +264,7 @@ class Installer { // upgrade to 3.0.0 $this->upgrade300(); } - $this->db->save(PSM_DB_PREFIX . 'config', array('value' => $version_to), array('key' => 'version')); + psm_update_conf('version', $version_to); } /**