Class: CassandraMigrations::Migration::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_migrations/migration/table_definition.rb

Overview

Used to define a table in a migration of table creation or to add columns to an existing table.

An instance of this class is passed to the block of the method create_table, available on every migration.

This class is also internally used in the method add_column.

Instance Method Summary collapse

Constructor Details

#initializeTableDefinition

Returns a new instance of TableDefinition.



62
63
64
65
66
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 62

def initialize()
  @columns_name_type_hash = {}
  @primary_keys = []
  @partition_keys = []
end

Instance Method Details

#ascii(column_name, options = {}) ⇒ Object



141
142
143
144
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 141

def ascii(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:ascii, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#binary(column_name, options = {}) ⇒ Object



166
167
168
169
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 166

def binary(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:binary, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#boolean(column_name, options = {}) ⇒ Object



105
106
107
108
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 105

def boolean(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:boolean, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#datetime(column_name, options = {}) ⇒ Object



146
147
148
149
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 146

def datetime(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:datetime, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#decimal(column_name, options = {}) ⇒ Object



115
116
117
118
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 115

def decimal(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:decimal, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#define_partition_keys(*keys) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 221

def define_partition_keys(*keys)
  if !@partition_keys.empty?
    raise Errors::MigrationDefinitionError, 'Partition key defined twice for the same table.'
  end

  @partition_keys = keys.flatten
end

#define_primary_keys(*keys) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 213

def define_primary_keys(*keys)
  if !@primary_keys.empty?
    raise Errors::MigrationDefinitionError, 'Primary key defined twice for the same table.'
  end

  @primary_keys = keys.flatten
end

#double(column_name, options = {}) ⇒ Object



125
126
127
128
129
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 125

def double(column_name, options={})
  options[:limit] = 8
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:float, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#float(column_name, options = {}) ⇒ Object



120
121
122
123
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 120

def float(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:float, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#integer(column_name, options = {}) ⇒ Object



110
111
112
113
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 110

def integer(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:integer, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#list(column_name, options = {}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 171

def list(column_name, options={})
  type = options[:type]
  if type.nil?
    raise Errors::MigrationDefinitionError, 'A list must define a collection type.'
  elsif !self.respond_to?(type)
    raise Errors::MigrationDefinitionError, "Type '#{type}' is not valid for cassandra migration."
  end
  if options[:primary_key]
    raise Errors::MigrationDefinitionError, 'A collection cannot be used as a primary key.'
  end
  @columns_name_type_hash[column_name.to_sym] = :"list<#{column_type_for(type)}>"
end

#map(column_name, options = {}) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 197

def map(column_name, options={})
  key_type, value_type = options[:key_type], options[:value_type]
  [key_type, value_type].each_with_index do |type, index|
    if type.nil?
      raise Errors::MigrationDefinitionError, "A map must define a #{index = 0 ? 'key' : 'value'} type."
    elsif !self.respond_to?(type)
      raise Errors::MigrationDefinitionError, "Type '#{type}' is not valid for cassandra migration."
    end
  end

  if options[:primary_key]
    raise Errors::MigrationDefinitionError, 'A collection cannot be used as a primary key.'
  end
  @columns_name_type_hash[column_name.to_sym] = :"map<#{column_type_for(key_type)},#{column_type_for(value_type)}>"
end

#set(column_name, options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 184

def set(column_name, options={})
  type = options[:type]
  if type.nil?
    raise Errors::MigrationDefinitionError, 'A set must define a collection type.'
  elsif !self.respond_to?(type)
    raise Errors::MigrationDefinitionError, "Type '#{type}' is not valid for cassandra migration."
  end
  if options[:primary_key]
    raise Errors::MigrationDefinitionError, 'A collection cannot be used as a primary key.'
  end
  @columns_name_type_hash[column_name.to_sym] = :"set<#{column_type_for(type)}>"
end

#string(column_name, options = {}) ⇒ Object



131
132
133
134
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 131

def string(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:string, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#text(column_name, options = {}) ⇒ Object



136
137
138
139
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 136

def text(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:text, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#timestamp(column_name, options = {}) ⇒ Object



151
152
153
154
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 151

def timestamp(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:timestamp, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#timeuuid(column_name, options = {}) ⇒ Object



161
162
163
164
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 161

def timeuuid(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:timeuuid, options)
  define_primary_keys(column_name) if options[:primary_key]
end

#to_add_column_cqlObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 91

def to_add_column_cql
  cql = ""

  if @columns_name_type_hash.size == 1
    cql = "#{@columns_name_type_hash.keys.first} #{@columns_name_type_hash.values.first}"
  elsif @columns_name_type_hash.empty?
    raise Errors::MigrationDefinitionError, 'No column to add.'
  else
    raise Errors::MigrationDefinitionError, 'Only one column can be added at once.'
  end

  cql
end

#to_create_cqlObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 68

def to_create_cql
  cql = []

  if !@columns_name_type_hash.empty?
    @columns_name_type_hash.each do |column_name, type|
      cql << "#{column_name} #{type}"
    end
  else
    raise Errors::MigrationDefinitionError, 'No columns defined for table.'
  end

  key_info = (@primary_keys - @partition_keys)
  key_info = ["(#{@partition_keys.join(', ')})", *key_info] if @partition_keys.any?

  if key_info.any?
    cql << "PRIMARY KEY(#{key_info.join(', ')})"
  else
    raise Errors::MigrationDefinitionError, 'No primary key defined.'
  end

  cql.join(', ')
end

#uuid(column_name, options = {}) ⇒ Object



156
157
158
159
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 156

def uuid(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:uuid, options)
  define_primary_keys(column_name) if options[:primary_key]
end