Class: CassandraMigrations::Migration

Inherits:
Object
  • Object
show all
Includes:
ColumnOperations, TableOperations
Defined in:
lib/cassandra_migrations/migration.rb,
lib/cassandra_migrations/migration/table_definition.rb,
lib/cassandra_migrations/migration/table_operations.rb,
lib/cassandra_migrations/migration/column_operations.rb

Overview

Base class for all cassandra migration

Defined Under Namespace

Modules: ColumnOperations, TableOperations Classes: TableDefinition

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ColumnOperations

#add_column, #remove_column

Methods included from TableOperations

#alter_table, #create_index, #create_table, #drop_index, #drop_table

Class Method Details

.method_missing(name, *args, &block) ⇒ Object

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



39
40
41
42
43
44
45
# 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

Instance Method Details

#downObject

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



26
27
28
29
30
# 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) ⇒ Object

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

#upObject

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



19
20
21
22
23
# 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