Class: Cequel::Schema::TableUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/schema/table_updater.rb

Overview

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

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyspace, table_name) ⇒ TableUpdater

Returns a new instance of TableUpdater.

Parameters:

  • keyspace (Metal::Keyspace)

    keyspace containing the table

  • table_name (Symbol)

    name of the table to modify

Since:

  • 1.0.0



27
28
29
30
# File 'lib/cequel/schema/table_updater.rb', line 27

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.

Parameters:

  • keyspace (Metal::Keyspace)

    keyspace containing the table

  • table_name (Symbol)

    name of the table to modify

Yield Parameters:

  • updater (TableUpdater)

    instance of updater whose modifications will be applied to the named table

Since:

  • 1.0.0



18
19
20
# File 'lib/cequel/schema/table_updater.rb', line 18

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

Parameters:

  • name (Symbol)

    the name of the column

  • type (Symbol, Type)

    the type of the column

Since:

  • 1.0.0



51
52
53
# File 'lib/cequel/schema/table_updater.rb', line 51

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

Parameters:

  • name (Symbol)

    the name of the list

  • type (Symbol, Type)

    the type of the list elements

Since:

  • 1.0.0



62
63
64
# File 'lib/cequel/schema/table_updater.rb', line 62

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

Parameters:

  • name (Symbol)

    the name of the map

  • key_type (Symbol, Type)

    the type of the map’s keys

  • value_type (Symbol, Type)

    the type of the map’s values

Since:

  • 1.0.0



85
86
87
# File 'lib/cequel/schema/table_updater.rb', line 85

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

Parameters:

  • name (Symbol)

    the name of the set

  • type (Symbol, Type)

    the type of the set elements

Since:

  • 1.0.0



73
74
75
# File 'lib/cequel/schema/table_updater.rb', line 73

def add_set(name, type)
  add_data_column(Set.new(name, type(type)))
end

#applyvoid

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

Since:

  • 1.0.0



40
41
42
# File 'lib/cequel/schema/table_updater.rb', line 40

def apply
  statements.each { |statement| keyspace.execute(statement) }
end

#change_column(name, type) ⇒ void

Note:

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

Parameters:

  • name (Symbol)

    the name of the column

  • type (Symbol, Type)

    the new type of the column

Since:

  • 1.0.0



102
103
104
# File 'lib/cequel/schema/table_updater.rb', line 102

def change_column(name, type)
  add_stmt %Q|ALTER TABLE "#{table_name}" ALTER "#{name}" TYPE #{type}|
end

#change_properties(options) ⇒ void

This method returns an undefined value.

Change one or more table storage properties

Parameters:

  • options (Hash)

    map of property names to new values

See Also:

Since:

  • 1.0.0



125
126
127
128
129
# File 'lib/cequel/schema/table_updater.rb', line 125

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 = "#{table_name}_#{column_name}_idx") ⇒ void

This method returns an undefined value.

Create a secondary index

Parameters:

  • column_name (Symbol)

    name of the column to add an index on

  • index_name (Symbol) (defaults to: "#{table_name}_#{column_name}_idx")

    name of the index; will be inferred from convention if nil

Since:

  • 1.0.0



139
140
141
# File 'lib/cequel/schema/table_updater.rb', line 139

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_index(index_name) ⇒ void

This method returns an undefined value.

Remove a secondary index

Parameters:

  • index_name (Symbol)

    the name of the index to remove

Since:

  • 1.0.0



149
150
151
# File 'lib/cequel/schema/table_updater.rb', line 149

def drop_index(index_name)
  add_stmt %Q|DROP INDEX IF EXISTS "#{index_name}"|
end

#rename_column(old_name, new_name) ⇒ void

This method returns an undefined value.

Rename a column

Parameters:

  • old_name (Symbol)

    the current name of the column

  • new_name (Symbol)

    the new name of the column

Since:

  • 1.0.0



113
114
115
# File 'lib/cequel/schema/table_updater.rb', line 113

def rename_column(old_name, new_name)
  add_stmt %Q|ALTER TABLE "#{table_name}" RENAME "#{old_name}" TO "#{new_name}"|
end