2013-10-20 11:05:46 +02:00
|
|
|
<?php
|
|
|
|
require_once 'src/core.php';
|
2013-11-18 00:16:47 +01:00
|
|
|
$config = \Chibi\Registry::getConfig();
|
2013-10-20 11:05:46 +02:00
|
|
|
|
2013-12-14 11:14:36 +01:00
|
|
|
function getDbVersion()
|
|
|
|
{
|
|
|
|
$dbVersion = Model_Property::get(Model_Property::DbVersion);
|
|
|
|
if (strpos($dbVersion, '.') !== false)
|
|
|
|
{
|
|
|
|
list ($dbVersionMajor, $dbVersionMinor) = explode('.', $dbVersion);
|
|
|
|
}
|
|
|
|
elseif ($dbVersion)
|
|
|
|
{
|
|
|
|
$dbVersionMajor = $dbVersion;
|
|
|
|
$dbVersionMinor = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$dbVersionMajor = 0;
|
|
|
|
$dbVersionMinor = 0;
|
|
|
|
}
|
|
|
|
return [$dbVersionMajor, $dbVersionMinor];
|
|
|
|
}
|
2013-10-20 11:05:46 +02:00
|
|
|
|
2013-12-14 11:14:36 +01:00
|
|
|
$upgradesPath = TextHelper::absolutePath(\Chibi\Registry::getContext()->rootDir . DS . 'src' . DS . 'Upgrades' . DS . $config->main->dbDriver);
|
2013-11-23 20:52:41 +01:00
|
|
|
$upgrades = glob($upgradesPath . DS . '*.sql');
|
2013-10-20 11:05:46 +02:00
|
|
|
natcasesort($upgrades);
|
|
|
|
|
|
|
|
foreach ($upgrades as $upgradePath)
|
|
|
|
{
|
|
|
|
preg_match('/(\d+)\.sql/', $upgradePath, $matches);
|
2013-12-14 11:14:36 +01:00
|
|
|
$upgradeVersionMajor = intval($matches[1]);
|
|
|
|
|
|
|
|
list ($dbVersionMajor, $dbVersionMinor) = getDbVersion();
|
2013-10-20 11:05:46 +02:00
|
|
|
|
2013-12-14 11:14:36 +01:00
|
|
|
if (($upgradeVersionMajor > $dbVersionMajor) or ($upgradeVersionMajor == $dbVersionMajor and $dbVersionMinor !== null))
|
2013-10-20 11:05:46 +02:00
|
|
|
{
|
2013-12-14 11:14:36 +01:00
|
|
|
printf('%s: executing' . PHP_EOL, $upgradePath);
|
2013-10-20 11:05:46 +02:00
|
|
|
$upgradeSql = file_get_contents($upgradePath);
|
2013-11-10 12:23:59 +01:00
|
|
|
$upgradeSql = preg_replace('/^[ \t]+(.*);/m', '\0--', $upgradeSql);
|
2013-10-20 11:05:46 +02:00
|
|
|
$queries = preg_split('/;\s*[\r\n]+/s', $upgradeSql);
|
|
|
|
$queries = array_map('trim', $queries);
|
2013-12-14 11:14:36 +01:00
|
|
|
$queries = array_filter($queries);
|
|
|
|
$upgradeVersionMinor = 0;
|
2013-10-20 11:05:46 +02:00
|
|
|
foreach ($queries as $query)
|
|
|
|
{
|
2013-12-14 11:14:36 +01:00
|
|
|
$query = preg_replace('/\s*--(.*?)$/m', '', $query);
|
|
|
|
++ $upgradeVersionMinor;
|
|
|
|
if ($upgradeVersionMinor > $dbVersionMinor)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
R::exec($query);
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
echo $e . PHP_EOL;
|
|
|
|
echo $query . PHP_EOL;
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
Model_Property::set(Model_Property::DbVersion, $upgradeVersionMajor . '.' . $upgradeVersionMinor);
|
|
|
|
}
|
2013-10-20 11:05:46 +02:00
|
|
|
}
|
2013-12-14 11:14:36 +01:00
|
|
|
Model_Property::set(Model_Property::DbVersion, $upgradeVersionMajor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf('%s: no need to execute' . PHP_EOL, $upgradePath);
|
2013-10-20 11:05:46 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-14 11:14:36 +01:00
|
|
|
|
|
|
|
list ($dbVersionMajor, $dbVersionMinor) = getDbVersion();
|
|
|
|
printf('Database version: %d.%d' . PHP_EOL, $dbVersionMajor, $dbVersionMinor);
|