Skip to content
Permalink
Browse files

added database versioning

  • Loading branch information...
following
following committed Apr 22, 2013
1 parent da12a3e commit a6547619e3063e2aeb2ab84b9b67d7f82e09cbb5
Showing with 122 additions and 8 deletions.
  1. +3 −2 bin/clear-webcache.php
  2. +107 −0 bin/dbsv-update.php
  3. +8 −3 bin/dbupdate.php
  4. +4 −3 htdocs/doc/sql/db-changes.txt
@@ -14,8 +14,9 @@
* TODO: quick&dirty to test functionality
*/
$opt['rootpath'] = '../htdocs/';
require($opt['rootpath'] . 'lib2/web.inc.php');
if (!isset($opt['rootpath']))
$opt['rootpath'] = '../htdocs/';
require_once($opt['rootpath'] . 'lib2/cli.inc.php');
// stop apache
system($opt['httpd']['stop']);
@@ -0,0 +1,107 @@
<?php
/***************************************************************************
* For license information see doc/license.txt
*
* Unicode Reminder メモ
***************************************************************************/
/*
* Database Structure Versioning
*/
if (!isset($opt['rootpath']))
$opt['rootpath'] = '../htdocs/';
require_once($opt['rootpath'] . 'lib2/cli.inc.php');
if (!field_exists('cache_attrib','gc_id'))
die("\n
ERROR: Database structure too old. You must first do a manual update to commit 467aae4
(March 27, 2013) to enable automatic updates. See htdocs/doc/sql/db-changes.txt.\n");
$db_version = max(99, sql_value("SELECT `value` FROM `sysconfig` WHERE `name`='db_version'",99));
do
{
++$db_version;
$dbv_function = 'dbv_'.$db_version;
if (function_exists($dbv_function))
{
echo "applying DB mutation #".$db_version;
call_user_func($dbv_function);
sql("INSERT INTO `sysconfig` (`name`,`value`) VALUES ('db_version','&1')
ON DUPLICATE KEY UPDATE `value`='&1'",
$db_version);
echo " - ok.\n";
}
else
$db_version = -1;
} while ($db_version > 0);
// Test if a certain database field exists
function field_exists($table, $field)
{
global $opt;
return sql_value("SELECT COUNT(*)
FROM `information_schema`.`columns`
WHERE `table_schema`='&1' AND `table_name`='&2' AND `column_name`='&3'",
0, $opt['db']['placeholder']['db'], $table, $field) > 0;
}
// get type of a database field
function field_type($table, $field)
{
global $opt;
return strtoupper(
sql_value("SELECT `data_type`
FROM `information_schema`.`columns`
WHERE `table_schema`='&1' AND `table_name`='&2' AND `column_name`='&3'",
'', $opt['db']['placeholder']['db'], $table, $field) );
}
// Database mutations
// - must be consecutively numbered
// - should behave well if run multiple times
function dbv_100() // expands log date to datetime, to enable time logging
{
if (field_type('cache_logs','date') != 'DATETIME')
sql("ALTER TABLE `cache_logs` CHANGE COLUMN `date` `date` DATETIME NOT NULL");
if (field_type('cache_logs_archived','date') != 'DATETIME')
sql("ALTER TABLE `cache_logs_archived` CHANGE COLUMN `date` `date` DATETIME NOT NULL");
}
function dbv_101() // add fields for fixing OKAPI issue #232
{
if (!field_exists('caches','meta_last_modified'))
{
// initialize with '0000-00-00 00:00:00' for existing data, that's ok
sql("ALTER TABLE `caches` ADD COLUMN `meta_last_modified` DATETIME NOT NULL COMMENT 'via Trigger (cache_logs)' AFTER `listing_last_modified`");
}
if (!field_exists('cache_logs','log_last_modified'))
{
if (field_exists('cache_logs','okapi_syncbase'))
$after = 'okapi_syncbase';
else
$after = 'last_modified';
sql("ALTER TABLE `cache_logs` ADD COLUMN `log_last_modified` DATETIME NOT NULL COMMENT 'via Trigger (stat_caches, gk_item_waypoint)' AFTER `".$after."`");
sql("UPDATE `cache_logs` SET `log_last_modified` = GREATEST(
`last_modified`,
IFNULL((SELECT MAX(`last_modified`) FROM `pictures` WHERE `pictures`.`object_type`=1 AND `pictures`.`object_id` = `cache_logs`.`id`),'0')
)");
}
if (!field_exists('cache_logs_archived','log_last_modified'))
{
if (field_exists('cache_logs_archived','okapi_syncbase'))
$after = 'okapi_syncbase';
else
$after = 'last_modified';
sql("ALTER TABLE `cache_logs_archived` ADD COLUMN `log_last_modified` DATETIME NOT NULL AFTER `".$after."`");
sql("UPDATE `cache_logs_archived` SET `log_last_modified` = `last_modified`");
}
}
?>
@@ -12,11 +12,16 @@
* Tables must be updated manually, see htdocs/doc/sql/db-changes.txt
*/
$rootpath = dirname(__FILE__) . '/../htdocs/';
require($rootpath . 'lib/settings.inc.php');
$rootpath = $opt['rootpath'] = dirname(__FILE__) . '/../htdocs/';
chdir($rootpath);
require_once('lib2/cli.inc.php');
echo "importing data.sql ...\n";
system('cat ' . $rootpath . 'doc/sql/static-data/data.sql | mysql -h' . $dbserver . ' -u' . $dbusername . ' --password=' . $dbpasswd . ' ' . $dbname);
system('cat ' . $rootpath . 'doc/sql/static-data/data.sql |' .
' mysql -h' . $opt['db']['servername'] . ' -u' . $opt['db']['username'] . ' --password=' . $opt['db']['password'] . ' ' . $opt['db']['placeholder']['db']);
echo "updating db structure ...\n";
require('dbsv-update.php');
echo "importing triggers ...\n";
chdir ($rootpath . 'doc/sql/stored-proc');
@@ -60,8 +60,9 @@ date commit ID change
2013-03-24 9644465 added table 'pw_dict'
2013-03-25 21d92fe added field cache_reports.date_created + trigger
2013-03-27 467aae4 added fields cache_attrib.gc_id, .gc_inc and .gc_name
2013-04-06 e281cfa changed cach_logs.date and cach_logs_archived.date type to datetime

2013-04-22 added fields caches.meta_last_modified, cache_logs.log_last_modified,
==== From here, all DB changes are automatically done when you run bin/dbupdate.php. ====

2013-04-06 e281cfa changed cach_logs.date and cach_logs_archived.date type to datetime
2013-04-22 c989c00 added fields caches.meta_last_modified, cache_logs.log_last_modified,
cache_logs_archived.log_last_modified, and triggers
initialize with dbmaint.php / sp_updateall_logdates

0 comments on commit a654761

Please sign in to comment.
You can’t perform that action at this time.