cassandra_migrations 0.0.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77de13dc274012a0854ed3626d60f4a0ac73af3d
4
- data.tar.gz: 4a17b0acf23ebaae56d88e4ba1572342fc248b03
3
+ metadata.gz: 937a04d1bf915cd899134c271aa645421259fb3b
4
+ data.tar.gz: c0916a9b1fb7be0b142ad0515a3fca628953fe9c
5
5
  SHA512:
6
- metadata.gz: aab3f0ddd91d81d590cbe6ad16dcbbff317ca9358f5ef924d099867316f0c145db9f53dcc67db658c077184b23a783a89047f3dc76094571b858e42bdba0526a
7
- data.tar.gz: 20ab748bf68a98bcbaf5524b1cc5ef7d76ccafff0735559e5c83747b459f0f9ffdd50931b64489265187f00484d5686755d1a8c6f23e3444cbce730794cb8854
6
+ metadata.gz: 557970e72c79f1b37035948c3f4990b99afd4a92c0e794d9251a252b9143a7f7a67be3767ef5fdbc4f204a990427d294d2eb351330561eae85935c445a8a571c
7
+ data.tar.gz: 38be1b8793e029e2d912699421c572906f60ceca0939b69951b4553e4b2f34fa5a0f51d9d570cfe95bf333add26447cb798afd1bab58c1f27f7730a4c04557dd
@@ -4,27 +4,29 @@ module CassandraMigrations
4
4
  module Cassandra
5
5
  module KeyspaceOperations
6
6
 
7
- def create_keyspace!
7
+ def create_keyspace!(env)
8
+ config = Config.configurations[env]
8
9
  begin
9
10
  execute(
10
- "CREATE KEYSPACE #{Config.keyspace} \
11
+ "CREATE KEYSPACE #{config.keyspace} \
11
12
  WITH replication = { \
12
- 'class':'#{Config.replication['class']}', \
13
- 'replication_factor': #{Config.replication['replication_factor']} \
13
+ 'class':'#{config.replication['class']}', \
14
+ 'replication_factor': #{config.replication['replication_factor']} \
14
15
  }"
15
16
  )
16
- use(Config.keyspace)
17
+ use(config.keyspace)
17
18
  rescue Exception => exception
18
- drop_keyspace!
19
+ drop_keyspace!(env)
19
20
  raise exception
20
21
  end
21
22
  end
22
23
 
23
- def drop_keyspace!
24
+ def drop_keyspace!(env)
25
+ config = Config.configurations[env]
24
26
  begin
25
- execute("DROP KEYSPACE #{Config.keyspace}")
27
+ execute("DROP KEYSPACE #{config.keyspace}")
26
28
  rescue Cql::QueryError
27
- raise Errors::UnexistingKeyspaceError, Config.keyspace
29
+ raise Errors::UnexistingKeyspaceError, config.keyspace
28
30
  end
29
31
  end
30
32
 
@@ -38,7 +38,13 @@ module CassandraMigrations
38
38
  client.close if client.connected?
39
39
  self.client = nil
40
40
  end
41
-
41
+
42
+ def self.using_keyspace(keyspace, &block)
43
+ use(keyspace)
44
+ block.call
45
+ use(Config.keyspace)
46
+ end
47
+
42
48
  def self.use(keyspace)
43
49
  connect_to_server unless client
44
50
 
@@ -2,23 +2,32 @@
2
2
 
3
3
  module CassandraMigrations
4
4
  module Config
5
-
6
- mattr_accessor :config
7
-
5
+
6
+ FIELDS = %w(host port keyspace replication)
7
+
8
+ Configuration = Struct.new(*FIELDS.map(&:to_sym))
9
+
10
+ mattr_writer :configurations
11
+
12
+ def self.configurations
13
+ @configurations || load_config
14
+ end
15
+
8
16
  def self.method_missing(method_sym, *arguments, &block)
9
- load_config unless config
10
- config[method_sym.to_s]
17
+ load_config unless configurations
18
+ self.configurations[Rails.env].send(method_sym)
11
19
  end
12
20
 
13
21
  private
14
22
 
15
23
  def self.load_config
16
24
  begin
17
- self.config = YAML.load(ERB.new(File.new(Rails.root.join("config", "cassandra.yml")).read).result)[Rails.env]
18
-
19
- if config.nil?
25
+ configs = YAML.load(ERB.new(File.new(Rails.root.join("config", "cassandra.yml")).read).result)
26
+ configurations = Hash[configs.map {|env, config| [env, Configuration.new(*(FIELDS.map {|k| config[k]}))]}]
27
+ if configurations[Rails.env].nil?
20
28
  raise Errors::MissingConfigurationError, "No configuration for #{Rails.env} environment! Complete your config/cassandra.yml."
21
29
  end
30
+ configurations
22
31
  rescue Errno::ENOENT
23
32
  raise Errors::MissingConfigurationError
24
33
  end
@@ -76,6 +76,14 @@ module CassandraMigrations
76
76
  raise Errors::MigrationDefinitionError, 'No columns defined for table.'
77
77
  end
78
78
 
79
+ if (@columns_name_type_hash.values.include? :counter)
80
+ non_key_columns = @columns_name_type_hash.keys - @primary_keys
81
+ counter_columns = [@columns_name_type_hash.select { |name, type| type == :counter }.first[0]]
82
+ if (non_key_columns - counter_columns).present?
83
+ raise Errors::MigrationDefinitionError, 'Non key fields not allowed in tables with counter'
84
+ end
85
+ end
86
+
79
87
  key_info = (@primary_keys - @partition_keys)
80
88
  key_info = ["(#{@partition_keys.join(', ')})", *key_info] if @partition_keys.any?
81
89
 
@@ -102,6 +110,10 @@ module CassandraMigrations
102
110
  cql
103
111
  end
104
112
 
113
+ def options
114
+ @options ? " WITH %s" % (@options.map {|option| build_option(option)}.join(" AND ")) : ''
115
+ end
116
+
105
117
  def boolean(column_name, options={})
106
118
  @columns_name_type_hash[column_name.to_sym] = column_type_for(:boolean, options)
107
119
  define_primary_keys(column_name) if options[:primary_key]
@@ -168,6 +180,13 @@ module CassandraMigrations
168
180
  define_primary_keys(column_name) if options[:primary_key]
169
181
  end
170
182
 
183
+ def counter(column_name, options={})
184
+ @columns_name_type_hash[column_name.to_sym] = column_type_for(:counter, options)
185
+ if options[:primary_key]
186
+ raise Errors::MigrationDefinitionError, 'Counter columns cannot be primary keys'
187
+ end
188
+ end
189
+
171
190
  def list(column_name, options={})
172
191
  type = options[:type]
173
192
  if type.nil?
@@ -226,11 +245,15 @@ module CassandraMigrations
226
245
  @partition_keys = keys.flatten
227
246
  end
228
247
 
248
+ def define_options(hash)
249
+ @options = hash
250
+ end
251
+
229
252
  private
230
253
 
231
254
  PASSTHROUGH_TYPES = [:text, :ascii, :decimal, :double, :boolean,
232
255
  :uuid, :timeuuid, :inet, :timestamp, :list,
233
- :map, :set]
256
+ :map, :set, :counter]
234
257
  TYPES_MAP = { string: :varchar,
235
258
  datetime: :timestamp,
236
259
  binary: :blob }
@@ -240,6 +263,11 @@ module CassandraMigrations
240
263
  float: { 4 => :float, 8 => :double, nil => :float }
241
264
  }
242
265
 
266
+ SPECIAL_OPTIONS_MAP = {
267
+ compact_storage: 'COMPACT STORAGE',
268
+ clustering_order: 'CLUSTERING ORDER'
269
+ }
270
+
243
271
  def column_type_for(type, options={})
244
272
  cql_type = type if PASSTHROUGH_TYPES.include?(type)
245
273
  cql_type ||= TYPES_MAP[type]
@@ -253,6 +281,19 @@ module CassandraMigrations
253
281
  cql_type
254
282
  end
255
283
 
284
+ def build_option(option)
285
+ name, value = option
286
+ cql_name = SPECIAL_OPTIONS_MAP.fetch(name, name.to_s)
287
+ case name
288
+ when :clustering_order
289
+ "#{cql_name} BY (#{value})"
290
+ when :compact_storage
291
+ cql_name
292
+ else
293
+ "#{cql_name} = #{value}"
294
+ end
295
+ end
296
+
256
297
  end
257
298
  end
258
299
  end
@@ -20,6 +20,7 @@ module CassandraMigrations
20
20
  table_definition = TableDefinition.new
21
21
  table_definition.define_primary_keys(options[:primary_keys]) if options[:primary_keys]
22
22
  table_definition.define_partition_keys(options[:partition_keys]) if options[:partition_keys]
23
+ table_definition.define_options(options[:options]) if options[:options]
23
24
 
24
25
  yield table_definition if block_given?
25
26
 
@@ -28,6 +29,7 @@ module CassandraMigrations
28
29
  create_cql = "CREATE TABLE #{table_name} ("
29
30
  create_cql << table_definition.to_create_cql
30
31
  create_cql << ")"
32
+ create_cql << table_definition.options
31
33
 
32
34
  announce_suboperation create_cql
33
35
 
@@ -63,6 +65,14 @@ module CassandraMigrations
63
65
  execute drop_index_cql
64
66
  end
65
67
 
68
+ def add_options(table_name, options)
69
+ announce_operation "add_options_#{table_name}"
70
+ add_options_cql = "ALTER TABLE #{properties_cql(options)}"
71
+ announce_suboperation drop_index_cql
72
+
73
+ execute add_options_cql
74
+ end
75
+
66
76
  end
67
77
  end
68
78
  end
@@ -13,6 +13,7 @@ module CassandraMigrations
13
13
 
14
14
  # Makes +execute+ method directly available to migrations
15
15
  delegate :execute, :to => Cassandra
16
+ delegate :using_keyspace, :to => Cassandra
16
17
 
17
18
  # Makes +up+ work if the method in the migration is defined with self.up
18
19
  def up
@@ -12,7 +12,7 @@ namespace :cassandra do
12
12
  CassandraMigrations::Cassandra.start!
13
13
  puts "Keyspace #{CassandraMigrations::Config.keyspace} already exists!"
14
14
  rescue CassandraMigrations::Errors::UnexistingKeyspaceError
15
- CassandraMigrations::Cassandra.create_keyspace!
15
+ CassandraMigrations::Cassandra.create_keyspace!(Rails.env)
16
16
  puts "Created keyspace #{CassandraMigrations::Config.keyspace}"
17
17
  end
18
18
  end
@@ -20,7 +20,7 @@ namespace :cassandra do
20
20
  desc 'Drop keyspace in config/cassandra.yml for the current environment'
21
21
  task :drop do
22
22
  begin
23
- CassandraMigrations::Cassandra.drop_keyspace!
23
+ CassandraMigrations::Cassandra.drop_keyspace!(Rails.env)
24
24
  puts "Dropped keyspace #{CassandraMigrations::Config.keyspace}"
25
25
  rescue CassandraMigrations::Errors::UnexistingKeyspaceError
26
26
  puts "Keyspace #{CassandraMigrations::Config.keyspace} does not exist... cannot be dropped"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassandra_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique Gubert
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-25 00:00:00.000000000 Z
12
+ date: 2014-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cql-rb
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - '='
19
19
  - !ruby/object:Gem::Version
20
- version: 2.0.0.pre1
20
+ version: 2.0.0
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - '='
26
26
  - !ruby/object:Gem::Version
27
- version: 2.0.0.pre1
27
+ version: 2.0.0
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -59,28 +59,28 @@ dependencies:
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '0.5'
62
+ version: 0.7.3
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '0.5'
69
+ version: 0.7.3
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rspec
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: '2.14'
76
+ version: '2.99'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: '2.14'
83
+ version: '2.99'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: debugger
86
86
  requirement: !ruby/object:Gem::Requirement
@@ -138,7 +138,7 @@ dependencies:
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0.7'
140
140
  description: A gem to manage Cassandra database schema for Rails. This gem offers
141
- migrations and environment sific databases out-of-the-box for Rails users.
141
+ migrations and environment specific databases out-of-the-box for Rails users.
142
142
  email:
143
143
  - guberthenrique@hotmail.com
144
144
  - bsbodden@integrallis.com