science-ation/db/db_update.php
dave f6cc5d7326 - Cleanup how variables are handled/rolled/etc. Convert the require_vars
function in the config editor to one that checks where ALL variables marked
  with year=-1 exist for FAIRYEAR.  If it doesn't, it creates them.  This
  function gets called in three places:
 	- On installation
	- On rollover
	- Whenever the database is updated
 We should, now, be able to just insert new variables with year=-1, and they
 will be automatically updated for the current year with the default value.
 (no more going into the variable editor to make sure the copy is done).

- Fix the superuser account creation in the install script
2007-11-25 19:53:15 +00:00

111 lines
2.8 KiB
PHP

<?
if(!function_exists("system")) {
echo "DB Update requires php's system() function to be available\n";
exit;
}
//include the config.inc.php
//so we have the db connection info
require("../data/config.inc.php");
echo "<pre>\n";
if(file_exists("db.code.version.txt"))
{
$dbcodeversion_file=file("db.code.version.txt");
$dbcodeversion=trim($dbcodeversion_file[0]);
}
else
{
echo "Couldnt load current db.code.version.txt\n";
exit;
}
mysql_connect($DBHOST,$DBUSER,$DBPASS);
mysql_select_db($DBNAME);
$q=mysql_query("SELECT val FROM config WHERE var='DBVERSION' AND year='0'");
$r=mysql_fetch_object($q);
$dbdbversion=$r->val;
if(!$dbdbversion)
{
echo "Couldnt get current db version. Is SFIAB properly installed?\n";
exit;
}
/* Get the fair year */
$q=mysql_query("SELECT val FROM config WHERE var='FAIRYEAR' AND year='0'");
$r=mysql_fetch_object($q);
$fairyear=$r->val;
require_once("../config_editor.inc.php"); // For config_update_variables()
if($dbcodeversion && $dbdbversion)
{
//lets see if they match
if($dbcodeversion == $dbdbversion)
{
echo "DB and CODE are all up-to-date. Version: $dbdbversion\n";
exit;
}
else if($dbcodeversion<$dbdbversion)
{
echo "ERROR: dbcodeversion<dbdbversion ($dbcodeversion<$dbdbversion). This should not happen!";
exit;
}
else if($dbcodeversion>$dbdbversion)
{
echo "DB update requirements detected\n";
echo "Current DB Version: $dbdbversion\n";
echo "Current CODE Version: $dbcodeversion\n";
echo "Updating database from $dbdbversion to $dbcodeversion\n";
for($ver=$dbdbversion+1;$ver<=$dbcodeversion;$ver++)
{
if(file_exists("db.update.$ver.php"))
{
include("db.update.$ver.php");
}
if(is_callable("db_update_{$ver}_pre")) {
echo "db.update.$ver.php::db_update_{$ver}_pre() exists - running...\n";
call_user_func("db_update_{$ver}_pre");
echo "db.update.$ver.php::db_update_{$ver}_pre() done.\n";
}
if(file_exists("db.update.$ver.sql"))
{
echo "db.update.$ver.sql detected - running...\n";
readfile("db.update.$ver.sql");
echo "\n";
system("mysql --default-character-set=latin1 -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME <db.update.$ver.sql");
}
else
{
echo "Version $ver SQL update file not found - skipping over\n";
}
if(is_callable("db_update_{$ver}_post")) {
echo "db.update.$ver.php::db_update_{$ver}_post() exists - running...\n";
call_user_func("db_update_{$ver}_post");
echo "db.update.$ver.php::db_update_{$ver}_post() done.\n";
}
}
if($db_update_skip_variables != true) {
echo "\nUpdating Configuration Variables...\n";
config_update_variables($fairyear);
}
echo "\nAll done - updating new DB version to $dbcodeversion\n";
mysql_query("UPDATE config SET val='$dbcodeversion' WHERE var='DBVERSION' AND year='0'");
}
}
else
{
echo "ERROR: dbcodeversion and dbdbversion are not defined\n";
}
echo "</pre>\n";
?>