]> git.wh0rd.org - tt-rss.git/blame - classes/dbupdater.php
pngcrush.sh
[tt-rss.git] / classes / dbupdater.php
CommitLineData
b4c47f7e
AD
1<?php
2class DbUpdater {
3
1d92297a 4 private $pdo;
0630a100 5 private $db_type;
b4c47f7e
AD
6 private $need_version;
7
1d92297a
AD
8 function __construct($pdo, $db_type, $need_version) {
9 $this->pdo = Db::pdo(); //$pdo;
0630a100 10 $this->db_type = $db_type;
b4c47f7e
AD
11 $this->need_version = (int) $need_version;
12 }
13
14 function getSchemaVersion() {
1d92297a
AD
15 $row = $this->pdo->query("SELECT schema_version FROM ttrss_version")->fetch();
16 return (int) $row['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 {
1d92297a 29 user_error("DB Updater: schema file for version $version is not found.");
b4c47f7e
AD
30 return false;
31 }
32 }
33
977cea14 34 function performUpdateTo($version, $html_output = true) {
b4c47f7e
AD
35 if ($this->getSchemaVersion() == $version - 1) {
36
37 $lines = $this->getSchemaLines($version);
38
39 if (is_array($lines)) {
40
1d92297a 41 $this->pdo->beginTransaction();
b4c47f7e
AD
42
43 foreach ($lines as $line) {
44 if (strpos($line, "--") !== 0 && $line) {
1d92297a 45 if (!$this->pdo->query($line)) {
977cea14
AD
46 if ($html_output) {
47 print_notice("Query: $line");
1d92297a 48 print_error("Error: " . implode(", ", $this->pdo->errorInfo()));
977cea14 49 } else {
c10a4306
AD
50 Debug::log("Query: $line");
51 Debug::log("Error: " . implode(", ", $this->pdo->errorInfo()));
977cea14
AD
52 }
53
54 return false;
55 }
b4c47f7e
AD
56 }
57 }
58
0630a100 59 $db_version = $this->getSchemaVersion();
b4c47f7e 60
0630a100 61 if ($db_version == $version) {
1d92297a 62 $this->pdo->commit();
b4c47f7e
AD
63 return true;
64 } else {
1d92297a 65 $this->pdo->rollBack();
b4c47f7e
AD
66 return false;
67 }
68 } else {
57a1143c 69 return false;
b4c47f7e
AD
70 }
71 } else {
72 return false;
73 }
74 }
75
ea79a0e0 76}