cassandra_migrations 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -4,23 +4,39 @@ module CassandraMigrations
|
|
4
4
|
module Cassandra
|
5
5
|
module Queries
|
6
6
|
|
7
|
-
def write!(table,
|
7
|
+
def write!(table, value_hash, options={})
|
8
8
|
columns = []
|
9
9
|
values = []
|
10
10
|
|
11
|
-
|
12
|
-
columns <<
|
13
|
-
|
14
|
-
if v.respond_to?(:strftime)
|
15
|
-
values << "'#{v.strftime('%Y-%m-%d %H:%M:%S%z')}'"
|
16
|
-
elsif v.is_a?(String)
|
17
|
-
values << "'#{v}'"
|
18
|
-
else
|
19
|
-
values << v.to_s
|
20
|
-
end
|
11
|
+
value_hash.each do |column, value|
|
12
|
+
columns << column.to_s
|
13
|
+
values << to_cql_value(value)
|
21
14
|
end
|
22
15
|
|
23
|
-
|
16
|
+
query = "INSERT INTO #{table} (#{columns.join(', ')}) VALUES (#{values.join(', ')})"
|
17
|
+
|
18
|
+
if options[:ttl]
|
19
|
+
query += " USING TTL #{options[:ttl]}"
|
20
|
+
end
|
21
|
+
|
22
|
+
execute(query)
|
23
|
+
end
|
24
|
+
|
25
|
+
def update!(table, selection, value_hash, options={})
|
26
|
+
set_terms = []
|
27
|
+
value_hash.each do |column, value|
|
28
|
+
set_terms << "#{column} = #{to_cql_value(value)}"
|
29
|
+
end
|
30
|
+
|
31
|
+
query = "UPDATE #{table}"
|
32
|
+
|
33
|
+
if options[:ttl]
|
34
|
+
query += " USING TTL #{options[:ttl]}"
|
35
|
+
end
|
36
|
+
|
37
|
+
query += " SET #{set_terms.join(', ')} WHERE #{selection}"
|
38
|
+
|
39
|
+
execute(query)
|
24
40
|
end
|
25
41
|
|
26
42
|
def select(table, options={})
|
@@ -42,13 +58,25 @@ module CassandraMigrations
|
|
42
58
|
end
|
43
59
|
|
44
60
|
def delete!(table, selection, options={})
|
45
|
-
|
61
|
+
options[:projection] = options[:projection].to_s + ' ' if options[:projection]
|
62
|
+
execute("DELETE #{options[:projection]}FROM #{table} WHERE #{selection}")
|
46
63
|
end
|
47
64
|
|
48
65
|
def truncate!(table)
|
49
66
|
execute("TRUNCATE #{table}")
|
50
67
|
end
|
51
68
|
|
69
|
+
private
|
70
|
+
|
71
|
+
def to_cql_value(value)
|
72
|
+
if value.respond_to?(:strftime)
|
73
|
+
"'#{value.strftime('%Y-%m-%d %H:%M:%S%z')}'"
|
74
|
+
elsif value.is_a?(String)
|
75
|
+
"'#{value}'"
|
76
|
+
else
|
77
|
+
value.to_s
|
78
|
+
end
|
79
|
+
end
|
52
80
|
end
|
53
81
|
end
|
54
82
|
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# encoding : utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe CassandraMigrations::Cassandra::Queries do
|
5
|
+
|
6
|
+
class TestQueryExecutor
|
7
|
+
extend CassandraMigrations::Cassandra::Queries
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '.write!' do
|
11
|
+
it 'should insert a record into the specified table' do
|
12
|
+
TestQueryExecutor.should_receive(:execute).with(
|
13
|
+
"INSERT INTO people (name, age, height_in_meters, birth_time) VALUES ('John', 24, 1.83, '1989-05-27 08:50:25+0000')"
|
14
|
+
)
|
15
|
+
|
16
|
+
TestQueryExecutor.write!('people',
|
17
|
+
:name => 'John',
|
18
|
+
:age => 24,
|
19
|
+
:height_in_meters => 1.83,
|
20
|
+
:birth_time => Time.new(1989, 05, 27, 8, 50, 25, 0)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should set record TTL' do
|
25
|
+
TestQueryExecutor.should_receive(:execute).with(
|
26
|
+
"INSERT INTO people (name) VALUES ('John') USING TTL 3600"
|
27
|
+
)
|
28
|
+
|
29
|
+
TestQueryExecutor.write!('people', {:name => 'John'}, :ttl => 3600)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.update!' do
|
34
|
+
it 'should update some record values' do
|
35
|
+
TestQueryExecutor.should_receive(:execute).with(
|
36
|
+
"UPDATE people SET height_in_meters = 1.93, birth_time = '1989-05-28 08:50:25+0000' WHERE name = 'John'"
|
37
|
+
)
|
38
|
+
|
39
|
+
TestQueryExecutor.update!('people', "name = 'John'",
|
40
|
+
:height_in_meters => 1.93,
|
41
|
+
:birth_time => Time.new(1989, 05, 28, 8, 50, 25, 0)
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should set record TTL' do
|
46
|
+
TestQueryExecutor.should_receive(:execute).with(
|
47
|
+
"UPDATE people USING TTL 3600 SET name = 'Johnny' WHERE name = 'John'"
|
48
|
+
)
|
49
|
+
|
50
|
+
TestQueryExecutor.update!('people', "name = 'John'", {:name => 'Johnny'}, :ttl => 3600)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '.select' do
|
55
|
+
it 'should make select query with WHERE, ORDER BY and LIMIT' do
|
56
|
+
TestQueryExecutor.should_receive(:execute).with(
|
57
|
+
"SELECT * FROM people WHERE name = 'John' ORDER BY birth_time LIMIT 200"
|
58
|
+
)
|
59
|
+
|
60
|
+
TestQueryExecutor.select('people',
|
61
|
+
:selection => "name = 'John'",
|
62
|
+
:order_by => 'birth_time',
|
63
|
+
:limit => 200
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should allow to specify projection (columns loaded)' do
|
68
|
+
TestQueryExecutor.should_receive(:execute).with(
|
69
|
+
"SELECT age, birth_time FROM people WHERE name = 'John'"
|
70
|
+
)
|
71
|
+
|
72
|
+
TestQueryExecutor.select('people',
|
73
|
+
:selection => "name = 'John'",
|
74
|
+
:projection => 'age, birth_time'
|
75
|
+
)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '.delete!' do
|
80
|
+
it 'should delete rows based in a selection' do
|
81
|
+
TestQueryExecutor.should_receive(:execute).with(
|
82
|
+
"DELETE FROM people WHERE name IN ('John', 'Mike')"
|
83
|
+
)
|
84
|
+
|
85
|
+
TestQueryExecutor.delete!('people', "name IN ('John', 'Mike')")
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should delete column based in a selection and projection' do
|
89
|
+
TestQueryExecutor.should_receive(:execute).with(
|
90
|
+
"DELETE age FROM people WHERE name IN ('John', 'Mike')"
|
91
|
+
)
|
92
|
+
|
93
|
+
TestQueryExecutor.delete!('people', "name IN ('John', 'Mike')", :projection => :age)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe '.truncate!' do
|
98
|
+
it 'should clear table' do
|
99
|
+
TestQueryExecutor.should_receive(:execute).with(
|
100
|
+
"TRUNCATE people"
|
101
|
+
)
|
102
|
+
|
103
|
+
TestQueryExecutor.truncate!('people')
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -10,6 +10,7 @@ describe CassandraMigrations::Cassandra do
|
|
10
10
|
|
11
11
|
after do
|
12
12
|
CassandraMigrations::Cassandra.shutdown! if CassandraMigrations::Cassandra.client
|
13
|
+
CassandraMigrations::Config.config = nil
|
13
14
|
end
|
14
15
|
|
15
16
|
describe '.execute' do
|
@@ -61,41 +62,35 @@ describe CassandraMigrations::Cassandra do
|
|
61
62
|
end
|
62
63
|
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
# end
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
# describe "query interface" do
|
93
|
-
# it "should respond to query methods" do
|
94
|
-
# CassandraMigrations::Cassandra.should respond_to :select
|
95
|
-
# CassandraMigrations::Cassandra.should respond_to :write
|
96
|
-
# CassandraMigrations::Cassandra.should respond_to :delete
|
97
|
-
# CassandraMigrations::Cassandra.should respond_to :truncate
|
98
|
-
# end
|
99
|
-
# end
|
65
|
+
describe ".start!" do
|
66
|
+
it "should use configured keyspace" do
|
67
|
+
CassandraMigrations::Cassandra.should_receive(:use).with('cassandra_migrations_development')
|
68
|
+
CassandraMigrations::Cassandra.start!
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should log missing configuration file or similar error, but swallow exception" do
|
72
|
+
Rails.stub(:root).and_return Pathname.new("spec/fake_fixture_path")
|
73
|
+
|
74
|
+
Rails.logger = mock('logger')
|
75
|
+
Rails.logger.should_receive(:warn).with('There is no config/cassandra.yml. Skipping connection to Cassandra...')
|
76
|
+
CassandraMigrations::Cassandra.start!
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "query interface" do
|
81
|
+
it "should respond to query helper methods" do
|
82
|
+
CassandraMigrations::Cassandra.should respond_to :select
|
83
|
+
CassandraMigrations::Cassandra.should respond_to :write!
|
84
|
+
CassandraMigrations::Cassandra.should respond_to :update!
|
85
|
+
CassandraMigrations::Cassandra.should respond_to :delete!
|
86
|
+
CassandraMigrations::Cassandra.should respond_to :truncate!
|
87
|
+
end
|
88
|
+
end
|
100
89
|
|
90
|
+
describe "keyspace interface" do
|
91
|
+
it "should respond to keyspace helper methods" do
|
92
|
+
CassandraMigrations::Cassandra.should respond_to :create_keyspace!
|
93
|
+
CassandraMigrations::Cassandra.should respond_to :drop_keyspace!
|
94
|
+
end
|
95
|
+
end
|
101
96
|
end
|
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.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cql-rb
|
@@ -115,28 +115,29 @@ executables:
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
+
- lib/cassandra_migrations.rb
|
119
|
+
- lib/cassandra_migrations/capistrano.rb
|
120
|
+
- lib/cassandra_migrations/migration.rb
|
121
|
+
- lib/cassandra_migrations/migrator.rb
|
122
|
+
- lib/cassandra_migrations/cassandra.rb
|
123
|
+
- lib/cassandra_migrations/railtie.rb
|
118
124
|
- lib/cassandra_migrations/cassandra/keyspace_operations.rb
|
119
|
-
- lib/cassandra_migrations/cassandra/queries.rb
|
120
125
|
- lib/cassandra_migrations/cassandra/query_result.rb
|
126
|
+
- lib/cassandra_migrations/cassandra/queries.rb
|
127
|
+
- lib/cassandra_migrations/config.rb
|
121
128
|
- lib/cassandra_migrations/migration/table_operations.rb
|
122
|
-
- lib/cassandra_migrations/migration/table_definition.rb
|
123
129
|
- lib/cassandra_migrations/migration/column_operations.rb
|
124
|
-
- lib/cassandra_migrations/migration.rb
|
125
|
-
- lib/cassandra_migrations/
|
126
|
-
- lib/cassandra_migrations/railtie.rb
|
127
|
-
- lib/cassandra_migrations/config.rb
|
128
|
-
- lib/cassandra_migrations/migrator.rb
|
129
|
-
- lib/cassandra_migrations/cassandra.rb
|
130
|
+
- lib/cassandra_migrations/migration/table_definition.rb
|
131
|
+
- lib/cassandra_migrations/railtie/initializer.rb
|
130
132
|
- lib/cassandra_migrations/railtie/generators/cassandra_migration/cassandra_migration_generator.rb
|
131
133
|
- lib/cassandra_migrations/railtie/generators/cassandra_migration/templates/empty_migration.rb.erb
|
132
134
|
- lib/cassandra_migrations/railtie/generators/cassandra_migration/USAGE
|
133
135
|
- lib/cassandra_migrations/railtie/tasks.rake
|
134
|
-
- lib/cassandra_migrations/
|
135
|
-
- lib/cassandra_migrations/capistrano.rb
|
136
|
-
- lib/cassandra_migrations.rb
|
136
|
+
- lib/cassandra_migrations/errors.rb
|
137
137
|
- template/cassandra.yml
|
138
138
|
- spec/cassandra_migrations_spec.rb
|
139
139
|
- spec/cassandra_migrations/config_spec.rb
|
140
|
+
- spec/cassandra_migrations/cassandra/queries_spec.rb
|
140
141
|
- spec/cassandra_migrations/cassandra_spec.rb
|
141
142
|
- bin/prepare_for_cassandra
|
142
143
|
homepage: https://github.com/hsgubert/cassandra_migrations
|
@@ -167,4 +168,5 @@ summary: Cassandra schema management for a multi-environment developer.
|
|
167
168
|
test_files:
|
168
169
|
- spec/cassandra_migrations_spec.rb
|
169
170
|
- spec/cassandra_migrations/config_spec.rb
|
171
|
+
- spec/cassandra_migrations/cassandra/queries_spec.rb
|
170
172
|
- spec/cassandra_migrations/cassandra_spec.rb
|