module CassandraMigrations::Cassandra

Public Class Methods

execute(*cql) click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 67
def self.execute(*cql)
  connect_to_server unless client
  Rails.logger.try(:info, "\e[1;35m [Cassandra Migrations] \e[0m #{cql.to_s}")
  result = client.execute(*cql)
  QueryResult.new(result) if result
end
restart() click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 34
def self.restart
  if client
    client.close if client.connected?
    self.client = nil
  end

  start!
end
restart!() click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 28
def self.restart!
  raise Errors::ClientNotStartedError unless client

  restart
end
shutdown!() click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 43
def self.shutdown!
  raise Errors::ClientNotStartedError unless client

  client.close if client.connected?
  self.client = nil
end
start!() click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 17
def self.start!
  begin
    # setup keyspace use
    use(Config.keyspace)
  rescue Errors::MissingConfigurationError
    # It's acceptable to not have a configuration file, that's why we rescue this exception.
    # On the other hand, if the user try to execute a query this same exception won't be rescued
    Rails.logger.try(:warn, "There is no config/cassandra.yml. Skipping connection to Cassandra...")
  end
end
use(keyspace) click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 56
def self.use(keyspace)
  connect_to_server unless client

  begin
    client.use(keyspace)
  rescue Exception => e # keyspace does not exist
    puts "#{e} : #{e.message}"
    raise Errors::UnexistingKeyspaceError, keyspace
  end
end
using_keyspace(keyspace, &block) click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 50
def self.using_keyspace(keyspace, &block)
  use(keyspace)
  block.call
  use(Config.keyspace)
end

Private Class Methods

connect_to_server() click to toggle source
# File lib/cassandra_migrations/cassandra.rb, line 76
def self.connect_to_server
  connection_params = Config.connection_config_for_env

  begin
    self.client = Client.connect(connection_params)
  rescue Ione::Io::ConnectionError => e
    raise Errors::ConnectionError, e.message
  end
end