From 59654289cb7c024d3cf7c9d00d38cf4453bc48df Mon Sep 17 00:00:00 2001 From: matthewmcgarvey Date: Fri, 11 Feb 2022 22:43:16 -0600 Subject: [PATCH] Run migrations through CLI instead of when app starts --- src/invidious.cr | 9 ++++++++- src/invidious/database/migrator.cr | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/invidious.cr b/src/invidious.cr index e8ad03ef..25ee7c78 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -102,6 +102,10 @@ Kemal.config.extra_options do |parser| puts SOFTWARE.to_pretty_json exit end + parser.on("--migrate", "Run any migrations") do + Invidious::Database::Migrator.new(PG_DB).migrate + exit + end end Kemal::CLI.new ARGV @@ -113,7 +117,10 @@ OUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mo LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level) # Run migrations -Invidious::Database::Migrator.new(PG_DB).migrate +if Invidious::Database::Migrator.new(PG_DB).pending_migrations? + puts "There are pending migrations. Run `invidious --migrate` to apply the migrations." + exit 46 +end # Check table integrity Invidious::Database.check_integrity(CONFIG) diff --git a/src/invidious/database/migrator.cr b/src/invidious/database/migrator.cr index 2cd869c9..660c3203 100644 --- a/src/invidious/database/migrator.cr +++ b/src/invidious/database/migrator.cr @@ -22,6 +22,13 @@ class Invidious::Database::Migrator puts "No migrations to run." unless ran_migration end + def pending_migrations? : Bool + versions = load_versions + + load_migrations.sort_by(&.version) + .any? { |migration| !versions.includes?(migration.version) } + end + private def load_migrations : Array(Invidious::Database::Migration) self.class.migrations.map(&.new(@db)) end