Class: Cassanity::Migration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cassanity/migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrator) ⇒ Migration

Public: Get new instance of a migration.

migrator - The Cassanity::Migrator instance that is running the show.



16
17
18
# File 'lib/cassanity/migration.rb', line 16

def initialize(migrator)
  @migrator = migrator
end

Instance Attribute Details

#migratorObject (readonly)

Private: The migrator that is running the migration.



8
9
10
# File 'lib/cassanity/migration.rb', line 8

def migrator
  @migrator
end

Instance Method Details

#add_column(column_family_name, column_name, type) ⇒ Object Also known as: create_column

Public: Add a column to a column family.

column_family_name - The String or Symbol name of the column family. column_name - The String or Symbol name of the column to index. type - The String or Symbol CQL data type for the column.

Returns nothing.



58
59
60
# File 'lib/cassanity/migration.rb', line 58

def add_column(column_family_name, column_name, type)
  keyspace[column_family_name].alter(add: {column_name => type})
end

#add_index(column_family_name, column_name, options = {}) ⇒ Object Also known as: create_index

Public: Create an index on a column for a column family.

column_family_name - The String or Symbol name of the column family. column_name - The String or Symbol name of the column to index. options - The Hash of options.

:name - The String or Symbol name of the index
        (defaults to column_name).

Returns nothing.



93
94
95
96
97
98
# File 'lib/cassanity/migration.rb', line 93

def add_index(column_family_name, column_name, options = {})
  index_args = {column_name: column_name}
  index_args[:name] = options[:name] if options.key?(:name)

  keyspace[column_family_name].create_index(index_args)
end

#alter_column_family(column_family_name, args = {}) ⇒ Object Also known as: alter_table

Public: Alter a column family.

column_family_name - The String or Symbol name of the column family. args - The Hash of arguments. See ColumnFamily#alter for available args.

Returns nothing.



79
80
81
# File 'lib/cassanity/migration.rb', line 79

def alter_column_family(column_family_name, args = {})
  keyspace[column_family_name].alter(args)
end

#create_column_family(column_family_name, schema) ⇒ Object Also known as: add_column_family, create_table, add_table

Public: Create a column family.

column_family_name - The String or Symbol name of the column family. args - The Hash of arguments. See ColumnFamily#create for available args.

Returns nothing.



34
35
36
# File 'lib/cassanity/migration.rb', line 34

def create_column_family(column_family_name, schema)
  keyspace.column_family(column_family_name, schema: schema).create
end

#downObject

Public: Override in subclass.



25
26
# File 'lib/cassanity/migration.rb', line 25

def down
end

#drop_column(column_family_name, column_name) ⇒ Object

Public: Drop a column from a column family.

column_family_name - The String or Symbol name of the column family. column_name - The String or Symbol name of the column to index.

Returns nothing.



69
70
71
# File 'lib/cassanity/migration.rb', line 69

def drop_column(column_family_name, column_name)
  keyspace[column_family_name].alter(drop: column_name)
end

#drop_column_family(column_family_name) ⇒ Object Also known as: drop_table

Public: Drop a column family.

column_family_name - The String or Symbol name of the column family.

Returns nothing.



46
47
48
# File 'lib/cassanity/migration.rb', line 46

def drop_column_family(column_family_name)
  keyspace[column_family_name].drop
end

#drop_index(column_family_name, index_name) ⇒ Object

Public: Drop an index by name for a column family.

column_family_name - The String or Symbol name of the column family. name - The String or Symbol name of the index.

Returns nothing.



107
108
109
# File 'lib/cassanity/migration.rb', line 107

def drop_index(column_family_name, index_name)
  keyspace[column_family_name].drop_index(name: index_name)
end

#say(message) ⇒ Object

Public: Spit something to the log.



121
122
123
# File 'lib/cassanity/migration.rb', line 121

def say(message)
  @migrator.log "-- #{message}"
end

#say_with_time(message) ⇒ Object

Public: Spit something to the log with timing.



112
113
114
115
116
117
118
# File 'lib/cassanity/migration.rb', line 112

def say_with_time(message)
  say message
  start = Time.now
  result = yield
  duration = (Time.now - start).round(3)
  @migrator.log "   -> #{duration}s"
end

#upObject

Public: Override in subclass.



21
22
# File 'lib/cassanity/migration.rb', line 21

def up
end