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;
}
//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))
{
echo "<html><head><title>SFIAB ERROR</title></head><body>";
@ -116,7 +82,7 @@ if(!mysql_select_db($DBNAME))
echo "</body></html>";
exit;
}
//find out the fair year and any other 'year=0' configuration parameters (things that dont change as the years go on)
$q=mysql_query("SELECT * FROM config WHERE year='0'");
if(!mysql_num_rows($q))
@ -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
$q=mysql_query("SELECT * FROM config WHERE year='".$config['FAIRYEAR']."'");
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.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.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 ('SK', 'Saskatchewan');
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 ('SK', 'Saskatchewan');
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"))
{
$dbcodeversion_file=file("db.code.version.txt");
@ -10,15 +13,16 @@ else
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");
$dbdbversion=trim($dbdbversion_file[0]);
}
else
{
echo "Couldnt load current db.db.version.txt - assuming version is 1\n";
$dbdbversion=1;
echo "Couldnt get current db version. Is SFIAB properly installed?\n";
exit;
}
if($dbcodeversion && $dbdbversion)
@ -40,39 +44,25 @@ if($dbcodeversion && $dbdbversion)
echo "DB update requirements detected\n";
echo "Current DB Version: $dbdbversion\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";
//include the config.inc.php
//so we have the db connection info
require("../data/config.inc.php");
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";
$fp=fopen("db.db.version.txt","w");
fputs($fp,$dbcodeversion."\n");
fclose($fp);
}
else
echo "Updating database from $dbdbversion to $dbcodeversion\n";
for($ver=$dbdbversion+1;$ver<=$dbcodeversion;$ver++)
{
echo "ERROR: cannot perform upgrade. db.db.version.txt is not writeable";
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'");
}
}

View File

@ -57,45 +57,29 @@ mysql_select_db($DBNAME);
echo "<b>version $dbcodeversion</b><br />";
echo "Checking for existing SFIAB database... ";
if(file_exists("db/db.db.version.txt"))
{
$dbdbversion_file=file("db/db.db.version.txt");
$dbdbversion=trim($dbdbversion_file[0]);
if($dbdbversion)
{
echo "<b>ERROR: found version $dbdbversion</b><br />";
//lets see if they match
if($dbcodeversion == $dbdbversion)
echo "Your SFIAB database is already setup with the required version\n";
else if($dbcodeversion<$dbdbversion)
echo "ERROR: dbcodeversion<dbdbversion ($dbcodeversion<$dbdbversion). This should not happen!";
else if($dbcodeversion>$dbdbversion)
echo "Your SFIAB database needs to be updated. You should run the update script instead of this installer!\n";
exit;
}
else
{
echo "<b>Not found (good!)</b><br />";
}
$q=@mysql_query("SELECT val FROM config WHERE var='DBVERSION' AND year='0'");
$r=@mysql_fetch_object($q);
$dbdbversion=$r->val;
if($dbdbversion)
{
echo "<b>ERROR: found version $dbdbversion</b><br />";
//lets see if they match
if($dbcodeversion == $dbdbversion)
echo "Your SFIAB database is already setup with the required version\n";
else if($dbcodeversion<$dbdbversion)
echo "ERROR: dbcodeversion<dbdbversion ($dbcodeversion<$dbdbversion). This should not happen!";
else if($dbcodeversion>$dbdbversion)
echo "Your SFIAB database needs to be updated. You should run the update script instead of this installer!\n";
exit;
}
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... ";
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");
echo "<b>Done! installed database version $dbcodeversion</b><br />\n";
$fp=fopen("db/db.db.version.txt","w");
fputs($fp,$dbcodeversion."\n");
fclose($fp);
//now update the db version in the database
mysql_query("UPDATE config SET val='$dbcodeversion' WHERE var='DBVERSION' AND year='0'");
echo "<br />";
echo "<b>Done!</b><br />";
echo "<a href=\"install3.php\">Proceed to installation step 3</a><br />";

View File

@ -43,30 +43,22 @@ mysql_connect($DBHOST,$DBUSER,$DBPASS);
mysql_select_db($DBNAME);
echo "Checking for SFIAB database... ";
if(file_exists("db/db.db.version.txt"))
{
$dbdbversion_file=file("db/db.db.version.txt");
$dbdbversion=trim($dbdbversion_file[0]);
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
$q=@mysql_query("SELECT val FROM config WHERE var='DBVERSION' AND year='0'");
$r=@mysql_fetch_object($q);
$dbdbversion=$r->val;
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;
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;
}
//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'");
//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())
{
//we say all tables, but really only we check for config where year=0;
@ -75,10 +67,11 @@ if(mysql_error())
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;
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>";
exit;
}