]>
Commit | Line | Data |
---|---|---|
b4c47f7e AD |
1 | <?php |
2 | class DbUpdater { | |
3 | ||
6322ac79 | 4 | private $dbh; |
0630a100 | 5 | private $db_type; |
b4c47f7e AD |
6 | private $need_version; |
7 | ||
0630a100 | 8 | function __construct($dbh, $db_type, $need_version) { |
6322ac79 | 9 | $this->dbh = $dbh; |
0630a100 | 10 | $this->db_type = $db_type; |
b4c47f7e AD |
11 | $this->need_version = (int) $need_version; |
12 | } | |
13 | ||
14 | function getSchemaVersion() { | |
0630a100 AD |
15 | $result = db_query("SELECT schema_version FROM ttrss_version"); |
16 | return (int) db_fetch_result($result, 0, "schema_version"); | |
b4c47f7e AD |
17 | } |
18 | ||
19 | function isUpdateRequired() { | |
20 | return $this->getSchemaVersion() < $this->need_version; | |
21 | } | |
22 | ||
23 | function getSchemaLines($version) { | |
0630a100 | 24 | $filename = "schema/versions/".$this->db_type."/$version.sql"; |
b4c47f7e AD |
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 | ||
0630a100 | 40 | db_query("BEGIN"); |
b4c47f7e AD |
41 | |
42 | foreach ($lines as $line) { | |
43 | if (strpos($line, "--") !== 0 && $line) { | |
0630a100 | 44 | db_query($line); |
b4c47f7e AD |
45 | } |
46 | } | |
47 | ||
0630a100 | 48 | $db_version = $this->getSchemaVersion(); |
b4c47f7e | 49 | |
0630a100 AD |
50 | if ($db_version == $version) { |
51 | db_query("COMMIT"); | |
b4c47f7e AD |
52 | return true; |
53 | } else { | |
0630a100 | 54 | db_query("ROLLBACK"); |
b4c47f7e AD |
55 | return false; |
56 | } | |
57 | } else { | |
58 | return true; | |
59 | } | |
60 | } else { | |
61 | return false; | |
62 | } | |
63 | } | |
64 | ||
65 | } ?> |