module CassandraObject module Schema class Migration @@verbose = true cattr_accessor :verbose class << self def connection CassandraObject::Base.connection end def 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 # 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: # # http://github.com/fauna/cassandra/blob/master/vendor/0.7/gen-rb/cassandra_types.rb # # Scroll down to the CfDef definition. def create_column_family(name, &block) say_with_time("create_column_family #{name}") do column_family_tasks.create(name, &block) end end # Drops the given column family def drop_column_family(name) say_with_time("drop_column_family #{name}") do column_family_tasks.drop(name) end end # Renames the column family from the old name to the new name def 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 def write(text="") puts(text) if verbose end def announce(message) version = defined?(@version) ? @version : nil text = "#{version} #{name}: #{message}" length = [0, 75 - text.length].max write "== %s %s" % [text, "=" * length] end def say(message, subitem=false) write "#{subitem ? " ->" : "--"} #{message}" end def 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 def suppress_messages save, self.verbose = verbose, false yield ensure self.verbose = save end private def column_family_tasks Tasks::ColumnFamily.new(CassandraObject::Base.connection.keyspace) end end end end end