]>
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 | ||
977cea14 | 33 | function performUpdateTo($version, $html_output = true) { |
b4c47f7e AD |
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) { | |
977cea14 AD |
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 | } | |
b4c47f7e AD |
55 | } |
56 | } | |
57 | ||
0630a100 | 58 | $db_version = $this->getSchemaVersion(); |
b4c47f7e | 59 | |
0630a100 AD |
60 | if ($db_version == $version) { |
61 | db_query("COMMIT"); | |
b4c47f7e AD |
62 | return true; |
63 | } else { | |
0630a100 | 64 | db_query("ROLLBACK"); |
b4c47f7e AD |
65 | return false; |
66 | } | |
67 | } else { | |
68 | return true; | |
69 | } | |
70 | } else { | |
71 | return false; | |
72 | } | |
73 | } | |
74 | ||
75 | } ?> |