changing database version-check so if no changes have been made

you dont have to run install.php again
This commit is contained in:
Pepijn Over 2014-04-09 21:47:42 +02:00
parent 2f032a1f4c
commit c9d1d7888f
3 changed files with 43 additions and 7 deletions

View File

@ -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, <a href="install.php">please click here</a> 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, <a href="install.php">please click here</a> to update your database to the latest version.');
}
}

View File

@ -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;
}

View File

@ -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);
}
/**