diff --git a/common.inc.php b/common.inc.php index aca2bfc..c10f7de 100644 --- a/common.inc.php +++ b/common.inc.php @@ -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 "SFIAB ERROR"; - echo "

Science Fair In A Box - ERROR

"; - echo "SFIAB installation is not complete. Please go to Installer Step 2 to complete the installation process"; - echo "
"; - echo ""; - exit; -} - -if($dbcodeversion[0]!=$dbdbversion[0]) -{ - echo "SFIAB ERROR"; - echo "

Science Fair In A Box - ERROR

"; - echo "SFIAB database and code are mismatched"; - echo "
"; - echo "Please run the db_update.php script in order to update"; - echo "
"; - echo "your database to the same version as the code"; - echo "
"; - echo "
"; - echo "
"; - echo "

Details

"; - echo "Current SFIAB codebase requires DB version: ".$dbcodeversion[0]; - echo "
"; - echo "Current SFIAB database is detected as version: ".$dbdbversion[0]; - echo "
"; - echo ""; - exit; -} - if(!mysql_connect($DBHOST,$DBUSER,$DBPASS)) { echo "SFIAB ERROR"; @@ -116,7 +82,7 @@ if(!mysql_select_db($DBNAME)) echo ""; 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 "SFIAB ERROR"; + echo "

Science Fair In A Box - ERROR

"; + echo "SFIAB installation is not complete. Please go to Installer Step 2 to complete the installation process"; + echo "
"; + echo ""; + exit; +} + +if($dbcodeversion[0]!=$dbdbversion) +{ + echo "SFIAB ERROR"; + echo "

Science Fair In A Box - ERROR

"; + echo "SFIAB database and code are mismatched"; + echo "
"; + echo "Please run the db_update.php script in order to update"; + echo "
"; + echo "your database to the same version as the code"; + echo "
"; + echo "
"; + echo "
"; + echo "

Details

"; + echo "Current SFIAB codebase requires DB version: ".$dbcodeversion[0]; + echo "
"; + echo "Current SFIAB database is detected as version: ".$dbdbversion; + echo "
"; + echo ""; + exit; +} + + //now pull the rest of the configuration $q=mysql_query("SELECT * FROM config WHERE year='".$config['FAIRYEAR']."'"); while($r=mysql_fetch_object($q)) diff --git a/db/FILES b/db/FILES index 8584023..df19b6b 100644 --- a/db/FILES +++ b/db/FILES @@ -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 diff --git a/db/db.full.7.sql b/db/db.full.7.sql index 117bf38..aea0b3a 100644 --- a/db/db.full.7.sql +++ b/db/db.full.7.sql @@ -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'); diff --git a/db/db.update.7.sql b/db/db.update.7.sql index 2e56898..52db0cd 100644 --- a/db/db.update.7.sql +++ b/db/db.update.7.sql @@ -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'); diff --git a/db/db_update.php b/db/db_update.php index 99aca90..7f0b9c5 100644 --- a/db/db_update.php +++ b/db/db_update.php @@ -1,4 +1,7 @@ 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 version $dbcodeversion
"; 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 "ERROR: found version $dbdbversion
"; - //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) - echo "Your SFIAB database needs to be updated. You should run the update script instead of this installer!\n"; - exit; - } - else - { - echo "Not found (good!)
"; - } + $q=@mysql_query("SELECT val FROM config WHERE var='DBVERSION' AND year='0'"); + $r=@mysql_fetch_object($q); + $dbdbversion=$r->val; + + if($dbdbversion) + { + echo "ERROR: found version $dbdbversion
"; + + //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) + echo "Your SFIAB database needs to be updated. You should run the update script instead of this installer!\n"; + exit; } else { echo "Not found (good!)
"; } - echo "Checking if db/db.db.version.txt is writable... "; - if(is_writable("db/db.db.version.txt")) - { - echo "Yes
"; - } - else - { - echo "ERROR: cannot install. db/db.db.version.txt is not writeable
"; - 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 Done! installed database version $dbcodeversion
\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 "
"; echo "Done!
"; echo "Proceed to installation step 3
"; diff --git a/install3.php b/install3.php index 7e32d74..048eb74 100644 --- a/install3.php +++ b/install3.php @@ -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 "
SFIAB Installation Step 2 is not yet complete.
"; - echo "Go back to installation step 2
"; - echo ""; - 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 "
SFIAB Installation Step 2 is not yet complete.
"; - echo "Go back to installation step 2
"; - echo ""; - exit; + echo "
SFIAB Installation Step 2 is not yet complete.
"; + echo "Go back to installation step 2
"; + echo ""; + 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 "
ERROR: Detected existing table data, SFIAB Installation Step 3 requires all tables to be empty.
"; + echo "
ERROR: Detected existing table data, SFIAB Installation Step 3 requires a clean SFIAB database installation.
"; echo ""; exit; }