cassandro 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b622b89e04dbec6f3f700f5c85fc395c010b2930
4
- data.tar.gz: c31e4c65fe75099e6cdf24ecbd571ce5d2435017
3
+ metadata.gz: 454e96452f040bc0fe75880ec95ab55da541ea75
4
+ data.tar.gz: c9d3980327a022d70e8524f53469c07810ea618b
5
5
  SHA512:
6
- metadata.gz: 0ee2cbec11f800c0557ebf54f095b7e9c0e03fc6593401e7023c55aa58d47682ed12e0a07a7e9216db940c334706013d3fe28da35bae8f137404cf4f33364b0b
7
- data.tar.gz: ee5966c8d96983de83c814ce06d2dd34b295a201afa814da887ee0f5584f10f47a67c64bf90796f6d0918ebb2ed4971e93c657fa0b34ee31bf4f750b88f96b09
6
+ metadata.gz: 2390fd1f15ba3d96885395bf5e207015c7779b2150872cd2060a93dfb450fe61e99f3318d8ad7c2cd2497b945a96d58b4b997354e1f32977dac073b88e9a53dc
7
+ data.tar.gz: 95e8324ad844c4f6fe2d298290cf8319f02fc9b49229b5e3ccbfc1b9e8d7b361f623c466ad7948395c49619926c0efd65ecc07b7dc15c01881d0937cabd99a50
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "cassandro"
5
- s.version = "2.0.1"
5
+ s.version = "2.1.0"
6
6
  s.summary = "Ruby ORM for Apache Cassandra"
7
7
  s.license = "MIT"
8
8
  s.description = "Lightweight Apache Cassandra ORM for Ruby"
@@ -51,9 +51,36 @@ end
51
51
 
52
52
  This will add an attribute `:delete` to your model. Next time you use `Model#destroy` your data will not be deleted from database but marked as deleted. You can then use `Model#restore` to unmark it.
53
53
 
54
- Data marked as deleted is not included within `Model#all` method by default. In order to include deleted records you have to send a boolean parameter to `all`.
54
+ Data marked as deleted is not included within find methods by default. In order to include deleted records you have to send a boolean parameter:
55
+
56
+ ### All
55
57
 
56
58
  ```ruby
57
59
  Person.all # => list all but deleted
58
60
  Person.all(true) # => list all, deleted included
59
61
  ```
62
+
63
+ ### Where
64
+
65
+ ```ruby
66
+ Person.where(:gender, "male") # => list "male" but deleted
67
+ Person.where(:gender, "male", true) # => list "male", deleted included
68
+ ```
69
+
70
+ ### Count
71
+
72
+ ```ruby
73
+ Person.count # => count all but deleted
74
+ Person.count(true) # => count all deleted included
75
+
76
+ # with filter
77
+ Person.count(:gender, "female") # => count all "female" but deleted
78
+ Person.count(:gender, "female", true) # => count all "female" deleted included
79
+ ```
80
+
81
+ ### Query
82
+
83
+ ```ruby
84
+ Person.query("gender = ? AND admin = ?", "male", true) # => list "male" "admins" but deleted
85
+ Person.query("gender = ? AND admin = ?", "male", true, true) # => list "male" "admins", deleted included
86
+ ```
@@ -14,6 +14,10 @@ Cassandro.connect(hosts: ['192.168.2.100', '192.168.2.101'], keyspace: 'some_key
14
14
 
15
15
  _For full list of options visit [Ruby Driver Documentation](http://datastax.github.io/ruby-driver/api/#cluster-class_method)_
16
16
 
17
+ ## Missing Connection
18
+
19
+ If the connection is not established, the `Cassandra::Errors::ClientError` will be raised when trying to interact with Cassandro.
20
+
17
21
  ## Keyspace
18
22
 
19
23
  ### Create Keyspace
@@ -44,6 +48,13 @@ Cassandro.use('keyspace_name')
44
48
  ```ruby
45
49
  result = Cassandro.execute("SELECT * FROM table_name;")
46
50
  ```
51
+
52
+ ### Execute prepared statements
53
+ ```ruby
54
+ st = Cassandro.prepare("SELECT * FROM table_name WHERE column1 = ?")
55
+ result = Cassandro.execute(st, arguments: ['value1'])
56
+ ```
57
+
47
58
  ### Create table
48
59
  ```ruby
49
60
  table = <<-TABLEDEF
@@ -24,7 +24,7 @@ module Cassandro
24
24
  end
25
25
 
26
26
  def self.use(keyspace)
27
- @@session.execute("USE #{keyspace}") if @@session
27
+ execute("USE #{keyspace}")
28
28
  end
29
29
 
30
30
  def self.disconnect
@@ -32,8 +32,22 @@ module Cassandro
32
32
  @@session = nil
33
33
  end
34
34
 
35
- def self.execute(cql_command)
36
- @@session.execute(cql_command)
35
+ def self.connected?
36
+ !@@session.nil?
37
+ end
38
+
39
+ def self.check_connection!
40
+ raise Cassandra::Errors::ClientError.new("Database connection is not established") unless connected?
41
+ end
42
+
43
+ def self.execute(statement, options = nil)
44
+ check_connection!
45
+ @@session.execute(statement, options)
46
+ end
47
+
48
+ def self.prepare(statement, options = nil)
49
+ check_connection!
50
+ @@session.prepare(statement, options)
37
51
  end
38
52
 
39
53
  def self.create_keyspace(name, options = { replication: { class: 'SimpleStrategy', replication_factor: 1}} )
@@ -51,11 +65,11 @@ module Cassandro
51
65
  #{with}
52
66
  KSDEF
53
67
 
54
- @@session.execute(keyspace_definition)
68
+ execute(keyspace_definition)
55
69
  end
56
70
 
57
71
  def self.truncate_table(table_name)
58
- @@session.execute("TRUNCATE #{table_name}")
72
+ execute("TRUNCATE #{table_name}")
59
73
  end
60
74
 
61
75
  def self.register_table(table_def)
@@ -63,10 +77,11 @@ module Cassandro
63
77
  end
64
78
 
65
79
  def self.load_tables
80
+ check_connection!
66
81
  @@tables.each do |table_definition|
67
82
  queries = table_definition.split(";").map(&:strip)
68
83
  queries.each do |query|
69
- @@session.execute(query) unless query.empty?
84
+ execute(query) unless query.empty?
70
85
  end
71
86
  end
72
87
  end
@@ -7,7 +7,7 @@ module Cassandro
7
7
  def down; end
8
8
 
9
9
  def execute(query)
10
- Cassandro.client.execute(query)
10
+ Cassandro.execute(query)
11
11
  end
12
12
 
13
13
  def self.version(version_number)
@@ -8,7 +8,7 @@ module Cassandro
8
8
  @migrations = Cassandro::Migration.migrations
9
9
  @logger = logger
10
10
 
11
- Cassandro.client.execute CassandroMigration.schema
11
+ Cassandro.execute CassandroMigration.schema
12
12
 
13
13
  version = CassandroMigration[name: 'version'] || CassandroMigration.create(name: 'version', value: "0")
14
14
 
@@ -8,6 +8,36 @@ module Cassandro
8
8
 
9
9
  results
10
10
  end
11
+
12
+ def where(key, value, with_deleted = false)
13
+ results = super(key, value)
14
+
15
+ results.reject!{ |r| r.deleted } unless with_deleted
16
+
17
+ results
18
+ end
19
+
20
+ def count(key = nil, value = nil, with_deleted = false)
21
+ return super(key, value) if with_deleted || key === true
22
+
23
+ if key && !value.nil?
24
+ results = where(key, value)
25
+ else
26
+ results = all
27
+ end
28
+
29
+ results.size
30
+ end
31
+
32
+ def query(where, *values)
33
+ with_deleted = where.scan(/\?/).length < values.length && values.pop === true
34
+
35
+ results = super(where, *values)
36
+
37
+ results.reject!{ |r| r.deleted } unless with_deleted
38
+
39
+ results
40
+ end
11
41
  end
12
42
 
13
43
  def self.included(model)
@@ -69,11 +69,12 @@ module Cassandro
69
69
  query += "WHERE #{p_keys.join(" AND ")}"
70
70
 
71
71
  begin
72
- st = Cassandro.client.prepare(query)
73
- Cassandro.client.execute(st, arguments: native_attributes(attrs))
72
+ st = Cassandro.prepare(query)
73
+ Cassandro.execute(st, arguments: native_attributes(attrs))
74
74
  @attributes.merge!(attrs)
75
75
  true
76
- rescue Exception => e
76
+ rescue => e
77
+ raise unless Cassandro.connected?
77
78
  @errors[:update_error] = e.message
78
79
  false
79
80
  end
@@ -101,10 +102,11 @@ module Cassandro
101
102
  st = self.statement_for(:insert, :insert_check => insert_check)
102
103
 
103
104
  begin
104
- r = Cassandro.client.execute(st, arguments: self.native_attributes)
105
+ r = Cassandro.execute(st, arguments: self.native_attributes)
105
106
  raise ModelException.new('not_applied') unless !insert_check || (insert_check && r.first["[applied]"])
106
107
  @persisted = true
107
108
  rescue => e
109
+ raise unless Cassandro.connected?
108
110
  @attributes[:id] = nil if !persisted? && @attributes.has_key?(:id)
109
111
  @errors[:save] = e.message
110
112
  false
@@ -187,8 +189,8 @@ module Cassandro
187
189
  WHERE #{where}
188
190
  QUERY
189
191
 
190
- st = Cassandro.client.prepare(query)
191
- result = Cassandro.client.execute(st, arguments: values)
192
+ st = Cassandro.prepare(query)
193
+ result = Cassandro.execute(st, arguments: values)
192
194
 
193
195
  return nil unless result.any?
194
196
 
@@ -229,8 +231,8 @@ module Cassandro
229
231
 
230
232
  query = "SELECT * FROM #{table_name} WHERE #{key} = ? ALLOW FILTERING"
231
233
 
232
- st = Cassandro.client.prepare(query)
233
- rows = Cassandro.client.execute(st, arguments: [value])
234
+ st = Cassandro.prepare(query)
235
+ rows = Cassandro.execute(st, arguments: [value])
234
236
 
235
237
  rows.each do |result|
236
238
  results << new(result, true)
@@ -245,10 +247,10 @@ module Cassandro
245
247
  if key && !value.nil?
246
248
  key = key.to_sym
247
249
  query << " WHERE #{key} = ? ALLOW FILTERING"
248
- st = Cassandro.client.prepare(query)
249
- results = Cassandro.client.execute(st, arguments: [value])
250
+ st = Cassandro.prepare(query)
251
+ results = Cassandro.execute(st, arguments: [value])
250
252
  else
251
- results = Cassandro.client.execute(query)
253
+ results = Cassandro.execute(query)
252
254
  end
253
255
 
254
256
  results.first["count"]
@@ -259,7 +261,8 @@ module Cassandro
259
261
  query = "TRUNCATE #{table_name}"
260
262
  st = Cassandro.execute(query)
261
263
  st.is_a? Cassandra::Client::VoidResult
262
- rescue e
264
+ rescue => e
265
+ raise unless Cassandro.connected?
263
266
  false
264
267
  end
265
268
  end
@@ -268,8 +271,8 @@ module Cassandro
268
271
  results = []
269
272
 
270
273
  query = "SELECT * FROM #{table_name} WHERE #{where} ALLOW FILTERING"
271
- st = Cassandro.client.prepare(query)
272
- rows = Cassandro.client.execute(st, arguments: values)
274
+ st = Cassandro.prepare(query)
275
+ rows = Cassandro.execute(st, arguments: values)
273
276
 
274
277
 
275
278
  rows.each do |result|
@@ -351,7 +354,7 @@ module Cassandro
351
354
  QUERY
352
355
  if @insert_statement.nil? ||
353
356
  @insert_statement.params_metadata.count != @attributes.count
354
- @insert_statement = Cassandro.client.prepare(query)
357
+ @insert_statement = Cassandro.prepare(query)
355
358
  else
356
359
  @insert_statement
357
360
  end
@@ -0,0 +1,123 @@
1
+ require_relative 'helper'
2
+
3
+ Protest.describe "Cassandro Connection check" do
4
+ setup do
5
+ class Admin < Cassandro::Model
6
+ table :admins
7
+ attribute :nickname, :text
8
+ attribute :age, :integer
9
+
10
+ primary_key :nickname
11
+ end
12
+
13
+ Cassandro.disconnect
14
+ end
15
+
16
+ context "Core" do
17
+ test "raise Exception on #execute" do
18
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
19
+ Cassandro.execute("USE test_keyspace")
20
+ end
21
+ end
22
+
23
+ test "raise Exception on #use" do
24
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
25
+ Cassandro.use("test_keyspace")
26
+ end
27
+ end
28
+
29
+ test "raise Exception on #create_keyspace" do
30
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
31
+ Cassandro.create_keyspace("test_keyspace")
32
+ end
33
+ end
34
+
35
+ test "raise Exception on #truncate_table" do
36
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
37
+ Cassandro.truncate_table("tests")
38
+ end
39
+ end
40
+ end
41
+
42
+ context "Model" do
43
+ test "raise Exception on #create" do
44
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
45
+ Admin.create(nickname: "tarolandia")
46
+ end
47
+ end
48
+
49
+ test "raise Exception on #save" do
50
+ admin = Admin.new(nickname: "tarolandia")
51
+
52
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
53
+ admin.save
54
+ end
55
+ end
56
+
57
+ test "raise Exception on #update_attributes" do
58
+ admin = Admin.new(nickname: "tarolandia")
59
+
60
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
61
+ admin.update_attributes(age: 29)
62
+ end
63
+ end
64
+
65
+ test "raise Exception on #[]" do
66
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
67
+ Admin[nickname: "tarolandia"]
68
+ end
69
+ end
70
+
71
+ test "raise Exception on #all" do
72
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
73
+ Admin.all
74
+ end
75
+ end
76
+
77
+ test "raise Exception on #where" do
78
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
79
+ Admin.where(:nickname, "tarolandia")
80
+ end
81
+ end
82
+
83
+ test "raise Exception on #count" do
84
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
85
+ Admin.count
86
+ end
87
+ end
88
+
89
+ test "raise Exception on #query" do
90
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
91
+ Admin.query("nickname = ?", "tarolandia")
92
+ end
93
+ end
94
+
95
+ test "raise Exception on #ttl" do
96
+ admin = Admin.new(nickname: "tarolandia")
97
+
98
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
99
+ admin.ttl
100
+ end
101
+ end
102
+
103
+ test "raise Exception on #create_with_ttl" do
104
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
105
+ Admin.create_with_ttl(20, nickname: "tarolandia", age: 29)
106
+ end
107
+ end
108
+
109
+ test "raise Exception on #destroy" do
110
+ admin = Admin.new(nickname: "tarolandia")
111
+
112
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
113
+ admin.destroy
114
+ end
115
+ end
116
+
117
+ test "raise Exception on #destroy_all" do
118
+ assert_raise(Cassandra::Errors::ClientError, "Database connection is not established") do
119
+ Admin.destroy_all
120
+ end
121
+ end
122
+ end
123
+ end
@@ -1,12 +1,12 @@
1
1
  require_relative 'helper'
2
2
 
3
3
  Protest.describe "Cassandro Migrator" do
4
- Cassandro.connect(hosts: ['127.0.0.1'], keyspace: 'cassandro_test')
5
4
  setup do
6
- #Cassandro::Migration.cleaassandra::Errors::InvalidError
7
- Cassandro.client.execute("DROP TABLE IF EXISTS users")
8
- Cassandro.client.execute("DROP TABLE IF EXISTS animals")
9
- Cassandro.client.execute("DROP TABLE IF EXISTS cassandro_migrations")
5
+ Cassandro.connect(hosts: ['127.0.0.1'], keyspace: 'cassandro_test')
6
+
7
+ Cassandro.execute("DROP TABLE IF EXISTS users")
8
+ Cassandro.execute("DROP TABLE IF EXISTS animals")
9
+ Cassandro.execute("DROP TABLE IF EXISTS cassandro_migrations")
10
10
  @migrator = Cassandro::Migrator.new("./test/support/migrations", Logger.new('/dev/null'))
11
11
  end
12
12
 
@@ -21,7 +21,6 @@ Protest.describe "Cassandro Model Soft Delete" do
21
21
  end
22
22
 
23
23
  test "all not include deleted" do
24
-
25
24
  me = Admin.create(:nickname => "k4nd4lf")
26
25
  other = Admin.create(:nickname => "Jim")
27
26
 
@@ -39,6 +38,78 @@ Protest.describe "Cassandro Model Soft Delete" do
39
38
  assert !Admin.all.include?(me)
40
39
 
41
40
  end
41
+
42
+ test "#where not includes deleted" do
43
+ me = Admin.create(:nickname => "tarolandia")
44
+ other = Admin.create(:nickname => "Jim")
45
+
46
+ me.destroy
47
+
48
+ assert_equal 0, Admin.where(:nickname, "tarolandia").size
49
+ end
50
+
51
+ test "#where should include deleted if asked" do
52
+ me = Admin.create(:nickname => "tarolandia")
53
+ other = Admin.create(:nickname => "Jim")
54
+
55
+ me.destroy
56
+
57
+ assert_equal 1, Admin.where(:nickname, "tarolandia", true).size
58
+ end
59
+
60
+ test "#count not includes deleted" do
61
+ me = Admin.create(:nickname => "tarolandia")
62
+ other = Admin.create(:nickname => "Jim")
63
+
64
+ me.destroy
65
+
66
+ assert_equal 1, Admin.count
67
+ end
68
+
69
+ test "#count should include deleted if asked" do
70
+ me = Admin.create(:nickname => "tarolandia")
71
+ other = Admin.create(:nickname => "Jim")
72
+
73
+ me.destroy
74
+
75
+ assert_equal 2, Admin.count(true)
76
+ end
77
+
78
+ test "#count should filter by key and exclude deleted" do
79
+ me = Admin.create(:nickname => "tarolandia")
80
+ other = Admin.create(:nickname => "Jim")
81
+
82
+ me.destroy
83
+
84
+ assert_equal 0, Admin.count(:nickname, "tarolandia")
85
+ end
86
+
87
+ test "#count should filter by key including deleted" do
88
+ me = Admin.create(:nickname => "tarolandia")
89
+ other = Admin.create(:nickname => "Jim")
90
+
91
+ me.destroy
92
+
93
+ assert_equal 1, Admin.count(:nickname, "tarolandia", true)
94
+ end
95
+
96
+ test "#query not includes deleted" do
97
+ me = Admin.create(:nickname => "tarolandia")
98
+ other = Admin.create(:nickname => "Jim")
99
+
100
+ me.destroy
101
+
102
+ assert_equal 0, Admin.query("nickname = ?", "tarolandia").size
103
+ end
104
+
105
+ test "#query should include deleted if asked" do
106
+ me = Admin.create(:nickname => "tarolandia")
107
+ other = Admin.create(:nickname => "Jim")
108
+
109
+ me.destroy
110
+
111
+ assert_equal 1, Admin.query("nickname = ?", "tarolandia", true).size
112
+ end
42
113
  end
43
114
 
44
115
 
@@ -3,6 +3,7 @@ require_relative 'helper'
3
3
  Protest.describe "Cassandro Module" do
4
4
  setup do
5
5
  SESSION.execute("DROP KEYSPACE IF EXISTS test_keyspace")
6
+ Cassandro.disconnect
6
7
  end
7
8
 
8
9
  test "connects to database" do
@@ -16,4 +17,3 @@ Protest.describe "Cassandro Module" do
16
17
  assert_equal Cassandra::Results::Void, Cassandro.use("test_keyspace").class
17
18
  end
18
19
  end
19
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassandro
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lautaro Orazi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-08 00:00:00.000000000 Z
12
+ date: 2015-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cassandra-driver
@@ -99,6 +99,7 @@ files:
99
99
  - lib/cassandro/model.rb
100
100
  - lib/cassandro/support/hash.rb
101
101
  - rakefile
102
+ - test/cassandro_check_connection_test.rb
102
103
  - test/cassandro_migration_test.rb
103
104
  - test/cassandro_migrator_test.rb
104
105
  - test/cassandro_model_test.rb