ttrss/classes/Db_Migrations.php

204 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2021-03-03 20:38:52 +00:00
<?php
class Db_Migrations {
private string $base_filename = "schema.sql";
private string $base_path;
private string $migrations_path;
private string $migrations_table;
private bool $base_is_latest;
private PDO $pdo;
private int $cached_version = 0;
private int $cached_max_version = 0;
private int $max_version_override;
2021-03-03 20:38:52 +00:00
function __construct() {
$this->pdo = Db::pdo();
}
function initialize_for_plugin(Plugin $plugin, bool $base_is_latest = true, string $schema_suffix = "sql"): void {
2021-03-03 20:38:52 +00:00
$plugin_dir = PluginHost::getInstance()->get_plugin_dir($plugin);
$this->initialize("{$plugin_dir}/{$schema_suffix}",
strtolower("ttrss_migrations_plugin_" . get_class($plugin)),
$base_is_latest);
2021-03-03 20:38:52 +00:00
}
function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true, int $max_version_override = 0): void {
2021-03-03 20:38:52 +00:00
$this->base_path = "$root_path/" . Config::get(Config::DB_TYPE);
$this->migrations_path = $this->base_path . "/migrations";
$this->migrations_table = $migrations_table;
$this->base_is_latest = $base_is_latest;
2021-03-04 05:30:52 +00:00
$this->max_version_override = $max_version_override;
2021-03-03 20:38:52 +00:00
}
private function set_version(int $version): void {
Debug::log("Updating table {$this->migrations_table} with version {$version}...", Debug::LOG_EXTENDED);
2021-03-04 05:30:52 +00:00
2021-03-03 20:38:52 +00:00
$sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}");
if ($sth->fetch()) {
2021-03-03 20:38:52 +00:00
$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;
}
2021-03-04 05:30:52 +00:00
function get_version() : int {
if ($this->cached_version)
2021-03-03 20:38:52 +00:00
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(): void {
2021-03-03 20:38:52 +00:00
$this->pdo->query("CREATE TABLE IF NOT EXISTS {$this->migrations_table} (schema_version integer not null)");
}
/**
* @throws PDOException
* @return bool false if the migration failed, otherwise true (or an exception)
*/
private function migrate_to(int $version): bool {
2021-03-03 20:38:52 +00:00
try {
2021-03-04 05:30:52 +00:00
if ($version <= $this->get_version()) {
Debug::log("Refusing to apply version $version: current version is higher", Debug::LOG_VERBOSE);
return false;
}
if ($version == 0)
Debug::log("Loading base database schema...", Debug::LOG_VERBOSE);
else
Debug::log("Starting migration to $version...", Debug::LOG_VERBOSE);
$lines = $this->get_lines($version);
if (count($lines) > 0) {
// mysql doesn't support transactions for DDL statements
if (Config::get(Config::DB_TYPE) != "mysql")
$this->pdo->beginTransaction();
foreach ($lines as $line) {
Debug::log($line, Debug::LOG_EXTENDED);
try {
$this->pdo->query($line);
} catch (PDOException $e) {
Debug::log("Failed on line: $line", Debug::LOG_VERBOSE);
throw $e;
}
2021-03-04 05:30:52 +00:00
}
2021-03-03 20:38:52 +00:00
if ($version == 0 && $this->base_is_latest)
$this->set_version($this->get_max_version());
else
$this->set_version($version);
2021-03-03 20:38:52 +00:00
if (Config::get(Config::DB_TYPE) != "mysql")
$this->pdo->commit();
Debug::log("Migration finished, current version: " . $this->get_version(), Debug::LOG_VERBOSE);
2021-03-04 08:33:25 +00:00
Logger::log(E_USER_NOTICE, "Applied migration to version $version for {$this->migrations_table}");
return true;
} else {
Debug::log("Migration failed: schema file is empty or missing.", Debug::LOG_VERBOSE);
return false;
}
2021-03-04 05:30:52 +00:00
2021-03-03 20:38:52 +00:00
} catch (PDOException $e) {
2021-03-04 05:30:52 +00:00
Debug::log("Migration failed: " . $e->getMessage(), Debug::LOG_VERBOSE);
2021-03-03 20:38:52 +00:00
try {
$this->pdo->rollback();
} catch (PDOException $ie) {
//
}
throw $e;
}
}
2021-03-04 05:30:52 +00:00
function get_max_version() : int {
if ($this->max_version_override > 0)
return $this->max_version_override;
if ($this->cached_max_version)
2021-03-03 20:38:52 +00:00
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;
}
2021-03-04 05:30:52 +00:00
function is_migration_needed() : bool {
return $this->get_version() != $this->get_max_version();
}
2021-03-03 20:38:52 +00:00
function migrate() : bool {
2021-03-04 05:30:52 +00:00
if ($this->get_version() == -1) {
try {
$this->migrate_to(0);
} catch (PDOException $e) {
user_error("Failed to load base schema for {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING);
return false;
}
}
for ($i = $this->get_version() + 1; $i <= $this->get_max_version(); $i++) {
2021-03-03 20:38:52 +00:00
try {
$this->migrate_to($i);
} catch (PDOException $e) {
user_error("Failed to apply migration {$i} for {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING);
2021-03-04 05:30:52 +00:00
return false;
2021-03-03 20:38:52 +00:00
//throw $e;
}
2021-03-04 05:30:52 +00:00
}
2021-03-03 20:38:52 +00:00
2021-03-04 05:30:52 +00:00
return !$this->is_migration_needed();
2021-03-03 20:38:52 +00:00
}
/**
* @return array<int, string>
*/
2021-03-03 20:38:52 +00:00
private function get_lines(int $version) : array {
if ($version > 0)
$filename = "{$this->migrations_path}/{$version}.sql";
2021-03-03 20:38:52 +00:00
else
$filename = "{$this->base_path}/{$this->base_filename}";
if (file_exists($filename)) {
2022-08-12 14:41:21 +00:00
$lines = array_filter(preg_split("/[\r\n]/", file_get_contents($filename)),
fn($line) => strlen(trim($line)) > 0 && strpos($line, "--") !== 0);
return array_filter(explode(";", implode("", $lines)),
fn($line) => strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"]));
2021-03-03 20:38:52 +00:00
} else {
user_error("Requested schema file {$filename} not found.", E_USER_ERROR);
2021-03-03 20:38:52 +00:00
return [];
}
}
}