/models/setup_configure_db.inc

http://flumpshop.googlecode.com/ · Pascal · 57 lines · 36 code · 4 blank · 17 comment · 3 complexity · bc0e195ba016c65f7596d6a5459fac3a MD5 · raw file

  1. <?php
  2. /**
  3. * This model checks the current state of the database and installs/upgrades it
  4. * accordingly. It sets a $configure_log variable which views can use to output
  5. * a log of what this model has done.
  6. *
  7. * This file is part of Flumpshop.
  8. *
  9. * Flumpshop is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Flumpshop is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with Flumpshop. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. *
  23. * @Name models/setup_configure_db.inc
  24. * @Version 1.00
  25. * @author Lloyd Wallis <flump5281@gmail.com>
  26. * @copyright Copyright (c) 2009-2012, Lloyd Wallis
  27. * @package Flumpshop
  28. */
  29. function DBUpgrade($current_version = 1) {
  30. global $dbConn, $configure_log;
  31. $current_version++;
  32. while (file_exists(dirname(__FILE__)."/../sql/DBUpgrade_v".$current_version.".sql")) {
  33. $configure_log .= "Running DBUpgrade (v$current_version...)<br />";
  34. $qry = implode("",file(dirname(__FILE__)."/../sql/DBUpgrade_v".$current_version.".sql"));
  35. $dbConn->multi_query($qry);
  36. $dbConn->query("UPDATE `stats` SET value = '$current_version' WHERE `key`='dbVer' LIMIT 1");
  37. $current_version++;
  38. }
  39. }
  40. $result = $dbConn->query("SELECT `value` FROM `stats` WHERE `key` = 'dbVer' LIMIT 1");
  41. if ($result) {
  42. $result = $result->fetch_assoc();
  43. $configure_log .= 'Upgrading database from version '.$result['value'].'<br />';
  44. DBUpgrade($result['value']);
  45. } else {
  46. $configure_log .= 'No previous installation found. Preparing database.<br />';
  47. $dbConn->multi_query(file_get_contents(dirname(__FILE__)."/../sql/install.sql"),true);
  48. $configure_log .= 'Database installed. Running upgrades.<br />';
  49. DBUpgrade(1);
  50. }
  51. $configure_log .= 'Database upgraded. Resetting values.<br />';
  52. $dbConn->multi_query(file_get_contents(dirname(__FILE__)."/../sql/reset.sql"),true);
  53. $configure_log .= 'Database ready.<br />';