Class: CassandraObject::Schema::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_object/schema/migration.rb

Constant Summary collapse

@@verbose =
true

Class Method Summary collapse

Class Method Details

.announce(message) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/cassandra_object/schema/migration.rb', line 70

def self.announce(message)
  version = defined?(@version) ? @version : nil

  text = "#{version} #{name}: #{message}"
  length = [0, 75 - text.length].max
  write "== %s %s" % [text, "=" * length]
end

.connectionObject

Returns the raw connection to Cassandra



10
11
12
# File 'lib/cassandra_object/schema/migration.rb', line 10

def self.connection
  CassandraObject::Base.connection
end

.create_column_family(name, &block) ⇒ Object

Creates a new column family with the given name. Column family configurations can be set within a block like this:

create_column_family(:users) do |cf|
  cf.comment = 'Users column family'
  cf.comparator_type = 'TimeUUIDType'
end

A complete list of available configuration settings is here:

github.com/fauna/cassandra/blob/master/vendor/0.7/gen-rb/cassandra_types.rb

Scroll down to the CfDef definition.



46
47
48
49
50
# File 'lib/cassandra_object/schema/migration.rb', line 46

def self.create_column_family(name, &block)
  say_with_time("create_column_family #{name}") do
    column_family_tasks.create(name, CassandraObject::Base.config[:column_family_defaults], &block)
  end
end

.drop_column_family(name) ⇒ Object

Drops the given column family



53
54
55
56
57
# File 'lib/cassandra_object/schema/migration.rb', line 53

def self.drop_column_family(name)
  say_with_time("drop_column_family #{name}") do
    column_family_tasks.drop(name)
  end
end

.migrate(direction) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cassandra_object/schema/migration.rb', line 14

def self.migrate(direction)
  return unless respond_to?(direction)

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

  result = nil
  time = Benchmark.measure { result = send("#{direction}") }

  case direction
  when :up   then announce "migrated (%.4fs)" % time.real; write
  when :down then announce "reverted (%.4fs)" % time.real; write
  end

  result
end

.rename_column_family(old_name, new_name) ⇒ Object

Renames the column family from the old name to the new name



60
61
62
63
64
# File 'lib/cassandra_object/schema/migration.rb', line 60

def self.rename_column_family(old_name, new_name)
  say_with_time("rename_column_family #{name}") do
    column_family_tasks.rename(old_name, new_name)
  end
end

.say(message, subitem = false) ⇒ Object



78
79
80
# File 'lib/cassandra_object/schema/migration.rb', line 78

def self.say(message, subitem=false)
  write "#{subitem ? "   ->" : "--"} #{message}"
end

.say_with_time(message) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/cassandra_object/schema/migration.rb', line 82

def self.say_with_time(message)
  say(message)
  result = nil
  time = Benchmark.measure { result = yield }
  say "%.4fs" % time.real, :subitem
  say("#{result} rows", :subitem) if result.is_a?(Integer)
  result
end

.suppress_messagesObject



91
92
93
94
95
96
# File 'lib/cassandra_object/schema/migration.rb', line 91

def self.suppress_messages
  save, self.verbose = verbose, false
  yield
ensure
  self.verbose = save
end

.write(text = "") ⇒ Object



66
67
68
# File 'lib/cassandra_object/schema/migration.rb', line 66

def self.write(text="")
  puts(text) if verbose
end