forked from science-ation/science-ation
6a9b470698
This gives us a much more robust database versioning system
76 lines
1.8 KiB
PHP
76 lines
1.8 KiB
PHP
<?
|
|
//include the config.inc.php
|
|
//so we have the db connection info
|
|
require("../data/config.inc.php");
|
|
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;
|
|
}
|
|
|
|
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.sql"))
|
|
{
|
|
echo "db.update.$ver.sql detected - running...\n";
|
|
readfile("db.update.$ver.sql");
|
|
echo "\n";
|
|
system("mysql -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME <db.update.$ver.sql");
|
|
}
|
|
else
|
|
{
|
|
echo "Version $ver update file not found - skipping over\n";
|
|
}
|
|
}
|
|
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";
|
|
}
|
|
|
|
|
|
?>
|