wip for db_migrations for core schema

master
Andrew Dolgov 2 years ago
parent 921569e5da
commit c0fb0a5ec0
  1. 26
      classes/config.php
  2. 59
      classes/db/migrations.php
  3. 5
      classes/debug.php
  4. 0
      sql/mysql/migrations/10.sql
  5. 0
      sql/mysql/migrations/100.sql
  6. 0
      sql/mysql/migrations/101.sql
  7. 0
      sql/mysql/migrations/102.sql
  8. 0
      sql/mysql/migrations/103.sql
  9. 0
      sql/mysql/migrations/104.sql
  10. 0
      sql/mysql/migrations/105.sql
  11. 0
      sql/mysql/migrations/106.sql
  12. 0
      sql/mysql/migrations/107.sql
  13. 0
      sql/mysql/migrations/108.sql
  14. 0
      sql/mysql/migrations/109.sql
  15. 0
      sql/mysql/migrations/11.sql
  16. 0
      sql/mysql/migrations/110.sql
  17. 0
      sql/mysql/migrations/111.sql
  18. 0
      sql/mysql/migrations/112.sql
  19. 0
      sql/mysql/migrations/113.sql
  20. 0
      sql/mysql/migrations/114.sql
  21. 0
      sql/mysql/migrations/115.sql
  22. 0
      sql/mysql/migrations/116.sql
  23. 0
      sql/mysql/migrations/117.sql
  24. 0
      sql/mysql/migrations/118.sql
  25. 0
      sql/mysql/migrations/119.sql
  26. 0
      sql/mysql/migrations/12.sql
  27. 0
      sql/mysql/migrations/120.sql
  28. 0
      sql/mysql/migrations/121.sql
  29. 0
      sql/mysql/migrations/122.sql
  30. 0
      sql/mysql/migrations/123.sql
  31. 0
      sql/mysql/migrations/124.sql
  32. 0
      sql/mysql/migrations/125.sql
  33. 0
      sql/mysql/migrations/126.sql
  34. 0
      sql/mysql/migrations/127.sql
  35. 0
      sql/mysql/migrations/128.sql
  36. 0
      sql/mysql/migrations/129.sql
  37. 0
      sql/mysql/migrations/13.sql
  38. 0
      sql/mysql/migrations/130.sql
  39. 0
      sql/mysql/migrations/131.sql
  40. 0
      sql/mysql/migrations/132.sql
  41. 0
      sql/mysql/migrations/133.sql
  42. 0
      sql/mysql/migrations/134.sql
  43. 0
      sql/mysql/migrations/135.sql
  44. 0
      sql/mysql/migrations/136.sql
  45. 0
      sql/mysql/migrations/137.sql
  46. 0
      sql/mysql/migrations/138.sql
  47. 0
      sql/mysql/migrations/139.sql
  48. 0
      sql/mysql/migrations/14.sql
  49. 0
      sql/mysql/migrations/140.sql
  50. 0
      sql/mysql/migrations/141.sql
  51. 0
      sql/mysql/migrations/142.sql
  52. 0
      sql/mysql/migrations/15.sql
  53. 0
      sql/mysql/migrations/16.sql
  54. 0
      sql/mysql/migrations/17.sql
  55. 0
      sql/mysql/migrations/18.sql
  56. 0
      sql/mysql/migrations/19.sql
  57. 0
      sql/mysql/migrations/20.sql
  58. 0
      sql/mysql/migrations/21.sql
  59. 0
      sql/mysql/migrations/22.sql
  60. 0
      sql/mysql/migrations/23.sql
  61. 0
      sql/mysql/migrations/24.sql
  62. 0
      sql/mysql/migrations/25.sql
  63. 0
      sql/mysql/migrations/26.sql
  64. 0
      sql/mysql/migrations/27.sql
  65. 0
      sql/mysql/migrations/28.sql
  66. 0
      sql/mysql/migrations/29.sql
  67. 0
      sql/mysql/migrations/3.sql
  68. 0
      sql/mysql/migrations/30.sql
  69. 0
      sql/mysql/migrations/31.sql
  70. 0
      sql/mysql/migrations/32.sql
  71. 0
      sql/mysql/migrations/33.sql
  72. 0
      sql/mysql/migrations/34.sql
  73. 0
      sql/mysql/migrations/35.sql
  74. 0
      sql/mysql/migrations/36.sql
  75. 0
      sql/mysql/migrations/37.sql
  76. 0
      sql/mysql/migrations/38.sql
  77. 0
      sql/mysql/migrations/39.sql
  78. 0
      sql/mysql/migrations/4.sql
  79. 0
      sql/mysql/migrations/40.sql
  80. 0
      sql/mysql/migrations/41.sql
  81. 0
      sql/mysql/migrations/42.sql
  82. 0
      sql/mysql/migrations/43.sql
  83. 0
      sql/mysql/migrations/44.sql
  84. 0
      sql/mysql/migrations/45.sql
  85. 0
      sql/mysql/migrations/46.sql
  86. 0
      sql/mysql/migrations/47.sql
  87. 0
      sql/mysql/migrations/48.sql
  88. 0
      sql/mysql/migrations/49.sql
  89. 0
      sql/mysql/migrations/5.sql
  90. 0
      sql/mysql/migrations/50.sql
  91. 0
      sql/mysql/migrations/51.sql
  92. 0
      sql/mysql/migrations/52.sql
  93. 0
      sql/mysql/migrations/53.sql
  94. 0
      sql/mysql/migrations/54.sql
  95. 0
      sql/mysql/migrations/55.sql
  96. 0
      sql/mysql/migrations/56.sql
  97. 0
      sql/mysql/migrations/57.sql
  98. 0
      sql/mysql/migrations/58.sql
  99. 0
      sql/mysql/migrations/59.sql
  100. 0
      sql/mysql/migrations/6.sql
  101. 0
      sql/mysql/migrations/60.sql
  102. 0
      sql/mysql/migrations/61.sql
  103. 0
      sql/mysql/migrations/62.sql
  104. 0
      sql/mysql/migrations/63.sql
  105. 0
      sql/mysql/migrations/64.sql
  106. 0
      sql/mysql/migrations/65.sql
  107. 0
      sql/mysql/migrations/66.sql
  108. 0
      sql/mysql/migrations/67.sql
  109. 0
      sql/mysql/migrations/68.sql
  110. 0
      sql/mysql/migrations/69.sql
  111. 0
      sql/mysql/migrations/7.sql
  112. 0
      sql/mysql/migrations/70.sql
  113. 0
      sql/mysql/migrations/71.sql
  114. 0
      sql/mysql/migrations/72.sql
  115. 0
      sql/mysql/migrations/73.sql
  116. 0
      sql/mysql/migrations/74.sql
  117. 0
      sql/mysql/migrations/75.sql
  118. 0
      sql/mysql/migrations/76.sql
  119. 0
      sql/mysql/migrations/77.sql
  120. 0
      sql/mysql/migrations/78.sql
  121. 0
      sql/mysql/migrations/79.sql
  122. 0
      sql/mysql/migrations/8.sql
  123. 0
      sql/mysql/migrations/80.sql
  124. 0
      sql/mysql/migrations/81.sql
  125. 0
      sql/mysql/migrations/82.sql
  126. 0
      sql/mysql/migrations/83.sql
  127. 0
      sql/mysql/migrations/84.sql
  128. 0
      sql/mysql/migrations/85.sql
  129. 0
      sql/mysql/migrations/86.sql
  130. 0
      sql/mysql/migrations/87.sql
  131. 0
      sql/mysql/migrations/88.sql
  132. 0
      sql/mysql/migrations/89.sql
  133. 0
      sql/mysql/migrations/9.sql
  134. 0
      sql/mysql/migrations/90.sql
  135. 0
      sql/mysql/migrations/91.sql
  136. 0
      sql/mysql/migrations/92.sql
  137. 0
      sql/mysql/migrations/93.sql
  138. 0
      sql/mysql/migrations/94.sql
  139. 0
      sql/mysql/migrations/95.sql
  140. 0
      sql/mysql/migrations/96.sql
  141. 0
      sql/mysql/migrations/97.sql
  142. 0
      sql/mysql/migrations/98.sql
  143. 0
      sql/mysql/migrations/99.sql
  144. 0
      sql/mysql/schema.sql
  145. 0
      sql/pgsql/migrations/10.sql
  146. 0
      sql/pgsql/migrations/100.sql
  147. 0
      sql/pgsql/migrations/101.sql
  148. 0
      sql/pgsql/migrations/102.sql
  149. 0
      sql/pgsql/migrations/103.sql
  150. 0
      sql/pgsql/migrations/104.sql
  151. 0
      sql/pgsql/migrations/105.sql
  152. 0
      sql/pgsql/migrations/106.sql
  153. 0
      sql/pgsql/migrations/107.sql
  154. 0
      sql/pgsql/migrations/108.sql
  155. 0
      sql/pgsql/migrations/109.sql
  156. 0
      sql/pgsql/migrations/11.sql
  157. 0
      sql/pgsql/migrations/110.sql
  158. 0
      sql/pgsql/migrations/111.sql
  159. 0
      sql/pgsql/migrations/112.sql
  160. 0
      sql/pgsql/migrations/113.sql
  161. 0
      sql/pgsql/migrations/114.sql
  162. 0
      sql/pgsql/migrations/115.sql
  163. 0
      sql/pgsql/migrations/116.sql
  164. 0
      sql/pgsql/migrations/117.sql
  165. 0
      sql/pgsql/migrations/118.sql
  166. 0
      sql/pgsql/migrations/119.sql
  167. 0
      sql/pgsql/migrations/12.sql
  168. 0
      sql/pgsql/migrations/120.sql
  169. 0
      sql/pgsql/migrations/121.sql
  170. 0
      sql/pgsql/migrations/122.sql
  171. 0
      sql/pgsql/migrations/123.sql
  172. 0
      sql/pgsql/migrations/124.sql
  173. 0
      sql/pgsql/migrations/125.sql
  174. 0
      sql/pgsql/migrations/126.sql
  175. 0
      sql/pgsql/migrations/127.sql
  176. 0
      sql/pgsql/migrations/128.sql
  177. 0
      sql/pgsql/migrations/129.sql
  178. 0
      sql/pgsql/migrations/13.sql
  179. 0
      sql/pgsql/migrations/130.sql
  180. 0
      sql/pgsql/migrations/131.sql
  181. 0
      sql/pgsql/migrations/132.sql
  182. 0
      sql/pgsql/migrations/133.sql
  183. 0
      sql/pgsql/migrations/134.sql
  184. 0
      sql/pgsql/migrations/135.sql
  185. 0
      sql/pgsql/migrations/136.sql
  186. 0
      sql/pgsql/migrations/137.sql
  187. 0
      sql/pgsql/migrations/138.sql
  188. 0
      sql/pgsql/migrations/139.sql
  189. 0
      sql/pgsql/migrations/14.sql
  190. 0
      sql/pgsql/migrations/140.sql
  191. 0
      sql/pgsql/migrations/141.sql
  192. 0
      sql/pgsql/migrations/142.sql
  193. 0
      sql/pgsql/migrations/15.sql
  194. 0
      sql/pgsql/migrations/16.sql
  195. 0
      sql/pgsql/migrations/17.sql
  196. 0
      sql/pgsql/migrations/18.sql
  197. 0
      sql/pgsql/migrations/19.sql
  198. 0
      sql/pgsql/migrations/20.sql
  199. 0
      sql/pgsql/migrations/21.sql
  200. 0
      sql/pgsql/migrations/22.sql
  201. 0
      sql/pgsql/migrations/23.sql
  202. 0
      sql/pgsql/migrations/24.sql
  203. 0
      sql/pgsql/migrations/25.sql
  204. 0
      sql/pgsql/migrations/26.sql
  205. 0
      sql/pgsql/migrations/27.sql
  206. 0
      sql/pgsql/migrations/28.sql
  207. 0
      sql/pgsql/migrations/29.sql
  208. 0
      sql/pgsql/migrations/3.sql
  209. 0
      sql/pgsql/migrations/30.sql
  210. 0
      sql/pgsql/migrations/31.sql
  211. 0
      sql/pgsql/migrations/32.sql
  212. 0
      sql/pgsql/migrations/33.sql
  213. 0
      sql/pgsql/migrations/34.sql
  214. 0
      sql/pgsql/migrations/35.sql
  215. 0
      sql/pgsql/migrations/36.sql
  216. 0
      sql/pgsql/migrations/37.sql
  217. 0
      sql/pgsql/migrations/38.sql
  218. 0
      sql/pgsql/migrations/39.sql
  219. 0
      sql/pgsql/migrations/4.sql
  220. 0
      sql/pgsql/migrations/40.sql
  221. 0
      sql/pgsql/migrations/41.sql
  222. 0
      sql/pgsql/migrations/42.sql
  223. 0
      sql/pgsql/migrations/43.sql
  224. 0
      sql/pgsql/migrations/44.sql
  225. 0
      sql/pgsql/migrations/45.sql
  226. 0
      sql/pgsql/migrations/46.sql
  227. 0
      sql/pgsql/migrations/47.sql
  228. 0
      sql/pgsql/migrations/48.sql
  229. 0
      sql/pgsql/migrations/49.sql
  230. 0
      sql/pgsql/migrations/5.sql
  231. 0
      sql/pgsql/migrations/50.sql
  232. 0
      sql/pgsql/migrations/51.sql
  233. 0
      sql/pgsql/migrations/52.sql
  234. 0
      sql/pgsql/migrations/53.sql
  235. 0
      sql/pgsql/migrations/54.sql
  236. 0
      sql/pgsql/migrations/55.sql
  237. 0
      sql/pgsql/migrations/56.sql
  238. 0
      sql/pgsql/migrations/57.sql
  239. 0
      sql/pgsql/migrations/58.sql
  240. 0
      sql/pgsql/migrations/59.sql
  241. 0
      sql/pgsql/migrations/6.sql
  242. 0
      sql/pgsql/migrations/60.sql
  243. 0
      sql/pgsql/migrations/61.sql
  244. 0
      sql/pgsql/migrations/62.sql
  245. 0
      sql/pgsql/migrations/63.sql
  246. 0
      sql/pgsql/migrations/64.sql
  247. 0
      sql/pgsql/migrations/65.sql
  248. 0
      sql/pgsql/migrations/66.sql
  249. 0
      sql/pgsql/migrations/67.sql
  250. 0
      sql/pgsql/migrations/68.sql
  251. 0
      sql/pgsql/migrations/69.sql
  252. 0
      sql/pgsql/migrations/7.sql
  253. 0
      sql/pgsql/migrations/70.sql
  254. 0
      sql/pgsql/migrations/71.sql
  255. 0
      sql/pgsql/migrations/72.sql
  256. 0
      sql/pgsql/migrations/73.sql
  257. 0
      sql/pgsql/migrations/74.sql
  258. 0
      sql/pgsql/migrations/75.sql
  259. 0
      sql/pgsql/migrations/76.sql
  260. 0
      sql/pgsql/migrations/77.sql
  261. 0
      sql/pgsql/migrations/78.sql
  262. 0
      sql/pgsql/migrations/79.sql
  263. 0
      sql/pgsql/migrations/8.sql
  264. 0
      sql/pgsql/migrations/80.sql
  265. 0
      sql/pgsql/migrations/81.sql
  266. 0
      sql/pgsql/migrations/82.sql
  267. 0
      sql/pgsql/migrations/83.sql
  268. 0
      sql/pgsql/migrations/84.sql
  269. 0
      sql/pgsql/migrations/85.sql
  270. 0
      sql/pgsql/migrations/86.sql
  271. 0
      sql/pgsql/migrations/87.sql
  272. 0
      sql/pgsql/migrations/88.sql
  273. 0
      sql/pgsql/migrations/89.sql
  274. 0
      sql/pgsql/migrations/9.sql
  275. 0
      sql/pgsql/migrations/90.sql
  276. 0
      sql/pgsql/migrations/91.sql
  277. 0
      sql/pgsql/migrations/92.sql
  278. 0
      sql/pgsql/migrations/93.sql
  279. 0
      sql/pgsql/migrations/94.sql
  280. 0
      sql/pgsql/migrations/95.sql
  281. 0
      sql/pgsql/migrations/96.sql
  282. 0
      sql/pgsql/migrations/97.sql
  283. 0
      sql/pgsql/migrations/98.sql
  284. 0
      sql/pgsql/migrations/99.sql
  285. 0
      sql/pgsql/schema.sql
  286. 35
      update.php

@ -114,6 +114,9 @@ class Config {
private $schema_version = null;
private $version = [];
/** @var Db_Migrations $migrations */
private $migrations;
public static function get_instance() : Config {
if (self::$instance == null)
self::$instance = new self();
@ -218,18 +221,25 @@ class Config {
return $rv;
}
static function get_schema_version(bool $nocache = false) {
return self::get_instance()->_schema_version($nocache);
static function get_migrations() : Db_Migrations {
return self::get_instance()->_get_migrations();
}
function _schema_version(bool $nocache = false) {
if (empty($this->schema_version) || $nocache) {
$row = Db::pdo()->query("SELECT schema_version FROM ttrss_version")->fetch();
$this->schema_version = (int) $row["schema_version"];
private function _get_migrations() : Db_Migrations {
if (empty($this->migrations)) {
$this->migrations = new Db_Migrations();
$this->migrations->initialize(dirname(__DIR__) . "/sql", "ttrss_version", true);
}
return $this->schema_version;
return $this->migrations;
}
static function is_migration_needed() : bool {
return self::get_migrations()->is_migration_needed();
}
static function get_schema_version() : int {
return self::get_migrations()->get_version();
}
static function cast_to(string $value, int $type_hint) {

@ -10,6 +10,7 @@ class Db_Migrations {
private $cached_version;
private $cached_max_version;
private $max_version_override;
function __construct() {
$this->pdo = Db::pdo();
@ -22,14 +23,17 @@ class Db_Migrations {
$base_is_latest);
}
function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true) {
function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true, int $max_version_override = 0) {
$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;
$this->max_version_override = $max_version_override;
}
private function set_version(int $version) {
Debug::log("Updating table {$this->migrations_table} with version ${version}...", Debug::LOG_EXTENDED);
$sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}");
if ($res = $sth->fetch()) {
@ -43,7 +47,7 @@ class Db_Migrations {
$this->cached_version = $version;
}
private function get_version() : int {
function get_version() : int {
if (isset($this->cached_version))
return $this->cached_version;
@ -68,10 +72,26 @@ class Db_Migrations {
private function migrate_to(int $version) {
try {
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);
$this->pdo->beginTransaction();
foreach ($this->get_lines($version) as $line) {
$this->pdo->query($line);
Debug::log($line, Debug::LOG_EXTENDED);
try {
$this->pdo->query($line);
} catch (PDOException $e) {
Debug::log("Failed on line: $line");
throw $e;
}
}
if ($version == 0 && $this->base_is_latest)
@ -80,7 +100,10 @@ class Db_Migrations {
$this->set_version($version);
$this->pdo->commit();
Debug::log("Migration finished, current version: " . $this->get_version(), Debug::LOG_VERBOSE);
} catch (PDOException $e) {
Debug::log("Migration failed: " . $e->getMessage(), Debug::LOG_VERBOSE);
try {
$this->pdo->rollback();
} catch (PDOException $ie) {
@ -90,7 +113,10 @@ class Db_Migrations {
}
}
private function get_max_version() : int {
function get_max_version() : int {
if ($this->max_version_override > 0)
return $this->max_version_override;
if (isset($this->cached_max_version))
return $this->cached_max_version;
@ -108,17 +134,32 @@ class Db_Migrations {
return $this->cached_max_version;
}
function is_migration_needed() : bool {
return $this->get_version() != $this->get_max_version();
}
function migrate() : bool {
for ($i = $this->get_version() + 1; $i <= $this->get_max_version(); $i++)
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++) {
try {
$this->migrate_to($i);
} catch (PDOException $e) {
user_error("Failed applying migration $i on table {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING);
user_error("Failed to apply migration ${i} for {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING);
return false;
//throw $e;
}
}
return $this->get_version() == $this->get_max_version();
return !$this->is_migration_needed();
}
private function get_lines(int $version) : array {
@ -134,11 +175,11 @@ class Db_Migrations {
});
return array_filter(explode(";", implode("", $lines)), function ($line) {
return strlen(trim($line)) > 0;
return strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"]);
});
} else {
user_error(E_USER_ERROR, "[migrations] requested schema file ${filename} not found.");
user_error("Requested schema file ${filename} not found.", E_USER_ERROR);
return [];
}
}

@ -1,5 +1,10 @@
<?php
class Debug {
const LOG_DISABLED = -1;
const LOG_NORMAL = 0;
const LOG_VERBOSE = 1;
const LOG_EXTENDED = 2;
public static $LOG_DISABLED = -1;
public static $LOG_NORMAL = 0;
public static $LOG_VERBOSE = 1;

@ -145,10 +145,8 @@
require_once "errorhandler.php";
}
if (!isset($options['update-schema'])) {
if (Db_Updater::is_update_required()) {
die("Schema version is wrong, please upgrade the database (--update-schema).\n");
}
if (!isset($options['update-schema']) && Config::is_migration_needed()) {
die("Schema version is wrong, please upgrade the database (--update-schema).\n");
}
Debug::set_enabled(true);
@ -372,7 +370,32 @@
}
if (isset($options["update-schema"])) {
Debug::log("Checking for updates (" . Config::get(Config::DB_TYPE) . ")...");
if (Config::is_migration_needed()) {
if ($options["update-schema"] != "force-yes") {
Debug::log("Type 'yes' to continue.");
if (read_stdin() != 'yes')
exit;
} else {
Debug::log("Proceeding to update without confirmation...");
}
if (!isset($options["log-level"])) {
Debug::set_loglevel(Debug::$LOG_VERBOSE);
}
$migrations = Config::get_migrations();
Debug::log("Migrating schema to version " . $migrations->get_max_version());
$migrations->migrate();
} else {
Debug::log("Database schema is already at latest version.");
}
/*Debug::log("Checking for updates (" . Config::get(Config::DB_TYPE) . ")...");
$updater = new Db_Updater(Db::pdo(), Config::get(Config::DB_TYPE));
@ -412,7 +435,7 @@
Debug::log("All done.");
} else {
Debug::log("Database schema is already at latest version.");
}
} */
}

Loading…
Cancel
Save