Revize 1353
Přidáno uživatelem Ondřej Fibich před více než 12 roky(ů)
freenetis/branches/testing/application/models/config.php | ||
---|---|---|
", $name, $value);
|
||
}
|
||
|
||
/**
|
||
* Sets revision of system
|
||
*
|
||
* @param integer $revision
|
||
*/
|
||
public function set_db_schema_version($revision = NULL)
|
||
{
|
||
if ($revision)
|
||
{
|
||
$this->db->query("
|
||
update config
|
||
set value = ?
|
||
where name = 'db_schema_version'
|
||
", $revision);
|
||
}
|
||
}
|
||
|
||
}
|
freenetis/branches/testing/application/libraries/MY_Controller.php | ||
---|---|---|
}
|
||
|
||
/**
|
||
* Upgrade database from current version to latest version.
|
||
* Upgrade database from the given (current) version to the latest version.
|
||
*
|
||
* If any error occure during an upgrade. A location of the error is stored
|
||
* in the config DB table in a form of a midpoint. The upgrade starts from
|
||
* the stored midpoind in the next attempt to do the upgrade.
|
||
* Midpoint for before function is a number lower than zero and for after
|
||
* function is it a number greater than a count of SQL commands in the upgrade.
|
||
*
|
||
* @param integer $from_version From which version(revision) upgrade starts
|
||
*/
|
||
public function upgrade_sql($from_version = 0)
|
||
{
|
||
// database connection
|
||
$db = Database::instance();
|
||
// config model for set DB schema version
|
||
$config = new Config_Model();
|
||
// midpoint
|
||
$midpoint = Settings::get('upgrade_midpoint_error', FALSE);
|
||
|
||
// for each revision
|
||
for ($i = ($from_version + 1); $i < $this->current_svn_db_schema_version; $i++)
|
||
... | ... | |
// database transaction
|
||
try
|
||
{
|
||
$fnc_upgrade_before = 'upgrade_sql_' . $i . '_before';
|
||
$fnc_upgrade_after = 'upgrade_sql_' . $i . '_after';
|
||
|
||
// upgrade function before
|
||
if (function_exists('upgrade_sql_' . $i . '_before'))
|
||
if (function_exists($fnc_upgrade_before) && (
|
||
!is_numeric($midpoint) || $midpoint < 0
|
||
))
|
||
{
|
||
if (!call_user_func('upgrade_sql_' . $i . '_before'))
|
||
try
|
||
{
|
||
throw new Exception('upgrade_sql_' . $i . '_before');
|
||
if (!call_user_func($fnc_upgrade_before))
|
||
throw new Exception($fnc_upgrade_before);
|
||
$midpoint = NULL;
|
||
}
|
||
catch (Exception $ex)
|
||
{
|
||
Settings::set('upgrade_midpoint_error', -1);
|
||
throw $ex;
|
||
}
|
||
}
|
||
|
||
// upgrade SQL
|
||
foreach ($upgrade_sql[$i] as $query)
|
||
$qindex = is_numeric($midpoint) ? intval($midpoint) : 0;
|
||
$upgrade_command_count = count($upgrade_sql[$i]);
|
||
|
||
for (; $qindex < $upgrade_command_count; $qindex++)
|
||
{
|
||
if (!$db->query($query))
|
||
$query = $upgrade_sql[$i][$qindex];
|
||
|
||
try
|
||
{
|
||
throw new Kohana_Database_Exception();
|
||
$db->query($query);
|
||
}
|
||
catch (Exception $ex)
|
||
{
|
||
Settings::set('upgrade_midpoint_error', $qindex);
|
||
throw $ex;
|
||
}
|
||
}
|
||
|
||
// upgrade function after
|
||
if (function_exists('upgrade_sql_' . $i . '_after'))
|
||
if (function_exists($fnc_upgrade_after))
|
||
{
|
||
if (!call_user_func('upgrade_sql_' . $i . '_after'))
|
||
try
|
||
{
|
||
throw new Exception('upgrade_sql_' . $i . '_after');
|
||
if (!call_user_func($fnc_upgrade_after))
|
||
throw new Exception($fnc_upgrade_after);
|
||
$midpoint = NULL;
|
||
}
|
||
catch (Exception $ex)
|
||
{
|
||
Settings::set('upgrade_midpoint_error', $upgrade_command_count + 1000);
|
||
throw $ex;
|
||
}
|
||
}
|
||
|
||
// set up db schema
|
||
Settings::set('db_schema_version', $i);
|
||
|
||
// clean midpoint if not cleaned
|
||
if (Settings::get('upgrade_midpoint_error') !== '')
|
||
Settings::set('upgrade_midpoint_error', '');
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
... | ... | |
|
||
self::error(UPGRADE, $message);
|
||
}
|
||
// set up db schema
|
||
$config->set_db_schema_version($i);
|
||
}
|
||
}
|
||
|
||
... | ... | |
try
|
||
{
|
||
// upgrade function before
|
||
if (function_exists('upgrade_sql_before'))
|
||
if (function_exists('upgrade_sql_before') && (
|
||
!is_numeric($midpoint) || $midpoint < 0
|
||
))
|
||
{
|
||
if (!call_user_func('upgrade_sql_before'))
|
||
try
|
||
{
|
||
throw new Exception('upgrade_sql_before');
|
||
if (!call_user_func('upgrade_sql_before'))
|
||
throw new Exception('upgrade_sql_before');
|
||
$midpoint = NULL;
|
||
}
|
||
catch (Exception $ex)
|
||
{
|
||
Settings::set('upgrade_midpoint_error', -1);
|
||
throw $ex;
|
||
}
|
||
}
|
||
|
||
// upgrade SQL
|
||
$i = $this->current_svn_db_schema_version;
|
||
$qindex = is_numeric($midpoint) ? intval($midpoint) : 0;
|
||
$upgrade_command_count = count($this->upgrade_sql[$i]);
|
||
|
||
// upgrade SQL
|
||
foreach ($this->upgrade_sql[$this->current_svn_db_schema_version] as $query)
|
||
for (; $qindex < $upgrade_command_count; $qindex++)
|
||
{
|
||
if (!$db->query($query))
|
||
$query = $this->upgrade_sql[$i][$qindex];
|
||
|
||
try
|
||
{
|
||
throw new Kohana_Database_Exception();
|
||
$db->query($query);
|
||
}
|
||
catch (Exception $ex)
|
||
{
|
||
Settings::set('upgrade_midpoint_error', $qindex);
|
||
throw $ex;
|
||
}
|
||
}
|
||
|
||
// upgrade function after
|
||
if (function_exists('upgrade_sql_after'))
|
||
{
|
||
if (!call_user_func('upgrade_sql_after'))
|
||
try
|
||
{
|
||
throw new Exception('upgrade_sql_after');
|
||
if (!call_user_func('upgrade_sql_after'))
|
||
throw new Exception('upgrade_sql_after');
|
||
}
|
||
catch (Exception $ex)
|
||
{
|
||
Settings::set('upgrade_midpoint_error', $upgrade_command_count + 1000);
|
||
throw $ex;
|
||
}
|
||
}
|
||
|
||
// set up db schema
|
||
Settings::set('db_schema_version', $i);
|
||
|
||
// clean midpoint// clean midpoint if not cleaned
|
||
if (Settings::get('upgrade_midpoint_error') !== '')
|
||
Settings::set('upgrade_midpoint_error', '');
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$message = 'SVN: ' . $this->current_svn_db_schema_version . ' <br />'
|
||
. __('file') . ': upgrade_sql.php<br /><br />'
|
||
$message = 'SVN: ' . $i . ' <br />' . __('file')
|
||
. ': upgrade_sql.php<br /><br />'
|
||
. 'Function: ' . $e->getMessage()
|
||
. '<br />Last SQL command: ' . @$query;
|
||
|
||
$this->error(UPGRADE, $message);
|
||
}
|
||
|
||
// set up db schema
|
||
$config->set_db_schema_version($this->current_svn_db_schema_version);
|
||
}
|
||
|
||
/**
|
Také k dispozici: Unified diff
Pridany milniky pri upgradu DB, viz.: