summaryrefslogtreecommitdiff
path: root/classes/db/migrations.php
blob: d1eee61d3ea72567cf032e0dfcba6811fde32fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
class Db_Migrations {

	private $base_filename = "schema.sql";
	private $base_path;
	private $migrations_path;
	private $migrations_table;
	private $pdo;

	private $cached_version;
	private $cached_max_version;

	function initialize_for_plugin(Plugin $plugin, string $schema_suffix = "sql") {
		$plugin_dir = PluginHost::getInstance()->get_plugin_dir($plugin);
		$this->initialize($plugin_dir . "/${schema_suffix}",
			strtolower("ttrss_migrations_plugin_" . get_class($plugin)));
	}

	function initialize(string $root_path, string $migrations_table) {
		$this->base_path = "$root_path/" . Config::get(Config::DB_TYPE);
		$this->migrations_path = $this->base_path . "/migrations";

		$this->migrations_table = $migrations_table;
	}

	function __construct() {
		$this->pdo = Db::pdo();
	}

	private function set_version(int $version) {
		$sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}");

		if ($res = $sth->fetch()) {
			$sth = $this->pdo->prepare("UPDATE {$this->migrations_table} SET schema_version = ?");
		} else {
			$sth = $this->pdo->prepare("INSERT INTO {$this->migrations_table} (schema_version) VALUES (?)");
		}

		$sth->execute([$version]);

		$this->cached_version = $version;
	}

	private function get_version() : int {
		if (isset($this->cached_version))
			return $this->cached_version;

		try {
			$sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}");

			if ($res = $sth->fetch()) {
				return (int) $res['schema_version'];
			} else {
				return -1;
			}
		} catch (PDOException $e) {
			$this->create_migrations_table();

			return -1;
		}
	}

	private function create_migrations_table() {
		$this->pdo->query("CREATE TABLE IF NOT EXISTS {$this->migrations_table} (schema_version integer not null)");
	}

	private function migrate_to(int $version) {
		try {
			$this->pdo->beginTransaction();

			foreach ($this->get_lines($version) as $line) {
				$this->pdo->query($line);
			}

			$this->set_version($version);

			$this->pdo->commit();
		} catch (PDOException $e) {
			try {
				$this->pdo->rollback();
			} catch (PDOException $ie) {
				//
			}
			throw $e;
		}
	}

	private function get_max_version() : int {
		if (isset($this->cached_max_version))
			return $this->cached_max_version;

		$migrations = glob("{$this->migrations_path}/*.sql");

		if (count($migrations) > 0) {
			natsort($migrations);

			$this->cached_max_version = (int) basename(array_pop($migrations), ".sql");

		} else {
			$this->cached_max_version = 0;
		}

		return $this->cached_max_version;
	}

	function migrate() : bool {

		for ($i = $this->get_version() + 1; $i <= $this->get_max_version(); $i++)
			try {
				$this->migrate_to($i);
			} catch (PDOException $e) {
				user_error("Failed applying migration $i on table {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING);
				//throw $e;
			}

		return $this->get_version() == $this->get_max_version();
	}

	private function get_lines(int $version) : array {
		if ($version > 0)
			$filename = "{$this->migrations_path}/${version}.sql";
		else
			$filename = "{$this->base_path}/{$this->base_filename}";

		if (file_exists($filename)) {
			$lines =	array_filter(preg_split("/[\r\n]/", file_get_contents($filename)),
							function ($line) {
								return strlen(trim($line)) > 0 && strpos($line, "--") !== 0;
							});

			return array_filter(explode(";", implode("", $lines)), function ($line) {
				return strlen(trim($line)) > 0;
			});

		} else {
			user_error(E_USER_ERROR, "[migrations] requested schema file ${filename} not found.");
			return [];
		}
	}
}