dbupdater.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. class DbUpdater {
  3. private $dbh;
  4. private $db_type;
  5. private $need_version;
  6. function __construct($dbh, $db_type, $need_version) {
  7. $this->dbh = $dbh;
  8. $this->db_type = $db_type;
  9. $this->need_version = (int) $need_version;
  10. }
  11. function getSchemaVersion() {
  12. $result = db_query("SELECT schema_version FROM ttrss_version");
  13. return (int) db_fetch_result($result, 0, "schema_version");
  14. }
  15. function isUpdateRequired() {
  16. return $this->getSchemaVersion() < $this->need_version;
  17. }
  18. function getSchemaLines($version) {
  19. $filename = "schema/versions/".$this->db_type."/$version.sql";
  20. if (file_exists($filename)) {
  21. return explode(";", preg_replace("/[\r\n]/", "", file_get_contents($filename)));
  22. } else {
  23. return false;
  24. }
  25. }
  26. function performUpdateTo($version) {
  27. if ($this->getSchemaVersion() == $version - 1) {
  28. $lines = $this->getSchemaLines($version);
  29. if (is_array($lines)) {
  30. db_query("BEGIN");
  31. foreach ($lines as $line) {
  32. if (strpos($line, "--") !== 0 && $line) {
  33. db_query($line);
  34. }
  35. }
  36. $db_version = $this->getSchemaVersion();
  37. if ($db_version == $version) {
  38. db_query("COMMIT");
  39. return true;
  40. } else {
  41. db_query("ROLLBACK");
  42. return false;
  43. }
  44. } else {
  45. return true;
  46. }
  47. } else {
  48. return false;
  49. }
  50. }
  51. } ?>