Module: CassandraMigrations::Migration::ColumnOperations

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

Overview

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

  • adding/removing columns

  • changing column types

  • renaming columns

Instance Method Summary collapse

Instance Method Details

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

Adds a column to a table.

options: same options you would pass to create a table with that column (i.e. :limit might be applicable)



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

def add_column(table_name, column_name, type, options = {})
  table_definition = TableDefinition.new
  
  if !table_definition.respond_to?(type)
    raise Errors::MigrationDefinitionError("Type '#{type}' is not valid for cassandra migration.")
  end

  table_definition.send(type, column_name, options)

  announce_operation "add_column(#{column_name}, #{type})"

  cql =  "ALTER TABLE #{table_name} ADD "
  cql << table_definition.to_add_column_cql
  announce_suboperation cql
  
  execute cql
end

#remove_column(table_name, column_name) ⇒ Object

Removes a column from the table



38
39
40
41
42
43
44
45
# File 'lib/cassandra_migrations/migration/column_operations.rb', line 38

def remove_column(table_name, column_name)
  announce_operation "drop_table(#{table_name})"

  cql =  "ALTER TABLE #{table_name} DROP #{column_name}"
  announce_suboperation cql
  
  execute cql
end