Run migrations through CLI instead of when app starts

This commit is contained in:
matthewmcgarvey 2022-02-11 22:43:16 -06:00
parent cf13c11236
commit 59654289cb
2 changed files with 15 additions and 1 deletions

View File

@ -102,6 +102,10 @@ Kemal.config.extra_options do |parser|
puts SOFTWARE.to_pretty_json puts SOFTWARE.to_pretty_json
exit exit
end end
parser.on("--migrate", "Run any migrations") do
Invidious::Database::Migrator.new(PG_DB).migrate
exit
end
end end
Kemal::CLI.new ARGV 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) LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level)
# Run migrations # 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 # Check table integrity
Invidious::Database.check_integrity(CONFIG) Invidious::Database.check_integrity(CONFIG)

View File

@ -22,6 +22,13 @@ class Invidious::Database::Migrator
puts "No migrations to run." unless ran_migration puts "No migrations to run." unless ran_migration
end 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) private def load_migrations : Array(Invidious::Database::Migration)
self.class.migrations.map(&.new(@db)) self.class.migrations.map(&.new(@db))
end end