Class: Rubino::Database::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/database/migrator.rb

Overview

Handles database schema migrations in order. Migrations are stored as numbered Sequel migration files.

Constant Summary collapse

MIGRATIONS_PATH =
File.expand_path("migrations", __dir__)

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Migrator

Returns a new instance of Migrator.



13
14
15
# File 'lib/rubino/database/migrator.rb', line 13

def initialize(connection)
  @connection = connection
end

Instance Method Details

#current_versionObject

Returns current migration version



23
24
25
26
27
# File 'lib/rubino/database/migrator.rb', line 23

def current_version
  Sequel::Migrator.get_current_migration_version(@connection.db)
rescue StandardError
  0
end

#migrate!Object

Runs all pending migrations



18
19
20
# File 'lib/rubino/database/migrator.rb', line 18

def migrate!
  Sequel::Migrator.run(@connection.db, MIGRATIONS_PATH)
end

#pending?Boolean

Returns true if there are unapplied migrations.

Intentionally does NOT rescue: a connection/schema error here is a real health problem and must propagate so callers (e.g. doctor) can report a failure instead of silently treating an unreachable DB as “up to date”.

Returns:

  • (Boolean)


34
35
36
# File 'lib/rubino/database/migrator.rb', line 34

def pending?
  !Sequel::Migrator.is_current?(@connection.db, MIGRATIONS_PATH)
end

#pending_migrationsObject

Returns list of pending migration files



39
40
41
42
43
44
45
# File 'lib/rubino/database/migrator.rb', line 39

def pending_migrations
  Sequel::Migrator.migrator_class(MIGRATIONS_PATH)
                  .new(@connection.db, MIGRATIONS_PATH)
                  .files
rescue StandardError
  []
end