class CassandraMigrations::Migration

Base class for all cassandra migration

Public Class Methods

method_missing(name, *args, &block) click to toggle source

Delegate missing method calls to an instance. That's what enables the writing of migrations using both +def up+ and +def self.up+ sintax.

Calls superclass method
# File lib/cassandra_migrations/migration.rb, line 39
def self.method_missing(name, *args, &block)
  if instance_to_delegate
    instance_to_delegate.send(name, *args, &block)
  else
    super
  end
end

Public Instance Methods

down() click to toggle source

Makes down work if the method in the migration is defined with self.down

# File lib/cassandra_migrations/migration.rb, line 26
def down
  return unless self.class.respond_to?(:down)
  self.class.instance_to_delegate = self
  self.class.down
end
migrate(direction) click to toggle source

Execute this migration in the named direction.

The advantage of using this instead of directly calling up or down is that this method gives informative output and benchmarks the time taken.

# File lib/cassandra_migrations/migration.rb, line 51
def migrate(direction)
  return unless respond_to?(direction)

  case direction
  when :up   then announce_migration "migrating"
  when :down then announce_migration "reverting"
  end

  time = Benchmark.measure { send(direction) }

  case direction
  when :up   then announce_migration "migrated (%.4fs)" % time.real; puts
  when :down then announce_migration "reverted (%.4fs)" % time.real; puts
  end
end
up() click to toggle source

Makes up work if the method in the migration is defined with self.up

# File lib/cassandra_migrations/migration.rb, line 19
def up
  return unless self.class.respond_to?(:up)
  self.class.instance_to_delegate = self
  self.class.up
end

Private Instance Methods

announce_migration(message) click to toggle source

Generates output labeled with name of migration and a line that goes up to 75 characters long in the terminal

# File lib/cassandra_migrations/migration.rb, line 71
def announce_migration(message)
  text = "#{name}: #{message}"
  length = [0, 75 - text.length].max
  puts "== %s %s" % [text, "=" * length]
end
announce_operation(message) click to toggle source
# File lib/cassandra_migrations/migration.rb, line 77
def announce_operation(message)
  puts "  " + message
end
announce_suboperation(message) click to toggle source
# File lib/cassandra_migrations/migration.rb, line 81
def announce_suboperation(message)
  puts "  -> " + message
end
name() click to toggle source

Gets the name of the migration

# File lib/cassandra_migrations/migration.rb, line 86
def name
  self.class.name
end