Class: ROM::Cassandra::Migrations::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/cassandra/migrations/migration.rb

Overview

Base class for migrations, responcible for sending queries to the session

Examples:

Migration can be created in OOP-style

class CreateAuthUsers < ROM::Cassandra::Migration
  def up
    call keyspace(:auth).create.if_not_exists
    call keyspace(:auth)
      .table(:users)
      .create(:id, :int)
      .add(:name, :text)
      .primary_key(:id)
      .if_not_exists
  end

  def down
    call keyspace(:auth).drop.if_exists
  end
end

The same migration in CQL style

class CreateAuthUsers < ROM::Cassandra::Migration
  def up
    call "CREATE KEYSPACE IF NOT EXISTS auth;"
    call(
      "CREATE TABLE auth.users (id int, name text, PRIMARY KEY (text));"
    )
  end

  def down
    call "DROM KEYSPACE IF EXISTS auth;"
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Migration

Initializes migration with session to the Cassandra cluster

Parameters:



54
55
56
57
# File 'lib/rom/cassandra/migrations/migration.rb', line 54

def initialize(session)
  @session = session
  @builder = Query.new
end

Instance Attribute Details

#sessionROM::Cassandra::Session (readonly)

Returns the session to send queries to.

Returns:



63
64
65
# File 'lib/rom/cassandra/migrations/migration.rb', line 63

def session
  @session
end

Class Method Details

.inherited(klass) ⇒ Object

Makes all helper methods private



46
47
48
# File 'lib/rom/cassandra/migrations/migration.rb', line 46

def self.inherited(klass)
  klass.__send__(:private, :call, :keyspace, :up, :down)
end

Instance Method Details

#call(query) ⇒ Array

Sends the query to Cassandra cluster

Parameters:

  • query (#to_s)

Returns:

  • (Array)


89
90
91
# File 'lib/rom/cassandra/migrations/migration.rb', line 89

def call(query)
  session.call query.to_s
end

#downundefined

This method is abstract.

By default does nothing

Returns:

  • (undefined)


80
81
# File 'lib/rom/cassandra/migrations/migration.rb', line 80

def down
end

#keyspace(name) ⇒ ROM::Cassandra::Query

Starts building the CQL query in the context of some keyspace

Parameters:

  • name (#to_s)

Returns:



99
100
101
# File 'lib/rom/cassandra/migrations/migration.rb', line 99

def keyspace(name)
  @builder.keyspace(name)
end

#upundefined

This method is abstract.

By default does nothing

Returns:

  • (undefined)


71
72
# File 'lib/rom/cassandra/migrations/migration.rb', line 71

def up
end