Class: Cequel::Schema::TableUpdater
- Inherits:
-
Object
- Object
- Cequel::Schema::TableUpdater
- Defined in:
- lib/cequel/schema/table_updater.rb
Overview
Encapsulates a series of schema modification statements that can be applied to an existing table
Class Method Summary collapse
-
.apply(keyspace, table_name) {|updater| ... } ⇒ void
Construct a table updater and apply the schema modifications to the given table.
Instance Method Summary collapse
-
#add_column(name, type) ⇒ void
Add a column to the table.
-
#add_list(name, type) ⇒ void
Add a list to the table.
-
#add_map(name, key_type, value_type) ⇒ void
Add a map to the table.
-
#add_set(name, type) ⇒ void
Add a set to the table.
-
#apply ⇒ void
private
Apply the schema modifications to the table schema in the database.
-
#change_column(name, type) ⇒ void
Change an existing column’s type.
-
#change_properties(options) ⇒ void
Change one or more table storage properties.
-
#create_index(column_name, index_name = nil) ⇒ void
Create a secondary index.
-
#drop_index(index_name) ⇒ void
Remove a secondary index.
-
#initialize(keyspace, table_name) ⇒ TableUpdater
constructor
A new instance of TableUpdater.
-
#rename_column(old_name, new_name) ⇒ void
Rename a column.
Constructor Details
#initialize(keyspace, table_name) ⇒ TableUpdater
Returns a new instance of TableUpdater.
26 27 28 29 |
# File 'lib/cequel/schema/table_updater.rb', line 26 def initialize(keyspace, table_name) @keyspace, @table_name = keyspace, table_name @statements = [] end |
Class Method Details
.apply(keyspace, table_name) {|updater| ... } ⇒ void
This method returns an undefined value.
Construct a table updater and apply the schema modifications to the given table.
17 18 19 |
# File 'lib/cequel/schema/table_updater.rb', line 17 def self.apply(keyspace, table_name, &block) new(keyspace, table_name).tap(&block).apply end |
Instance Method Details
#add_column(name, type) ⇒ void
This method returns an undefined value.
Add a column to the table
50 51 52 |
# File 'lib/cequel/schema/table_updater.rb', line 50 def add_column(name, type) add_data_column(Column.new(name, type(type))) end |
#add_list(name, type) ⇒ void
This method returns an undefined value.
Add a list to the table
61 62 63 |
# 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) ⇒ void
This method returns an undefined value.
Add a map to the table
84 85 86 |
# 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) ⇒ void
This method returns an undefined value.
Add a set to the table
72 73 74 |
# File 'lib/cequel/schema/table_updater.rb', line 72 def add_set(name, type) add_data_column(Set.new(name, type(type))) end |
#apply ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Apply the schema modifications to the table schema in the database
39 40 41 |
# File 'lib/cequel/schema/table_updater.rb', line 39 def apply statements.each { |statement| keyspace.execute(statement) } end |
#change_column(name, type) ⇒ void
Changing the type of a CQL column does not modify the data currently stored in the column. Thus, client-side handling is needed to convert old values to the new type at read time. Cequel does not currently support this functionality, although it may in the future. Altering column types is not recommended.
This method returns an undefined value.
Change an existing column’s type
101 102 103 |
# File 'lib/cequel/schema/table_updater.rb', line 101 def change_column(name, type) alter_table("ALTER #{name} TYPE #{type(type).cql_name}") end |
#change_properties(options) ⇒ void
This method returns an undefined value.
Change one or more table storage properties
124 125 126 127 128 |
# File 'lib/cequel/schema/table_updater.rb', line 124 def change_properties() properties = .map { |name, value| TableProperty.build(name, value).to_cql } alter_table("WITH #{properties.join(' AND ')}") end |
#create_index(column_name, index_name = nil) ⇒ void
This method returns an undefined value.
Create a secondary index
138 139 140 141 142 |
# File 'lib/cequel/schema/table_updater.rb', line 138 def create_index(column_name, index_name = nil) index_name ||= "#{table_name}_#{column_name}_idx" statements << "CREATE INDEX #{index_name} ON #{table_name} (#{column_name})" end |
#drop_index(index_name) ⇒ void
This method returns an undefined value.
Remove a secondary index
150 151 152 |
# File 'lib/cequel/schema/table_updater.rb', line 150 def drop_index(index_name) statements << "DROP INDEX #{index_name}" end |
#rename_column(old_name, new_name) ⇒ void
This method returns an undefined value.
Rename a column
112 113 114 |
# File 'lib/cequel/schema/table_updater.rb', line 112 def rename_column(old_name, new_name) alter_table(%(RENAME "#{old_name}" TO "#{new_name}")) end |