Skip to content

Commit 5965428

Browse files
Run migrations through CLI instead of when app starts
1 parent cf13c11 commit 5965428

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/invidious.cr

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ Kemal.config.extra_options do |parser|
102102
puts SOFTWARE.to_pretty_json
103103
exit
104104
end
105+
parser.on("--migrate", "Run any migrations") do
106+
Invidious::Database::Migrator.new(PG_DB).migrate
107+
exit
108+
end
105109
end
106110

107111
Kemal::CLI.new ARGV
@@ -113,7 +117,10 @@ OUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mo
113117
LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level)
114118

115119
# Run migrations
116-
Invidious::Database::Migrator.new(PG_DB).migrate
120+
if Invidious::Database::Migrator.new(PG_DB).pending_migrations?
121+
puts "There are pending migrations. Run `invidious --migrate` to apply the migrations."
122+
exit 46
123+
end
117124
# Check table integrity
118125
Invidious::Database.check_integrity(CONFIG)
119126

src/invidious/database/migrator.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class Invidious::Database::Migrator
2222
puts "No migrations to run." unless ran_migration
2323
end
2424

25+
def pending_migrations? : Bool
26+
versions = load_versions
27+
28+
load_migrations.sort_by(&.version)
29+
.any? { |migration| !versions.includes?(migration.version) }
30+
end
31+
2532
private def load_migrations : Array(Invidious::Database::Migration)
2633
self.class.migrations.map(&.new(@db))
2734
end

0 commit comments

Comments
 (0)