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

C* Data Types. See www.datastax.com/documentation/cql/3.0/cql/cql_reference/cql_data_types_c.html


Migration | CQL Type | Ruby | Description Type | | Class |


string | varchar | String | UTF-8 encoded string text | text | String | UTF-8 encoded string ascii | ascii | String | US-ASCII character string


integer(4) | int | Integer | 32-bit signed integer integer(8) | bigint | Fixnum | 64-bit signed long varint | varint | Bignum | Arbitrary-precision integer


decimal | decimal | BigDecimal | Variable-precision decimal float(4) | float | | 32-bit IEEE-754 floating point double | double | | Float 64-bit IEEE-754 floating point float(8) | double | |


boolean | boolean | TrueClass | true or false

|           | FalseClass    |

uuid | uuid | Cql::Uuid | A UUID in standard UUID format timeuuid | timeuuid | Cql::TimeUuid | Type 1 UUID only (CQL 3)


inet | inet | IPAddr | IP address string in IPv4 or

|           |               | IPv6 format*

timestamp | timestamp | Time | Date plus time, encoded as 8

|           |               | bytes since epoch

datetime | timestamp | |


list | list | Array | A collection of one or more

|           |               | ordered elements

map | map | Hash | A JSON-style array of literals:

|           |               | { literal : literal, ... }

set | set | Set | A collection of one or more

|           |               | elements

binary | blob | | Arbitrary bytes (no validation),

|           |               | expressed as hexadecimal

| counter | | Distributed counter value

|           |               | (64-bit long)



60
61
62
63
64
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 60

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

Instance Method Details

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



128
129
130
131
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 128

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



153
154
155
156
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 153

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



92
93
94
95
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 92

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

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



158
159
160
161
162
163
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 158

def counter(column_name, options={})
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:counter, options)
  if options[:primary_key]
    raise Errors::MigrationDefinitionError, 'Counter columns cannot be primary keys'
  end
end

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



133
134
135
136
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 133

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



102
103
104
105
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 102

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_options(hash) ⇒ Object



205
206
207
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 205

def define_options(hash)
  @options = hash
end

#define_partition_keys(*keys) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 197

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



189
190
191
192
193
194
195
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 189

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



112
113
114
115
116
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 112

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



107
108
109
110
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 107

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



97
98
99
100
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 97

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



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

def list(column_name, options={})
  list_or_set(:list, column_name, options)
end

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 173

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

#optionsObject



88
89
90
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 88

def options
  @options ? " WITH %s" % (@options.map {|option| build_option(option)}.join(" AND ")) : ''
end

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



169
170
171
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 169

def set(column_name, options={})
  list_or_set(:set, column_name, options)
end

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



118
119
120
121
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 118

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



123
124
125
126
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 123

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



138
139
140
141
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 138

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



148
149
150
151
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 148

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



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 74

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



66
67
68
69
70
71
72
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 66

def to_create_cql
  cql = []
  build_name_type_cql(cql)
  check_for_non_key_fields_in_counter_table
  build_pk_clause(cql)
  cql.join(', ')
end

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



143
144
145
146
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 143

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