class ROM::Cassandra::Migrations::Runner

Base class that loads and runs the migration, registers it in the Cassandra ‘rom.migrations’ table, and logs the result.

As a base class uses the Command pattern to define a sequence of actions, that should be implemented by the subclasses: ‘RunnerUp`, and `RunnderDown`.

@api private

Attributes

logger[R]

@!attribute [r] logger

@return [::Logger] The logger to report results

migration[R]

@!attribute [r] migration

@return [ROM::Cassandra::Migrations::Migration]

The migration class
path[R]

@!attribute [r] path

@return [String] The path to the migration file

session[R]

@!attribute [r] session

@return [ROM::Cassandra::Session]

The session for sending requests to Cassandra
version[R]

@!attribute [r] version

@return [Integer] The number of migration

Public Class Methods

apply(session, logger, path) click to toggle source

Applies the runner to session, logger and migration path

@param (see initialize)

@return (see call)

# File lib/rom/cassandra/migrations/runner.rb, line 58
def self.apply(session, logger, path)
  new(session, logger, path).call
end
new(session, logger, path) click to toggle source

Initializes the runner for the session, logger and migration path

@param [ROM::Cassandra::Session] session @param [Logger] logger @param [String] path

# File lib/rom/cassandra/migrations/runner.rb, line 68
def initialize(session, logger, path)
  @session   = session
  @logger    = logger
  @path      = path
  @version   = extract_version
  @migration = extract_migration if migrate? # defined in a subclass
end

Public Instance Methods

call() click to toggle source

Runs the sequence of commands to provide migration

@return [undefined]

# File lib/rom/cassandra/migrations/runner.rb, line 80
def call
  return unless migration
  apply    # defined in a subclass
  register # defined in a subclass
  log      # defined in a subclass
end
select_version() click to toggle source

Prepares the table and selects version

@return [Array<Hash>] list of rows with the selected version

# File lib/rom/cassandra/migrations/runner.rb, line 91
def select_version
  session.call "CREATE KEYSPACE IF NOT EXISTS rom WITH" \
    " REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 3};"
  session.call "CREATE TABLE IF NOT EXISTS rom.migrations" \
    " (version text, PRIMARY KEY (version));"
  session.call "SELECT * FROM rom.migrations WHERE" \
    " version = '#{version}';"
end

Private Instance Methods

extract_migration() click to toggle source
# File lib/rom/cassandra/migrations/runner.rb, line 108
def extract_migration
  require path
  basename   = File.basename(path, ".rb")
  class_name = Inflecto.camelize basename[/_.+/][1..-1]
  Inflecto.constantize(class_name).new(session)
end
extract_version() click to toggle source
# File lib/rom/cassandra/migrations/runner.rb, line 102
def extract_version
  version = GET_VERSION[path]
  return version unless version.equal? 0
  fail NameError.new "invalid version number: '#{path}'"
end