Switch the database versioning code from a flat textfile (db/db.db.version.txt) to a record in the config table (var=DBVERSION, year=0)

This gives us a much more robust database versioning system
This commit is contained in:
james 2005-06-07 21:03:55 +00:00
parent 7509f5f7c1
commit 6a9b470698
7 changed files with 101 additions and 130 deletions

View File

@ -65,40 +65,6 @@ else
exit; exit;
} }
//first things first - make sure our DB version matches our CODE version
$dbcodeversion=@file($prependdir."db/db.code.version.txt");
$dbdbversion=@file($prependdir."db/db.db.version.txt");
if(!$dbdbversion)
{
echo "<html><head><title>SFIAB ERROR</title></head><body>";
echo "<h1>Science Fair In A Box - ERROR</h1>";
echo "SFIAB installation is not complete. Please go to <A href=\"install2.php\">Installer Step 2</a> to complete the installation process";
echo "<br>";
echo "</body></html>";
exit;
}
if($dbcodeversion[0]!=$dbdbversion[0])
{
echo "<html><head><title>SFIAB ERROR</title></head><body>";
echo "<h1>Science Fair In A Box - ERROR</h1>";
echo "SFIAB database and code are mismatched";
echo "<br>";
echo "Please run the db_update.php script in order to update";
echo "<br>";
echo "your database to the same version as the code";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<h2>Details</h2>";
echo "Current SFIAB codebase requires DB version: ".$dbcodeversion[0];
echo "<br>";
echo "Current SFIAB database is detected as version: ".$dbdbversion[0];
echo "<br>";
echo "</body></html>";
exit;
}
if(!mysql_connect($DBHOST,$DBUSER,$DBPASS)) if(!mysql_connect($DBHOST,$DBUSER,$DBPASS))
{ {
echo "<html><head><title>SFIAB ERROR</title></head><body>"; echo "<html><head><title>SFIAB ERROR</title></head><body>";
@ -137,6 +103,41 @@ else
} }
} }
$dbdbversion=$config['DBVERSION'];
$dbcodeversion=@file($prependdir."db/db.code.version.txt");
if(!$dbdbversion)
{
echo "<html><head><title>SFIAB ERROR</title></head><body>";
echo "<h1>Science Fair In A Box - ERROR</h1>";
echo "SFIAB installation is not complete. Please go to <A href=\"install2.php\">Installer Step 2</a> to complete the installation process";
echo "<br>";
echo "</body></html>";
exit;
}
if($dbcodeversion[0]!=$dbdbversion)
{
echo "<html><head><title>SFIAB ERROR</title></head><body>";
echo "<h1>Science Fair In A Box - ERROR</h1>";
echo "SFIAB database and code are mismatched";
echo "<br>";
echo "Please run the db_update.php script in order to update";
echo "<br>";
echo "your database to the same version as the code";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<h2>Details</h2>";
echo "Current SFIAB codebase requires DB version: ".$dbcodeversion[0];
echo "<br>";
echo "Current SFIAB database is detected as version: ".$dbdbversion;
echo "<br>";
echo "</body></html>";
exit;
}
//now pull the rest of the configuration //now pull the rest of the configuration
$q=mysql_query("SELECT * FROM config WHERE year='".$config['FAIRYEAR']."'"); $q=mysql_query("SELECT * FROM config WHERE year='".$config['FAIRYEAR']."'");
while($r=mysql_fetch_object($q)) while($r=mysql_fetch_object($q))

View File

@ -1,5 +1,4 @@
db.code.version.txt - contains the version number of the database that the code requires db.code.version.txt - contains the version number of the database that the code requires
db.db.version.txt - contains the actual version number of the database
db_update.php - will update the db to the newest version (db.code=db.db) db_update.php - will update the db to the newest version (db.code=db.db)
db.update.1.sql - the SQL commands required to update - to upgrade across multiple versions db.update.1.sql - the SQL commands required to update - to upgrade across multiple versions

View File

@ -781,3 +781,5 @@ INSERT INTO `provinces` (`code`, `province`) VALUES ('PE', 'Prince Edward Island
INSERT INTO `provinces` (`code`, `province`) VALUES ('QC', 'Québec'); INSERT INTO `provinces` (`code`, `province`) VALUES ('QC', 'Québec');
INSERT INTO `provinces` (`code`, `province`) VALUES ('SK', 'Saskatchewan'); INSERT INTO `provinces` (`code`, `province`) VALUES ('SK', 'Saskatchewan');
INSERT INTO `provinces` (`code`, `province`) VALUES ('YK', 'Yukon Territory'); INSERT INTO `provinces` (`code`, `province`) VALUES ('YK', 'Yukon Territory');
INSERT INTO `config` (`var`,`val`,`year`) VALUES ('DBVERSION','7','0');

View File

@ -44,4 +44,5 @@ INSERT INTO `provinces` (`code`, `province`) VALUES ('PE', 'Prince Edward Island
INSERT INTO `provinces` (`code`, `province`) VALUES ('QC', 'Québec'); INSERT INTO `provinces` (`code`, `province`) VALUES ('QC', 'Québec');
INSERT INTO `provinces` (`code`, `province`) VALUES ('SK', 'Saskatchewan'); INSERT INTO `provinces` (`code`, `province`) VALUES ('SK', 'Saskatchewan');
INSERT INTO `provinces` (`code`, `province`) VALUES ('YK', 'Yukon Territory'); INSERT INTO `provinces` (`code`, `province`) VALUES ('YK', 'Yukon Territory');
INSERT INTO `config` (`var`,`val`,`year`) VALUES ('DBVERSION','7','0');

View File

@ -1,4 +1,7 @@
<? <?
//include the config.inc.php
//so we have the db connection info
require("../data/config.inc.php");
if(file_exists("db.code.version.txt")) if(file_exists("db.code.version.txt"))
{ {
$dbcodeversion_file=file("db.code.version.txt"); $dbcodeversion_file=file("db.code.version.txt");
@ -10,15 +13,16 @@ else
exit; exit;
} }
if(file_exists("db.db.version.txt"))
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)
{ {
$dbdbversion_file=file("db.db.version.txt"); echo "Couldnt get current db version. Is SFIAB properly installed?\n";
$dbdbversion=trim($dbdbversion_file[0]); exit;
}
else
{
echo "Couldnt load current db.db.version.txt - assuming version is 1\n";
$dbdbversion=1;
} }
if($dbcodeversion && $dbdbversion) if($dbcodeversion && $dbdbversion)
@ -40,15 +44,8 @@ if($dbcodeversion && $dbdbversion)
echo "DB update requirements detected\n"; echo "DB update requirements detected\n";
echo "Current DB Version: $dbdbversion\n"; echo "Current DB Version: $dbdbversion\n";
echo "Current CODE Version: $dbcodeversion\n"; echo "Current CODE Version: $dbcodeversion\n";
//first, make sure we have write access to the db.db.version.txt
//otherwise, we will not be able to save the new version number
//which would screw things up.
if(is_writable("db.db.version.txt"))
{
echo "Updating database from $dbdbversion to $dbcodeversion\n"; echo "Updating database from $dbdbversion to $dbcodeversion\n";
//include the config.inc.php
//so we have the db connection info
require("../data/config.inc.php");
for($ver=$dbdbversion+1;$ver<=$dbcodeversion;$ver++) for($ver=$dbdbversion+1;$ver<=$dbcodeversion;$ver++)
{ {
@ -65,14 +62,7 @@ if($dbcodeversion && $dbdbversion)
} }
} }
echo "\nAll done - updating new DB version to $dbcodeversion\n"; echo "\nAll done - updating new DB version to $dbcodeversion\n";
$fp=fopen("db.db.version.txt","w"); mysql_query("UPDATE config SET val='$dbcodeversion' WHERE var='DBVERSION' AND year='0'");
fputs($fp,$dbcodeversion."\n");
fclose($fp);
}
else
{
echo "ERROR: cannot perform upgrade. db.db.version.txt is not writeable";
}
} }
} }

View File

@ -57,10 +57,11 @@ mysql_select_db($DBNAME);
echo "<b>version $dbcodeversion</b><br />"; echo "<b>version $dbcodeversion</b><br />";
echo "Checking for existing SFIAB database... "; echo "Checking for existing SFIAB database... ";
if(file_exists("db/db.db.version.txt"))
{ $q=@mysql_query("SELECT val FROM config WHERE var='DBVERSION' AND year='0'");
$dbdbversion_file=file("db/db.db.version.txt"); $r=@mysql_fetch_object($q);
$dbdbversion=trim($dbdbversion_file[0]); $dbdbversion=$r->val;
if($dbdbversion) if($dbdbversion)
{ {
echo "<b>ERROR: found version $dbdbversion</b><br />"; echo "<b>ERROR: found version $dbdbversion</b><br />";
@ -78,23 +79,6 @@ mysql_select_db($DBNAME);
{ {
echo "<b>Not found (good!)</b><br />"; echo "<b>Not found (good!)</b><br />";
} }
}
else
{
echo "<b>Not found (good!)</b><br />";
}
echo "Checking if db/db.db.version.txt is writable... ";
if(is_writable("db/db.db.version.txt"))
{
echo "<b>Yes</b><br />";
}
else
{
echo "<b>ERROR: cannot install. db/db.db.version.txt is not writeable</b><br />";
exit;
}
echo "Checking for database installer for version $dbcodeversion... "; echo "Checking for database installer for version $dbcodeversion... ";
if(file_exists("db/db.full.$dbcodeversion.sql")) if(file_exists("db/db.full.$dbcodeversion.sql"))
@ -106,9 +90,10 @@ mysql_select_db($DBNAME);
system("mysql -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME <db/db.full.$dbcodeversion.sql"); system("mysql -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME <db/db.full.$dbcodeversion.sql");
echo "<b>Done! installed database version $dbcodeversion</b><br />\n"; echo "<b>Done! installed database version $dbcodeversion</b><br />\n";
$fp=fopen("db/db.db.version.txt","w");
fputs($fp,$dbcodeversion."\n"); //now update the db version in the database
fclose($fp); mysql_query("UPDATE config SET val='$dbcodeversion' WHERE var='DBVERSION' AND year='0'");
echo "<br />"; echo "<br />";
echo "<b>Done!</b><br />"; echo "<b>Done!</b><br />";
echo "<a href=\"install3.php\">Proceed to installation step 3</a><br />"; echo "<a href=\"install3.php\">Proceed to installation step 3</a><br />";

View File

@ -43,20 +43,12 @@ mysql_connect($DBHOST,$DBUSER,$DBPASS);
mysql_select_db($DBNAME); mysql_select_db($DBNAME);
echo "Checking for SFIAB database... "; echo "Checking for SFIAB database... ";
if(file_exists("db/db.db.version.txt"))
{ $q=@mysql_query("SELECT val FROM config WHERE var='DBVERSION' AND year='0'");
$dbdbversion_file=file("db/db.db.version.txt"); $r=@mysql_fetch_object($q);
$dbdbversion=trim($dbdbversion_file[0]); $dbdbversion=$r->val;
if(!$dbdbversion) if(!$dbdbversion)
{
echo "<div class=\"error\">SFIAB Installation Step 2 is not yet complete.</div>";
echo "<a href=\"install2.php\">Go back to installation step 2</a><br />";
echo "</body></html>";
exit;
}
}
else
{ {
echo "<div class=\"error\">SFIAB Installation Step 2 is not yet complete.</div>"; echo "<div class=\"error\">SFIAB Installation Step 2 is not yet complete.</div>";
echo "<a href=\"install2.php\">Go back to installation step 2</a><br />"; echo "<a href=\"install2.php\">Go back to installation step 2</a><br />";
@ -64,9 +56,9 @@ mysql_select_db($DBNAME);
exit; exit;
} }
//if there are entries in the config table for year=0, then this is NOT a fresh install, so error out //a fresh install should ONLY have DBVERSION defined in the config table. If there are others (FAIRYEAR, SFIABDIRECTORY) then this is NOT fresh
$q=mysql_query("SELECT * FROM config WHERE year='0'"); $q=mysql_query("SELECT * FROM config WHERE year='0'");
//we might get an error if the config table does not exist (ie, step 2 failed) //we might get an error if the config table does not exist (ie, installer step 2 failed)
if(mysql_error()) if(mysql_error())
{ {
//we say all tables, but really only we check for config where year=0; //we say all tables, but really only we check for config where year=0;
@ -75,10 +67,11 @@ if(mysql_error())
exit; exit;
} }
if(mysql_num_rows($q)) //1 is okay (DBVERSION). More than 1 is bad (already isntalled)
if(mysql_num_rows($q)>1)
{ {
//we say all tables, but really only we check for config where year=0; //we say all tables, but really only we check for config where year=0;
echo "<div class=\"error\">ERROR: Detected existing table data, SFIAB Installation Step 3 requires all tables to be empty.</div>"; echo "<div class=\"error\">ERROR: Detected existing table data, SFIAB Installation Step 3 requires a clean SFIAB database installation.</div>";
echo "</body></html>"; echo "</body></html>";
exit; exit;
} }