]>
Commit | Line | Data |
---|---|---|
b4c47f7e AD |
1 | <?php |
2 | class DbUpdater { | |
3 | ||
6322ac79 | 4 | private $dbh; |
b4c47f7e AD |
5 | private $db_type; |
6 | private $need_version; | |
7 | ||
6322ac79 AD |
8 | function __construct($dbh, $db_type, $need_version) { |
9 | $this->dbh = $dbh; | |
b4c47f7e AD |
10 | $this->db_type = $db_type; |
11 | $this->need_version = (int) $need_version; | |
12 | } | |
13 | ||
14 | function getSchemaVersion() { | |
6322ac79 | 15 | $result = db_query( "SELECT schema_version FROM ttrss_version"); |
b4c47f7e AD |
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 | ||
6322ac79 | 40 | db_query( "BEGIN"); |
b4c47f7e AD |
41 | |
42 | foreach ($lines as $line) { | |
43 | if (strpos($line, "--") !== 0 && $line) { | |
6322ac79 | 44 | db_query( $line); |
b4c47f7e AD |
45 | } |
46 | } | |
47 | ||
48 | $db_version = $this->getSchemaVersion(); | |
49 | ||
50 | if ($db_version == $version) { | |
6322ac79 | 51 | db_query( "COMMIT"); |
b4c47f7e AD |
52 | return true; |
53 | } else { | |
6322ac79 | 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 | } ?> |