cassanity 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  Not all changes will be here, but the important ones will be for sure.
4
4
 
5
- ## 0.2.1
5
+ ## 0.2.2
6
6
 
7
- * Added [using, order and limit clauses](https://github.com/jnunemaker/cassanity/compare/414d780...3aeb254) to column family select
7
+ * Added [Keyspace#column_families](https://github.com/jnunemaker/cassanity/commit/8101835)
8
+ * Added [ColumnFamily#recreate and exists?](https://github.com/jnunemaker/cassanity/commit/11a5739)
9
+ * Added [Keyspace#exist? and ColumnFamily#exist?](https://github.com/jnunemaker/cassanity/commit/9fe88ab)
8
10
 
9
- ## 0.2.0
11
+ ## 0.2.1
10
12
 
11
- *
13
+ * Added [using, order and limit clauses](https://github.com/jnunemaker/cassanity/compare/414d780...3aeb254) to column family select
data/examples/batch.rb CHANGED
@@ -6,8 +6,7 @@ executor = Cassanity::Executors::CassandraCql.new(client: client)
6
6
 
7
7
  connection = Cassanity::Connection.new(executor: executor)
8
8
  keyspace = connection['cassanity_examples']
9
- keyspace.drop if connection.keyspace?('cassanity_examples')
10
- keyspace.create
9
+ keyspace.recreate
11
10
 
12
11
  # setting up the apps column family
13
12
  apps_schema = Cassanity::Schema.new({
@@ -6,8 +6,7 @@ executor = Cassanity::Executors::CassandraCql.new(client: client)
6
6
 
7
7
  connection = Cassanity::Connection.new(executor: executor)
8
8
  keyspace = connection['cassanity_examples']
9
- keyspace.drop if connection.keyspace?('cassanity_examples')
10
- keyspace.create
9
+ keyspace.recreate
11
10
 
12
11
  apps_schema = Cassanity::Schema.new({
13
12
  primary_key: :id,
@@ -23,7 +23,7 @@ keyspace = connection.keyspace('cassanity_examples', {
23
23
  })
24
24
 
25
25
  # drop keyspace if it exists
26
- keyspace.drop if connection.keyspace?('cassanity_examples')
26
+ keyspace.drop if keyspace.exists?
27
27
 
28
28
  # create the keyspace, uses options from above
29
29
  keyspace.create
@@ -0,0 +1,19 @@
1
+ module Cassanity
2
+ module ArgumentGenerators
3
+ class ColumnFamilies
4
+
5
+ # Internal
6
+ def call(args = {})
7
+ variables = []
8
+ cql = 'SELECT * FROM system.schema_columnfamilies'
9
+
10
+ if (keyspace_name = args[:keyspace_name])
11
+ cql << ' WHERE "keyspace" = ?'
12
+ variables << keyspace_name
13
+ end
14
+
15
+ [cql, *variables]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -30,6 +30,29 @@ module Cassanity
30
30
  @schema = args[:schema]
31
31
  end
32
32
 
33
+ # Public: Returns true or false depending on if column family exists in
34
+ # the keyspace.
35
+ #
36
+ # Returns true if column family exists, false if it does not.
37
+ def exists?
38
+ @executor.call({
39
+ command: :column_families,
40
+ arguments: {
41
+ keyspace_name: @keyspace.name,
42
+ }
43
+ }).any? { |row| row['columnfamily'].to_s == @name.to_s }
44
+ end
45
+
46
+ alias_method :exist?, :exists?
47
+
48
+ # Public: Drops column family if it exists and then calls create.
49
+ #
50
+ # Returns nothing.
51
+ def recreate
52
+ drop if exists?
53
+ create
54
+ end
55
+
33
56
  # Public: Creates the column family in the keyspace based on the schema.
34
57
  #
35
58
  # args - The Hash of arguments to pass to the executor. Always passes
@@ -4,6 +4,7 @@ require 'cassanity/argument_generators/keyspaces'
4
4
  require 'cassanity/argument_generators/keyspace_create'
5
5
  require 'cassanity/argument_generators/keyspace_drop'
6
6
  require 'cassanity/argument_generators/keyspace_use'
7
+ require 'cassanity/argument_generators/column_families'
7
8
  require 'cassanity/argument_generators/column_family_create'
8
9
  require 'cassanity/argument_generators/column_family_drop'
9
10
  require 'cassanity/argument_generators/column_family_truncate'
@@ -28,6 +29,7 @@ module Cassanity
28
29
  keyspace_create: Cassanity::ArgumentGenerators::KeyspaceCreate.new,
29
30
  keyspace_drop: Cassanity::ArgumentGenerators::KeyspaceDrop.new,
30
31
  keyspace_use: Cassanity::ArgumentGenerators::KeyspaceUse.new,
32
+ column_families: Cassanity::ArgumentGenerators::ColumnFamilies.new,
31
33
  column_family_create: Cassanity::ArgumentGenerators::ColumnFamilyCreate.new,
32
34
  column_family_drop: Cassanity::ArgumentGenerators::ColumnFamilyDrop.new,
33
35
  column_family_truncate: Cassanity::ArgumentGenerators::ColumnFamilyTruncate.new,
@@ -43,8 +45,9 @@ module Cassanity
43
45
 
44
46
  # Private: Hash of commands to related result transformers.
45
47
  ResultTransformers = {
46
- column_family_select: Cassanity::ResultTransformers::ResultToArray.new,
47
48
  keyspaces: Cassanity::ResultTransformers::ResultToArray.new,
49
+ column_families: Cassanity::ResultTransformers::ResultToArray.new,
50
+ column_family_select: Cassanity::ResultTransformers::ResultToArray.new,
48
51
  }
49
52
 
50
53
  # Private: Default result transformer for commands that do not have one.
@@ -40,6 +40,8 @@ module Cassanity
40
40
  rows.any? { |row| row['name'].to_s == name.to_s }
41
41
  end
42
42
 
43
+ alias_method :exist?, :exists?
44
+
43
45
  # Public: Creates the keyspace
44
46
  #
45
47
  # args - The Hash of arguments to pass to the argument generator
@@ -68,7 +70,10 @@ module Cassanity
68
70
  })
69
71
  end
70
72
 
71
- def recreate(args = {})
73
+ # Public: Drops keyspace if it exists and then calls create.
74
+ #
75
+ # Returns nothing.
76
+ def recreate
72
77
  drop if exists?
73
78
  create
74
79
  end
@@ -111,6 +116,23 @@ module Cassanity
111
116
  })
112
117
  end
113
118
 
119
+ # Public: Get all column families for keyspace.
120
+ #
121
+ # Returns Array of Cassanity::ColumnFamily instances.
122
+ def column_families
123
+ @executor.call({
124
+ command: :column_families,
125
+ arguments: {
126
+ keyspace_name: @name,
127
+ },
128
+ }).map { |row|
129
+ ColumnFamily.new({
130
+ name: row['columnfamily'],
131
+ keyspace: self,
132
+ })
133
+ }
134
+ end
135
+
114
136
  # Public: Get a column family instance
115
137
  #
116
138
  # name_or_args - The String name of the column family or a Hash which has
@@ -1,3 +1,3 @@
1
1
  module Cassanity
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -62,6 +62,25 @@ describe Cassanity::ColumnFamily do
62
62
  client_drop_keyspace(client, keyspace_name)
63
63
  end
64
64
 
65
+ it "knows if it exists" do
66
+ subject.exists?.should be_true
67
+ client_drop_column_family(client, column_family_name)
68
+ subject.exists?.should be_false
69
+ end
70
+
71
+ it "can recreate when not created" do
72
+ client_drop_column_family(client, column_family_name)
73
+ client_column_family?(client, column_family_name).should be_false
74
+ subject.recreate
75
+ client_column_family?(client, column_family_name).should be_true
76
+ end
77
+
78
+ it "can recreate when already created" do
79
+ client_column_family?(client, column_family_name).should be_true
80
+ subject.recreate
81
+ client_column_family?(client, column_family_name).should be_true
82
+ end
83
+
65
84
  it "can create itself" do
66
85
  column_family = described_class.new(arguments.merge(name: 'people'))
67
86
  column_family.create
@@ -80,4 +80,22 @@ describe Cassanity::Keyspace do
80
80
  subject.drop
81
81
  client_keyspace?(client, keyspace_name).should be_false
82
82
  end
83
+
84
+ it "knows column families" do
85
+ client_create_column_family(client, 'something1')
86
+ client_create_column_family(client, 'something2')
87
+
88
+ result = subject.column_families
89
+ result.each do |column_family|
90
+ column_family.should be_instance_of(Cassanity::ColumnFamily)
91
+ column_family.keyspace.should eq(subject)
92
+ end
93
+
94
+ names = result.map(&:name)
95
+ names.should include('something1')
96
+ names.should include('something2')
97
+
98
+ client_drop_column_family(client, 'something1')
99
+ client_drop_column_family(client, 'something2')
100
+ end
83
101
  end
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+ require 'cassanity/argument_generators/column_families'
3
+
4
+ describe Cassanity::ArgumentGenerators::ColumnFamilies do
5
+ describe "#call" do
6
+ context "without keyspace" do
7
+ it "returns array of arguments for selecting all column families" do
8
+ cql = 'SELECT * FROM system.schema_columnfamilies'
9
+ expected = [cql]
10
+ subject.call.should eq(expected)
11
+ end
12
+ end
13
+
14
+ context "with :keyspace_name" do
15
+ it "returns array of arguments for selecting all column families for keyspace" do
16
+ cql = 'SELECT * FROM system.schema_columnfamilies WHERE "keyspace" = ?'
17
+ variables = ['foo']
18
+ expected = [cql, 'foo']
19
+ subject.call(keyspace_name: 'foo').should eq(expected)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -80,6 +80,38 @@ describe Cassanity::ColumnFamily do
80
80
  end
81
81
  end
82
82
 
83
+ shared_examples_for "column family existence" do |method_name|
84
+ it "returns true if column family name included in column families" do
85
+ executor.should_receive(:call).with({
86
+ command: :column_families,
87
+ arguments: {keyspace_name: keyspace.name},
88
+ }).and_return([
89
+ {'columnfamily' => column_family_name},
90
+ ])
91
+
92
+ subject.send(method_name).should be_true
93
+ end
94
+
95
+ it "returns false if column family name not included in column families" do
96
+ executor.should_receive(:call).with({
97
+ command: :column_families,
98
+ arguments: {keyspace_name: keyspace.name},
99
+ }).and_return([
100
+ {'columnfamily' => 'boo'},
101
+ ])
102
+
103
+ subject.send(method_name).should be_false
104
+ end
105
+ end
106
+
107
+ describe "#exists?" do
108
+ include_examples "column family existence", :exists?
109
+ end
110
+
111
+ describe "#exist?" do
112
+ include_examples "column family existence", :exist?
113
+ end
114
+
83
115
  describe "#create" do
84
116
  context "with schema set during initialization" do
85
117
  subject {
@@ -66,6 +66,7 @@ describe Cassanity::Executors::CassandraCql do
66
66
  :keyspace_create,
67
67
  :keyspace_drop,
68
68
  :keyspace_use,
69
+ :column_families,
69
70
  :column_family_create,
70
71
  :column_family_drop,
71
72
  :column_family_truncate,
@@ -178,27 +178,35 @@ describe Cassanity::Keyspace do
178
178
  end
179
179
  end
180
180
 
181
- describe "#exists?" do
181
+ shared_examples_for "keyspace existence" do |method_name|
182
182
  it "returns true if name in existing keyspace names" do
183
183
  executor.should_receive(:call).with(command: :keyspaces).and_return([
184
184
  {'name' => keyspace_name.to_s},
185
185
  ])
186
- subject.exists?.should be_true
186
+ subject.send(method_name).should be_true
187
187
  end
188
188
 
189
189
  it "returns false if name not in existing keyspace names" do
190
190
  executor.should_receive(:call).with(command: :keyspaces).and_return([
191
191
  {'name' => 'batman'},
192
192
  ])
193
- subject.exists?.should be_false
193
+ subject.send(method_name).should be_false
194
194
  end
195
195
 
196
196
  it "returns false if no keyspaces" do
197
197
  executor.should_receive(:call).with(command: :keyspaces).and_return([])
198
- subject.exists?.should be_false
198
+ subject.send(method_name).should be_false
199
199
  end
200
200
  end
201
201
 
202
+ describe "#exists?" do
203
+ include_examples "keyspace existence", :exists?
204
+ end
205
+
206
+ describe "#exist?" do
207
+ include_examples "keyspace existence", :exist?
208
+ end
209
+
202
210
  describe "#create" do
203
211
  it "sends command and arguments, including :name, to executor" do
204
212
  args = {something: 'else'}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassanity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.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: 2012-11-16 00:00:00.000000000 Z
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cassandra-cql
@@ -51,6 +51,7 @@ files:
51
51
  - examples/keyspaces.rb
52
52
  - lib/cassanity.rb
53
53
  - lib/cassanity/argument_generators/batch.rb
54
+ - lib/cassanity/argument_generators/column_families.rb
54
55
  - lib/cassanity/argument_generators/column_family_alter.rb
55
56
  - lib/cassanity/argument_generators/column_family_create.rb
56
57
  - lib/cassanity/argument_generators/column_family_delete.rb
@@ -86,6 +87,7 @@ files:
86
87
  - spec/integration/cassanity/keyspace_spec.rb
87
88
  - spec/support/cassanity_helpers.rb
88
89
  - spec/unit/cassanity/argument_generators/batch_spec.rb
90
+ - spec/unit/cassanity/argument_generators/column_families_spec.rb
89
91
  - spec/unit/cassanity/argument_generators/column_family_alter_spec.rb
90
92
  - spec/unit/cassanity/argument_generators/column_family_create_spec.rb
91
93
  - spec/unit/cassanity/argument_generators/column_family_delete_spec.rb
@@ -129,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
131
  version: '0'
130
132
  segments:
131
133
  - 0
132
- hash: -3816618086957989783
134
+ hash: 878477492175471212
133
135
  required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  none: false
135
137
  requirements:
@@ -138,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
140
  version: '0'
139
141
  segments:
140
142
  - 0
141
- hash: -3816618086957989783
143
+ hash: 878477492175471212
142
144
  requirements: []
143
145
  rubyforge_project:
144
146
  rubygems_version: 1.8.23
@@ -153,6 +155,7 @@ test_files:
153
155
  - spec/integration/cassanity/keyspace_spec.rb
154
156
  - spec/support/cassanity_helpers.rb
155
157
  - spec/unit/cassanity/argument_generators/batch_spec.rb
158
+ - spec/unit/cassanity/argument_generators/column_families_spec.rb
156
159
  - spec/unit/cassanity/argument_generators/column_family_alter_spec.rb
157
160
  - spec/unit/cassanity/argument_generators/column_family_create_spec.rb
158
161
  - spec/unit/cassanity/argument_generators/column_family_delete_spec.rb