Class: CassandraMigrations::Migration::TableDefinition
- Inherits:
-
Object
- Object
- CassandraMigrations::Migration::TableDefinition
- 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
- #ascii(column_name, options = {}) ⇒ Object
- #binary(column_name, options = {}) ⇒ Object
- #boolean(column_name, options = {}) ⇒ Object
- #counter(column_name, options = {}) ⇒ Object
- #datetime(column_name, options = {}) ⇒ Object
- #decimal(column_name, options = {}) ⇒ Object
- #define_options(hash) ⇒ Object
- #define_partition_keys(*keys) ⇒ Object
- #define_primary_keys(*keys) ⇒ Object
- #double(column_name, options = {}) ⇒ Object
- #float(column_name, options = {}) ⇒ Object
-
#initialize ⇒ TableDefinition
constructor
A new instance of TableDefinition.
- #integer(column_name, options = {}) ⇒ Object
- #list(column_name, options = {}) ⇒ Object
- #map(column_name, options = {}) ⇒ Object
- #options ⇒ Object
- #set(column_name, options = {}) ⇒ Object
- #string(column_name, options = {}) ⇒ Object
- #text(column_name, options = {}) ⇒ Object
- #timestamp(column_name, options = {}) ⇒ Object
- #timeuuid(column_name, options = {}) ⇒ Object
- #to_add_column_cql ⇒ Object
- #to_create_cql ⇒ Object
- #uuid(column_name, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ TableDefinition
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
153 154 155 156 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 153 def ascii(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:ascii, ) define_primary_keys(column_name) if [:primary_key] end |
#binary(column_name, options = {}) ⇒ Object
178 179 180 181 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 178 def binary(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:binary, ) define_primary_keys(column_name) if [:primary_key] end |
#boolean(column_name, options = {}) ⇒ Object
117 118 119 120 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 117 def boolean(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:boolean, ) define_primary_keys(column_name) if [:primary_key] end |
#counter(column_name, options = {}) ⇒ Object
183 184 185 186 187 188 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 183 def counter(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:counter, ) if [:primary_key] raise Errors::MigrationDefinitionError, 'Counter columns cannot be primary keys' end end |
#datetime(column_name, options = {}) ⇒ Object
158 159 160 161 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 158 def datetime(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:datetime, ) define_primary_keys(column_name) if [:primary_key] end |
#decimal(column_name, options = {}) ⇒ Object
127 128 129 130 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 127 def decimal(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:decimal, ) define_primary_keys(column_name) if [:primary_key] end |
#define_options(hash) ⇒ Object
248 249 250 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 248 def (hash) = hash end |
#define_partition_keys(*keys) ⇒ Object
240 241 242 243 244 245 246 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 240 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
232 233 234 235 236 237 238 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 232 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
137 138 139 140 141 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 137 def double(column_name, ={}) [:limit] = 8 @columns_name_type_hash[column_name.to_sym] = column_type_for(:float, ) define_primary_keys(column_name) if [:primary_key] end |
#float(column_name, options = {}) ⇒ Object
132 133 134 135 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 132 def float(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:float, ) define_primary_keys(column_name) if [:primary_key] end |
#integer(column_name, options = {}) ⇒ Object
122 123 124 125 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 122 def integer(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:integer, ) define_primary_keys(column_name) if [:primary_key] end |
#list(column_name, options = {}) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 190 def list(column_name, ={}) type = [: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 [: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
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 216 def map(column_name, ={}) key_type, value_type = [:key_type], [: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 [: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 |
#options ⇒ Object
113 114 115 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 113 def ? " WITH %s" % (.map {|option| build_option(option)}.join(" AND ")) : '' end |
#set(column_name, options = {}) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 203 def set(column_name, ={}) type = [: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 [: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
143 144 145 146 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 143 def string(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:string, ) define_primary_keys(column_name) if [:primary_key] end |
#text(column_name, options = {}) ⇒ Object
148 149 150 151 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 148 def text(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:text, ) define_primary_keys(column_name) if [:primary_key] end |
#timestamp(column_name, options = {}) ⇒ Object
163 164 165 166 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 163 def (column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:timestamp, ) define_primary_keys(column_name) if [:primary_key] end |
#timeuuid(column_name, options = {}) ⇒ Object
173 174 175 176 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 173 def timeuuid(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:timeuuid, ) define_primary_keys(column_name) if [:primary_key] end |
#to_add_column_cql ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 99 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_cql ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# 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 if (@columns_name_type_hash.values.include? :counter) non_key_columns = @columns_name_type_hash.keys - @primary_keys counter_columns = [@columns_name_type_hash.select { |name, type| type == :counter }.first[0]] if (non_key_columns - counter_columns).present? raise Errors::MigrationDefinitionError, 'Non key fields not allowed in tables with counter' end 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
168 169 170 171 |
# File 'lib/cassandra_migrations/migration/table_definition.rb', line 168 def uuid(column_name, ={}) @columns_name_type_hash[column_name.to_sym] = column_type_for(:uuid, ) define_primary_keys(column_name) if [:primary_key] end |