[sfiab-commits] r257 - in trunk: . db
Brought to you by:
sfiab
From: <ja...@li...> - 2005-06-07 21:03:45
|
Author: james Date: 2005-06-07 17:03:55 -0400 (Tue, 07 Jun 2005) New Revision: 257 Modified: trunk/common.inc.php trunk/db/FILES trunk/db/db.full.7.sql trunk/db/db.update.7.sql trunk/db/db_update.php trunk/install2.php trunk/install3.php Log: 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 Modified: trunk/common.inc.php =================================================================== --- trunk/common.inc.php 2005-06-07 20:30:38 UTC (rev 256) +++ trunk/common.inc.php 2005-06-07 21:03:55 UTC (rev 257) @@ -65,40 +65,6 @@ 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 @@ 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 @@ } } +$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)) Modified: trunk/db/FILES =================================================================== --- trunk/db/FILES 2005-06-07 20:30:38 UTC (rev 256) +++ trunk/db/FILES 2005-06-07 21:03:55 UTC (rev 257) @@ -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 Modified: trunk/db/db.full.7.sql =================================================================== --- trunk/db/db.full.7.sql 2005-06-07 20:30:38 UTC (rev 256) +++ trunk/db/db.full.7.sql 2005-06-07 21:03:55 UTC (rev 257) @@ -781,3 +781,5 @@ INSERT INTO `provinces` (`code`, `province`) VALUES ('QC', 'Quc'); 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'); Modified: trunk/db/db.update.7.sql =================================================================== --- trunk/db/db.update.7.sql 2005-06-07 20:30:38 UTC (rev 256) +++ trunk/db/db.update.7.sql 2005-06-07 21:03:55 UTC (rev 257) @@ -44,4 +44,5 @@ INSERT INTO `provinces` (`code`, `province`) VALUES ('QC', 'Quc'); 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'); Modified: trunk/db/db_update.php =================================================================== --- trunk/db/db_update.php 2005-06-07 20:30:38 UTC (rev 256) +++ trunk/db/db_update.php 2005-06-07 21:03:55 UTC (rev 257) @@ -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,16 +13,17 @@ 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]); + echo "Couldnt get current db version. Is SFIAB properly installed?\n"; + exit; } -else -{ - echo "Couldnt load current db.db.version.txt - assuming version is 1\n"; - $dbdbversion=1; -} if($dbcodeversion && $dbdbversion) { @@ -40,39 +44,25 @@ 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++) + echo "Updating database from $dbdbversion to $dbcodeversion\n"; + + for($ver=$dbdbversion+1;$ver<=$dbcodeversion;$ver++) + { + if(file_exists("db.update.$ver.sql")) { - 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 "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"); } - 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 "Version $ver update file not found - skipping over\n"; + } } - else - { - echo "ERROR: cannot perform upgrade. db.db.version.txt is not writeable"; - } + echo "\nAll done - updating new DB version to $dbcodeversion\n"; + mysql_query("UPDATE config SET val='$dbcodeversion' WHERE var='DBVERSION' AND year='0'"); } } Modified: trunk/install2.php =================================================================== --- trunk/install2.php 2005-06-07 20:30:38 UTC (rev 256) +++ trunk/install2.php 2005-06-07 21:03:55 UTC (rev 257) @@ -57,45 +57,29 @@ echo "<b>version $dbcodeversion</b><br />"; 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'"); + $r=@mysql_fetch_object($q); + $dbdbversion=$r->val; + + if($dbdbversion) { - $dbdbversion_file=file("db/db.db.version.txt"); - $dbdbversion=trim($dbdbversion_file[0]); - if($dbdbversion) - { - echo "<b>ERROR: found version $dbdbversion</b><br />"; + 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 />"; - } + //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 @@ 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 />"; Modified: trunk/install3.php =================================================================== --- trunk/install3.php 2005-06-07 20:30:38 UTC (rev 256) +++ trunk/install3.php 2005-06-07 21:03:55 UTC (rev 257) @@ -43,30 +43,22 @@ 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 @@ 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; } |