class Redcord::Migration::Migrator

Constants

MIGRATION_FILENAME_REGEX

Public Class Methods

migrate(redis:, version:, direction:) click to toggle source
# File lib/redcord/migration/migrator.rb, line 14
def self.migrate(redis:, version:, direction:)
  migration = load_version(version)
  print [
    T.must("#{redis.inspect.match('(redis://.*)>')[1]}"[0...30]),
    direction.to_s.upcase,
    version,
    T.must(migration.name).underscore.humanize,
  ].map { |str| str.ljust(30) }.join("\t")

  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  migration.new(redis).send(direction)
  if direction == :up
    redis.sadd(
      Redcord::Migration::Version::MIGRATION_VERSIONS_REDIS_KEY,
      version,
    )
  else
    redis.srem(
      Redcord::Migration::Version::MIGRATION_VERSIONS_REDIS_KEY,
      version,
    )
  end
  end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  puts "\t#{(end_time - start_time) * 1000.0.round(3)} ms"
end
need_to_migrate?(redis) click to toggle source
# File lib/redcord/migration/migrator.rb, line 7
def self.need_to_migrate?(redis)
  local_version = Redcord::Migration::Version.new
  remote_version = Redcord::Migration::Version.new(redis: redis)
  !(local_version.all - remote_version.all).empty?
end

Private Class Methods

load_version(version) click to toggle source
# File lib/redcord/migration/migrator.rb, line 43
def self.load_version(version)
  file = T.must(migration_files.select { |f| f.match(version) }.first)
  require(File.expand_path(file))
  underscore_const_name = parse_migration_filename(file)[1]
  Object.const_get(underscore_const_name.camelize)
end
migration_files() click to toggle source
# File lib/redcord/migration/migrator.rb, line 63
def self.migration_files
  paths = migrations_paths
  # Use T.unsafe to workaround sorbet: splat the paths
  T.unsafe(Dir)[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
end
migrations_paths() click to toggle source
# File lib/redcord/migration/migrator.rb, line 58
def self.migrations_paths
  @@migrations_paths
end
parse_migration_filename(filename) click to toggle source
# File lib/redcord/migration/migrator.rb, line 71
def self.parse_migration_filename(filename)
  T.unsafe(File.basename(filename).scan(MIGRATION_FILENAME_REGEX).first)
end