class Cequel::Schema::TableUpdater
Encapsulates a series of schema modification statements that can be applied to an existing table
Attributes
Public Class Methods
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
@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 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
@!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 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 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 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 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 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 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
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
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 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
# File lib/cequel/schema/table_updater.rb, line 155 def add_stmt(cql) statements << Cequel::Metal::Statement.new(cql) end
# File lib/cequel/schema/table_updater.rb, line 159 def type(type) ::Cequel::Type[type] end