From 921569e5da6c3ccbf203506a243cbf499432ec15 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 4 Mar 2021 07:26:05 +0300 Subject: [PATCH] support loading base schema as latest version --- classes/db/migrations.php | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/classes/db/migrations.php b/classes/db/migrations.php index d1eee61d3..f85b4d7c7 100644 --- a/classes/db/migrations.php +++ b/classes/db/migrations.php @@ -5,28 +5,30 @@ class Db_Migrations { private $base_path; private $migrations_path; private $migrations_table; + private $base_is_latest; 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(); } + function initialize_for_plugin(Plugin $plugin, bool $base_is_latest = true, 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)), + $base_is_latest); + } + + function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true) { + $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; + } + private function set_version(int $version) { $sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}"); @@ -72,7 +74,10 @@ class Db_Migrations { $this->pdo->query($line); } - $this->set_version($version); + if ($version == 0 && $this->base_is_latest) + $this->set_version($this->get_max_version()); + else + $this->set_version($version); $this->pdo->commit(); } catch (PDOException $e) {