Module: CassandraMigrations::Migration::TableOperations

Included in:
CassandraMigrations::Migration
Defined in:
lib/cassandra_migrations/migration/table_operations.rb

Overview

Module grouping methods used in migrations to make table operations like:

  • creating tables

  • dropping tables

Instance Method Summary collapse

Instance Method Details

#alter_table(table_name, options = {}) {|table_definition| ... } ⇒ Object

Yields:

  • (table_definition)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cassandra_migrations/migration/table_operations.rb', line 39

def alter_table(table_name, options={})
  table_definition = TableDefinition.new
  table_definition.define_options(options[:options]) if options[:options]

  yield table_definition if block_given?

  announce_operation "alter_table #{table_name}"

  create_cql =  "ALTER TABLE #{table_name}"
  create_cql << table_definition.options

  announce_suboperation create_cql

  execute create_cql
end

#create_index(table_name, column_name, options = {}) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/cassandra_migrations/migration/table_operations.rb', line 55

def create_index(table_name, column_name, options = {})
  announce_operation "create_index(#{table_name})"
  create_index_cql = "CREATE INDEX #{options[:name]} ON #{table_name} (#{column_name})".squeeze(' ')
  announce_suboperation create_index_cql

  execute create_index_cql
end

#create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object

Creates a new table in the keyspace

options:

  • :primary_keys: single value or array (for compound primary keys). If

not defined, some column must be chosen as primary key in the table definition.

Yields:

  • (table_definition)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cassandra_migrations/migration/table_operations.rb', line 19

def create_table(table_name, options = {})
  table_definition = TableDefinition.new
  table_definition.define_primary_keys(options[:primary_keys]) if options[:primary_keys]
  table_definition.define_partition_keys(options[:partition_keys]) if options[:partition_keys]
  table_definition.define_options(options[:options]) if options[:options]

  yield table_definition if block_given?

  announce_operation "create_table(#{table_name})"

  create_cql =  "CREATE TABLE #{table_name} ("
  create_cql << table_definition.to_create_cql
  create_cql << ")"
  create_cql << table_definition.options

  announce_suboperation create_cql

  execute create_cql
end

#drop_index(table_or_index_name, column_name = nil, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cassandra_migrations/migration/table_operations.rb', line 72

def drop_index(table_or_index_name, column_name = nil, options = {})
  if column_name
    index_name = "#{table_or_index_name}_#{column_name}_idx"
  else
    index_name = table_or_index_name
  end
  drop_index_cql = "DROP INDEX #{options[:if_exists] ? 'IF EXISTS' : ''}#{index_name}"
  announce_suboperation drop_index_cql

  execute drop_index_cql
end

#drop_table(table_name) ⇒ Object

Drops a table



64
65
66
67
68
69
70
# File 'lib/cassandra_migrations/migration/table_operations.rb', line 64

def drop_table(table_name)
  announce_operation "drop_table(#{table_name})"
  drop_cql =  "DROP TABLE #{table_name}"
  announce_suboperation drop_cql

  execute drop_cql
end