]> git.wh0rd.org - tt-rss.git/blob - classes/dbupdater.php
bump VERSION_STATIC due to Dojo changes
[tt-rss.git] / classes / dbupdater.php
1 <?php
2 class DbUpdater {
3
4 private $dbh;
5 private $db_type;
6 private $need_version;
7
8 function __construct($dbh, $db_type, $need_version) {
9 $this->dbh = $dbh;
10 $this->db_type = $db_type;
11 $this->need_version = (int) $need_version;
12 }
13
14 function getSchemaVersion() {
15 $result = db_query("SELECT schema_version FROM ttrss_version");
16 return (int) db_fetch_result($result, 0, "schema_version");
17 }
18
19 function isUpdateRequired() {
20 return $this->getSchemaVersion() < $this->need_version;
21 }
22
23 function getSchemaLines($version) {
24 $filename = "schema/versions/".$this->db_type."/$version.sql";
25
26 if (file_exists($filename)) {
27 return explode(";", preg_replace("/[\r\n]/", "", file_get_contents($filename)));
28 } else {
29 return false;
30 }
31 }
32
33 function performUpdateTo($version, $html_output = true) {
34 if ($this->getSchemaVersion() == $version - 1) {
35
36 $lines = $this->getSchemaLines($version);
37
38 if (is_array($lines)) {
39
40 db_query("BEGIN");
41
42 foreach ($lines as $line) {
43 if (strpos($line, "--") !== 0 && $line) {
44 if (!db_query($line, false)) {
45 if ($html_output) {
46 print_notice("Query: $line");
47 print_error("Error: " . db_last_query_error());
48 } else {
49 _debug("Query: $line");
50 _debug("Error: " . db_last_query_error());
51 }
52
53 return false;
54 }
55 }
56 }
57
58 $db_version = $this->getSchemaVersion();
59
60 if ($db_version == $version) {
61 db_query("COMMIT");
62 return true;
63 } else {
64 db_query("ROLLBACK");
65 return false;
66 }
67 } else {
68 return true;
69 }
70 } else {
71 return false;
72 }
73 }
74
75 } ?>