Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pending migrations check on startup. Includes index change migration #4746

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/invidious.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ end
OUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mode: "a")
LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level)

# Check pending migrations
Invidious::Database::Migrator.new(PG_DB).check_pending_migrations

# Check table integrity
Invidious::Database.check_integrity(CONFIG)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this method while working... and this is a bit of a monster hidden behind a simple name. It performs database updates besides doing checks. This might have to be taken out completely once the migration files are for sure the way forward since it reads the sql files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a remnant I haven't dare to touch. I think that it should be safe to remove all of that code.


Expand Down
9 changes: 9 additions & 0 deletions src/invidious/database/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ abstract class Invidious::Database::Migration
end

@@version : Int64?
@@required : Bool = false

def self.version(version : Int32 | Int64)
@@version = version.to_i64
end

def self.required(required : Bool)
@@required = required
end

getter? completed = false

def initialize(@db : DB::Database)
Expand All @@ -32,6 +37,10 @@ abstract class Invidious::Database::Migration
@@version.not_nil!
end

def required? : Bool
@@required
end

private def track(conn : DB::Connection)
conn.exec("INSERT INTO #{Migrator::MIGRATIONS_TABLE} (version) VALUES ($1)", version)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Invidious::Database::Migrations
class LimitChannelVideosIndex < Migration
version 11
required false

def up(conn : DB::Connection)
conn.exec <<-SQL
CREATE INDEX IF NOT EXISTS channel_videos_ucid_published_idx
ON public.channel_videos
USING btree
(ucid COLLATE pg_catalog."default", published);
SQL

conn.exec <<-SQL
DROP INDEX IF EXISTS channel_videos_ucid_idx;
SQL
end
end
end
16 changes: 13 additions & 3 deletions src/invidious/database/migrator.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Invidious::Database::Migrator
MIGRATIONS_TABLE = "public.invidious_migrations"
MIGRATE_INSTRUCTION = "Run `invidious --migrate` to apply the migration(s)."

class_getter migrations = [] of Invidious::Database::Migration.class

Expand All @@ -22,11 +23,20 @@ class Invidious::Database::Migrator
puts "No migrations to run." unless ran_migration
end

def pending_migrations? : Bool
def check_pending_migrations
versions = load_versions

load_migrations.sort_by(&.version)
.any? { |migration| !versions.includes?(migration.version) }
pending_migrations = load_migrations.sort_by(&.version)
.select { |migration| !versions.includes?(migration.version) }

return if pending_migrations.empty?

if pending_migrations.any?(&.required?)
LOGGER.error("There are pending migrations and the application is unable to continue. #{MIGRATE_INSTRUCTION}")
exit 1
else
LOGGER.warn("There are pending migrations. #{MIGRATE_INSTRUCTION}")
end
end

private def load_migrations : Array(Invidious::Database::Migration)
Expand Down
Loading