]> git.wh0rd.org Git - tt-rss.git/blob - classes/dbupdater.php
css fixes as reported by pull 394
[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) {
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                                                 db_query($line);
45                                         }
46                                 }
47
48                                 $db_version = $this->getSchemaVersion();
49
50                                 if ($db_version == $version) {
51                                         db_query("COMMIT");
52                                         return true;
53                                 } else {
54                                         db_query("ROLLBACK");
55                                         return false;
56                                 }
57                         } else {
58                                 return true;
59                         }
60                 } else {
61                         return false;
62                 }
63         }
64
65 } ?>