gotime-cassandra_object 4.2.2 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '4.2.2'
5
+ s.version = '4.3.0'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -16,6 +16,16 @@ module CassandraCQL
16
16
  end
17
17
 
18
18
  module CassandraObject
19
+ class Config
20
+ attr_accessor :servers, :keyspace, :thrift_options
21
+
22
+ def initialize(options)
23
+ self.servers = Array.wrap(options[:servers] || "127.0.0.1:9160")
24
+ self.keyspace = options[:keyspace]
25
+ self.thrift_options = (options[:thrift] || {}).symbolize_keys
26
+ end
27
+ end
28
+
19
29
  module Connection
20
30
  extend ActiveSupport::Concern
21
31
 
@@ -24,17 +34,12 @@ module CassandraObject
24
34
  end
25
35
 
26
36
  module ClassMethods
27
- DEFAULT_OPTIONS = {
28
- servers: "127.0.0.1:9160",
29
- thrift: {}
30
- }
31
-
32
37
  def establish_connection(spec)
33
- self.connection_config = spec.reverse_merge(DEFAULT_OPTIONS)
38
+ self.connection_config = Config.new(spec)
34
39
  end
35
40
 
36
41
  def cql
37
- @@cql ||= CassandraCQL::Database.new(connection_config[:servers], {keyspace: connection_config[:keyspace]}, connection_config[:thrift].symbolize_keys)
42
+ @@cql ||= CassandraCQL::Database.new(connection_config.servers, {keyspace: connection_config.keyspace}, connection_config.thrift_options)
38
43
  end
39
44
 
40
45
  def execute_cql(cql_string, *bind_vars)
@@ -1,5 +1,9 @@
1
+ require 'cassandra_object/schema/tasks'
2
+
1
3
  module CassandraObject
2
4
  class Schema
5
+ extend Tasks
6
+
3
7
  class << self
4
8
  def create_keyspace(keyspace)
5
9
  system_execute "CREATE KEYSPACE #{keyspace} " +
@@ -21,7 +25,6 @@ module CassandraObject
21
25
  end
22
26
 
23
27
  def add_index()
24
-
25
28
  end
26
29
 
27
30
  private
@@ -30,7 +33,7 @@ module CassandraObject
30
33
  end
31
34
 
32
35
  def system_execute(cql)
33
- @system_cql ||= CassandraCQL::Database.new(CassandraObject::Base.connection_config[:servers], keyspace: 'system')
36
+ @system_cql ||= CassandraCQL::Database.new(CassandraObject::Base.connection_config.servers, keyspace: 'system')
34
37
  @system_cql.execute cql
35
38
  end
36
39
  end
@@ -0,0 +1,33 @@
1
+ module CassandraObject
2
+ class Schema
3
+ module Tasks
4
+ def dump(io)
5
+ column_families.each do |column_family|
6
+ io.puts run_command("DESCRIBE COLUMNFAMILY #{column_family}")
7
+ io.puts
8
+ end
9
+ end
10
+
11
+ def load(filename)
12
+ `cqlsh -k #{keyspace} -f #{filename} #{server}`
13
+ end
14
+
15
+ def column_families
16
+ run_command('DESCRIBE COLUMNFAMILIES').split.sort
17
+ end
18
+
19
+ private
20
+ def run_command(command)
21
+ `echo "#{command};" | cqlsh -k #{keyspace} #{server}`.sub(/^(.*)$/, '').strip
22
+ end
23
+
24
+ def keyspace
25
+ CassandraObject::Base.connection_config.keyspace
26
+ end
27
+
28
+ def server
29
+ CassandraObject::Base.connection_config.servers.first.gsub(/:.*/, '')
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,4 +1,4 @@
1
- namespace :ks do
1
+ ks_namespace = namespace :ks do
2
2
  desc 'Create the keyspace in cassandra_config/cassandra.yml for the current environment'
3
3
  task create: :environment do
4
4
  CassandraObject::Schema.create_keyspace cassandra_config['keyspace']
@@ -8,6 +8,26 @@ namespace :ks do
8
8
  CassandraObject::Schema.drop_keyspace cassandra_config['keyspace']
9
9
  end
10
10
 
11
+ task reset: [:drop, :setup]
12
+
13
+ task setup: :create do
14
+ filename = ENV['SCHEMA'] || "#{Rails.root}/ks/structure.cql"
15
+ CassandraObject::Schema.load(filename)
16
+ end
17
+
18
+ namespace :structure do
19
+ task dump: :environment do
20
+ filename = ENV['SCHEMA'] || "#{Rails.root}/ks/structure.cql"
21
+ File.open(filename, "w:utf-8") do |file|
22
+ CassandraObject::Schema.dump(file)
23
+ end
24
+ end
25
+ end
26
+
27
+ task :_dump do
28
+ ks_namespace["structure:dump"].invoke
29
+ end
30
+
11
31
  private
12
32
  def cassandra_config
13
33
  @cassandra_config ||= begin
@@ -15,16 +15,16 @@ class CassandraObject::ConnectionTest < CassandraObject::TestCase
15
15
  thrift: {'timeout' => 10}
16
16
  )
17
17
  #
18
- assert_equal 'place_directory_development', TestObject.connection_config[:keyspace]
19
- assert_equal 10, TestObject.connection_config[:thrift]['timeout']
18
+ assert_equal ['192.168.0.100:9160'], TestObject.connection_config.servers
19
+ assert_equal 'place_directory_development', TestObject.connection_config.keyspace
20
+ assert_equal 10, TestObject.connection_config.thrift_options[:timeout]
20
21
  end
21
22
 
22
23
  test 'establish_connection defaults' do
23
24
  TestObject.establish_connection(
24
25
  keyspace: 'place_directory_development'
25
26
  )
26
- #
27
- # assert_equal 'place_directory_development', TestObject.connection.keyspace
28
- assert_equal "127.0.0.1:9160", TestObject.connection_config[:servers]
27
+
28
+ assert_equal ["127.0.0.1:9160"], TestObject.connection_config.servers
29
29
  end
30
30
  end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::Schema::TasksTest < CassandraObject::TestCase
4
+ test "column_families" do
5
+ assert_equal ['Issues'], CassandraObject::Schema.column_families
6
+ end
7
+
8
+ test "dump" do
9
+ io = StringIO.new
10
+
11
+ CassandraObject::Schema.dump(io)
12
+ io.rewind
13
+
14
+ assert_match /Issues/, io.read
15
+ end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gotime-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.2
4
+ version: 4.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -112,6 +112,7 @@ files:
112
112
  - lib/cassandra_object/railtie.rb
113
113
  - lib/cassandra_object/savepoints.rb
114
114
  - lib/cassandra_object/schema.rb
115
+ - lib/cassandra_object/schema/tasks.rb
115
116
  - lib/cassandra_object/scope.rb
116
117
  - lib/cassandra_object/scope/batches.rb
117
118
  - lib/cassandra_object/scope/finder_methods.rb
@@ -154,6 +155,7 @@ files:
154
155
  - test/unit/log_subscriber_test.rb
155
156
  - test/unit/persistence_test.rb
156
157
  - test/unit/savepoints_test.rb
158
+ - test/unit/schema/tasks_test.rb
157
159
  - test/unit/scope/batches_test.rb
158
160
  - test/unit/scope/finder_methods_test.rb
159
161
  - test/unit/scope/query_methods_test.rb