Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiping Cassandra DB between tests (Rspec)

I'm running rspec tests that involve data in a Cassandra db. What is the best practice here for wiping/cleaning the db between tests? For my mongo data, I'm using DatabaseCleaner, and am looking for a Cassandra equivalent. I'm currently doing the following in my spec_helper.rb but it's very slow, so am looking for a better solution. Thanks!

config.before :each do
    ['column1', 'column2'].each do |name|
      begin
        $cassandra.drop_column_family(name)
      rescue
        next
      ensure
        cf = Cassandra::ColumnFamily.new(keyspace: 'db_name', name: name, comparator_type: 'TimeUUIDType')
        $cassandra.add_column_family(cf)
      end
    end
like image 351
jyli7 Avatar asked Oct 07 '22 10:10

jyli7


2 Answers

Old question, but I've found useful gist for it, maybe it will help someone else too (I'm using cequel gem to access Cassandra):

https://gist.github.com/elado/c95a4ffa952809865ee8

# in spec_helper.rb

RSpec.configure do |config|
  records = []

  config.before :suite do
    Cequel::Record.descendants.each do |klass|
      klass.after_create {|r| records << r }
    end
  end

  config.after :each do
    records.each(&:destroy)
    records.clear
  end

  def clean_cequel!
    Cequel::Record.descendants.each { |klass| Cequel::Record.connection.schema.truncate_table(klass.table_name) }
  end

  config.before :suite do
    clean_cequel!
  end

  config.after :suite do
    clean_cequel!
  end
end
like image 87
santuxus Avatar answered Oct 10 '22 18:10

santuxus


Try to truncate, disable autoSnapshot in your cassandra.yaml and disable durable_writes on your keyspace.

Truncate used to be quite slow, but this is fixed since 1.1.1: https://issues.apache.org/jira/browse/CASSANDRA-4153

like image 27
Christian Spriegel Avatar answered Oct 10 '22 17:10

Christian Spriegel