class Cequel::Schema::TableUpdater

Encapsulates a series of schema modification statements that can be applied to an existing table

Attributes

keyspace[R]
statements[R]
table_name[R]

Public Class Methods

apply(keyspace, table_name, &block) click to toggle source

Construct a table updater and apply the schema modifications to the given table.

@param (see initialize) @yieldparam updater [TableUpdater] instance of updater whose

modifications will be applied to the named table

@return [void]

# File lib/cequel/schema/table_updater.rb, line 17
def self.apply(keyspace, table_name, &block)
  new(keyspace, table_name).tap(&block).apply
end

Private Class Methods

new(keyspace, table_name) click to toggle source

@param keyspace [Metal::Keyspace] keyspace containing the table @param table_name [Symbol] name of the table to modify @private

# File lib/cequel/schema/table_updater.rb, line 26
def initialize(keyspace, table_name)
  @keyspace, @table_name = keyspace, table_name
  @statements = []
end

Public Instance Methods

add_column(name, type) click to toggle source

Add a column to the table

@param name [Symbol] the name of the column @param type [Symbol,Type] the type of the column @return [void]

# File lib/cequel/schema/table_updater.rb, line 50
def add_column(name, type)
  add_data_column(Column.new(name, type(type)))
end
add_data_column(column) click to toggle source

@!visibility protected

# File lib/cequel/schema/table_updater.rb, line 145
def add_data_column(column)
  add_stmt(%Q|ALTER TABLE #{table_name} ADD #{column.to_cql}|)
end
add_list(name, type) click to toggle source

Add a list to the table

@param name [Symbol] the name of the list @param type [Symbol,Type] the type of the list elements @return [void]

# File lib/cequel/schema/table_updater.rb, line 61
def add_list(name, type)
  add_data_column(List.new(name, type(type)))
end
add_map(name, key_type, value_type) click to toggle source

Add a map to the table

@param name [Symbol] the name of the map @param key_type [Symbol,Type] the type of the map's keys @param value_type [Symbol,Type] the type of the map's values @return [void]

# File lib/cequel/schema/table_updater.rb, line 84
def add_map(name, key_type, value_type)
  add_data_column(Map.new(name, type(key_type), type(value_type)))
end
add_set(name, type) click to toggle source

Add a set to the table

@param name [Symbol] the name of the set @param type [Symbol,Type] the type of the set elements @return [void]

# File lib/cequel/schema/table_updater.rb, line 72
def add_set(name, type)
  add_data_column(Set.new(name, type(type)))
end
apply() click to toggle source

Apply the schema modifications to the table schema in the database

@return [void]

@api private

# File lib/cequel/schema/table_updater.rb, line 39
def apply
  statements.each { |statement| keyspace.execute(statement) }
end
change_properties(options) click to toggle source

Change one or more table storage properties

@param options [Hash] map of property names to new values @return [void]

@see Table#add_property

# File lib/cequel/schema/table_updater.rb, line 116
def change_properties(options)
  properties = options
    .map { |name, value| TableProperty.build(name, value).to_cql }
  add_stmt %Q|ALTER TABLE "#{table_name}" WITH #{properties.join(' AND ')}|
end
create_index(column_name, index_name = " click to toggle source

Create a secondary index

@param column_name [Symbol] name of the column to add an index on @param index_name [Symbol] name of the index; will be inferred from

convention if nil

@return [void]

# File lib/cequel/schema/table_updater.rb, line 130
def create_index(column_name, index_name = "#{table_name}_#{column_name}_idx")
  add_stmt %Q|CREATE INDEX "#{index_name}" ON "#{table_name}" ("#{column_name}")|
end
drop_column(name) click to toggle source

Remove a column

@param name [Symbol] the name of the column to remove @return [void]

# File lib/cequel/schema/table_updater.rb, line 104
def drop_column(name)
  add_stmt %Q|ALTER TABLE "#{table_name}" DROP "#{name}"|
end
drop_index(index_name) click to toggle source

Remove a secondary index

@param index_name [Symbol] the name of the index to remove @return [void]

# File lib/cequel/schema/table_updater.rb, line 140
def drop_index(index_name)
  add_stmt %Q|DROP INDEX IF EXISTS "#{index_name}"|
end
rename_column(old_name, new_name) click to toggle source

Rename a column

@param old_name [Symbol] the current name of the column @param new_name [Symbol] the new name of the column @return [void]

# File lib/cequel/schema/table_updater.rb, line 95
def rename_column(old_name, new_name)
  add_stmt %Q|ALTER TABLE "#{table_name}" RENAME "#{old_name}" TO "#{new_name}"|
end

Private Instance Methods

add_stmt(cql) click to toggle source
# File lib/cequel/schema/table_updater.rb, line 155
def add_stmt(cql)
  statements << Cequel::Metal::Statement.new(cql)
end
type(type) click to toggle source
# File lib/cequel/schema/table_updater.rb, line 159
def type(type)
  ::Cequel::Type[type]
end