activerecord-simpledb-adapter 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ module ActiveRecord
2
+ class SimpleDBLogger
3
+ def initialize(logger)
4
+ @logger = logger
5
+ end
6
+
7
+ def info *args
8
+ #skip noisy info messages from aws interface
9
+ end
10
+
11
+ def method_missing m, *args
12
+ @logger.send(m, args)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class SimpleDbTableDifinition < TableDefinition
4
+
5
+ attr_reader :collection_column_name
6
+
7
+ def initialize collection_column_name
8
+ super nil
9
+ @collection_column_name = collection_column_name
10
+ end
11
+
12
+ def xml_column_fallback(*args)
13
+ raise ConfigurationError, "Not supported"
14
+ end
15
+
16
+ def [](name)
17
+ @columns.find {|column| column.db_column_name == name.to_s}
18
+ end
19
+
20
+ def column(name, type, options = {})
21
+ raise ConfigurationError, %Q(column '#{collection_column_name}' reserved, please change column name) if name.to_s == collection_column_name
22
+ @columns << SimpleDBColumn.new(name.to_s, type.to_sym, options[:limit], options[:precision], options[:to], options[:default])
23
+ self
24
+ end
25
+
26
+ def columns_with_defaults
27
+ @columns_with_defaults ||= @columns.select { |column| column.default.present? }
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,50 @@
1
+ module ActiveRecord
2
+ module Validations
3
+ class UniquenessValidator < ActiveModel::EachValidator
4
+
5
+ def validate_each(record, attribute, value)
6
+ finder_class = find_finder_class_for(record)
7
+ table = finder_class.unscoped
8
+
9
+ table_name = record.class.quoted_table_name
10
+
11
+ if value && record.class.serialized_attributes.key?(attribute.to_s)
12
+ value = YAML.dump value
13
+ end
14
+
15
+ sql, params = mount_sql_and_params(finder_class, table_name, attribute, value)
16
+
17
+ relation = table.where(sql, *params)
18
+
19
+ Array.wrap(options[:scope]).each do |scope_item|
20
+ scope_value = record.send(scope_item)
21
+ relation = relation.where(scope_item => scope_value)
22
+ end
23
+
24
+ if record.persisted?
25
+ # TODO : This should be in Arel
26
+ relation = relation.where("#{record.class.primary_key} != ?", record.send(:id))
27
+ end
28
+
29
+ if relation.exists?
30
+ record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
31
+ end
32
+ end
33
+
34
+ protected
35
+ def mount_sql_and_params(klass, table_name, attribute, value) #:nodoc:
36
+ column = klass.columns_hash[attribute.to_s]
37
+
38
+ sql_attribute = "#{klass.connection.quote_column_name(column.db_column_name)}"
39
+ sql = "#{sql_attribute} = ?"
40
+ if value.nil?
41
+ [sql, [value]]
42
+ elsif (options[:case_sensitive] || !column.text?)
43
+ [sql, [value]]
44
+ else
45
+ [sql, [value.downcase]]
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ #spec helpers difition
3
+ class F < ActiveRecord::Base
4
+ columns_definition do |t|
5
+ t.string :body
6
+ end
7
+
8
+ has_many :bs
9
+ after_save lambda { self.bs.create!({:body => "test"}) }
10
+ end
11
+ class B < ActiveRecord::Base
12
+ columns_definition do |t|
13
+ t.string :body
14
+ t.string :f_id
15
+ end
16
+ end
17
+ #testes
18
+ describe "SimpleDBAdapter ActiveRecord batches operation" do
19
+
20
+ before :each do
21
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
22
+ ActiveRecord::Base.establish_connection($config)
23
+ ActiveRecord::Base.connection.create_domain($config[:domain_name])
24
+ end
25
+
26
+ after :each do
27
+ ActiveRecord::Base.connection.delete_domain($config[:domain_name])
28
+ end
29
+
30
+ it "should work with usual create statments" do
31
+ count = 5
32
+ Person.batch do
33
+ count.times { |i| Person.create_valid }
34
+ Person.count.should == 0
35
+ end
36
+ items = Person.all
37
+ items.count.should == count
38
+ items.each { |item| item.is_valid? }
39
+ end
40
+
41
+ #this test doesn't work with fakesdb
42
+ it "should work with usual destroy statments" do
43
+ count = 5
44
+ items = []
45
+ count.times { items << Person.create_valid }
46
+ Person.batch(:delete) do
47
+ items.each {|item| item.destroy }
48
+ end
49
+ Person.count.should == 0
50
+ end
51
+
52
+ it "should auto split to several batches when count of items more than BATCH_MAX_ITEM_COUNT" do
53
+ count = 35
54
+ Person.batch do
55
+ count.times { |i| Person.create_valid }
56
+ end
57
+ Person.count.should == count
58
+ end
59
+
60
+ it "should batch internal operations too (sub-updates)" do
61
+ count = 5
62
+ F.batch do
63
+ count.times { F.create!({:body => "body"}) }
64
+ end
65
+ [F, B].each {|r| r.count.should == count }
66
+ end
67
+ end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,7 @@ rescue Bundler::GemNotFound => e
12
12
  end if File.exist?(gemfile)
13
13
 
14
14
  $:.unshift(File.dirname(__FILE__) + '/../lib')
15
+ require 'ap'
15
16
  require 'rspec'
16
17
  require 'aws'
17
18
  require 'active_record'
@@ -54,6 +55,12 @@ class Person < ActiveRecord::Base
54
55
  :state => 'paid'
55
56
  }
56
57
  end
57
- end
58
58
 
59
+ def self.create_valid
60
+ create! valid_params
61
+ end
59
62
 
63
+ def is_valid?
64
+ Person.valid_params.all? {|key, value| self.try(key) == value}
65
+ end
66
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-simpledb-adapter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ilia Ablamonov
@@ -17,11 +17,25 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-01-21 00:00:00 +03:00
20
+ date: 2011-04-21 00:00:00 +04:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
24
  version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ requirement: *id001
34
+ prerelease: false
35
+ type: :development
36
+ name: awesome_print
37
+ - !ruby/object:Gem::Dependency
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
25
39
  none: false
26
40
  requirements:
27
41
  - - ~>
@@ -32,12 +46,12 @@ dependencies:
32
46
  - 3
33
47
  - 0
34
48
  version: 2.3.0
35
- requirement: *id001
49
+ requirement: *id002
36
50
  prerelease: false
37
51
  type: :development
38
52
  name: rspec
39
53
  - !ruby/object:Gem::Dependency
40
- version_requirements: &id002 !ruby/object:Gem::Requirement
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
41
55
  none: false
42
56
  requirements:
43
57
  - - ~>
@@ -48,12 +62,12 @@ dependencies:
48
62
  - 0
49
63
  - 0
50
64
  version: 1.0.0
51
- requirement: *id002
65
+ requirement: *id003
52
66
  prerelease: false
53
67
  type: :development
54
68
  name: bundler
55
69
  - !ruby/object:Gem::Dependency
56
- version_requirements: &id003 !ruby/object:Gem::Requirement
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
57
71
  none: false
58
72
  requirements:
59
73
  - - ~>
@@ -64,12 +78,12 @@ dependencies:
64
78
  - 5
65
79
  - 2
66
80
  version: 1.5.2
67
- requirement: *id003
81
+ requirement: *id004
68
82
  prerelease: false
69
83
  type: :development
70
84
  name: jeweler
71
85
  - !ruby/object:Gem::Dependency
72
- version_requirements: &id004 !ruby/object:Gem::Requirement
86
+ version_requirements: &id005 !ruby/object:Gem::Requirement
73
87
  none: false
74
88
  requirements:
75
89
  - - ">="
@@ -78,12 +92,12 @@ dependencies:
78
92
  segments:
79
93
  - 0
80
94
  version: "0"
81
- requirement: *id004
95
+ requirement: *id005
82
96
  prerelease: false
83
97
  type: :development
84
98
  name: rcov
85
99
  - !ruby/object:Gem::Dependency
86
- version_requirements: &id005 !ruby/object:Gem::Requirement
100
+ version_requirements: &id006 !ruby/object:Gem::Requirement
87
101
  none: false
88
102
  requirements:
89
103
  - - ">="
@@ -92,12 +106,12 @@ dependencies:
92
106
  segments:
93
107
  - 0
94
108
  version: "0"
95
- requirement: *id005
109
+ requirement: *id006
96
110
  prerelease: false
97
111
  type: :development
98
112
  name: aws
99
113
  - !ruby/object:Gem::Dependency
100
- version_requirements: &id006 !ruby/object:Gem::Requirement
114
+ version_requirements: &id007 !ruby/object:Gem::Requirement
101
115
  none: false
102
116
  requirements:
103
117
  - - ">="
@@ -106,12 +120,12 @@ dependencies:
106
120
  segments:
107
121
  - 0
108
122
  version: "0"
109
- requirement: *id006
123
+ requirement: *id007
110
124
  prerelease: false
111
125
  type: :development
112
126
  name: activerecord
113
127
  - !ruby/object:Gem::Dependency
114
- version_requirements: &id007 !ruby/object:Gem::Requirement
128
+ version_requirements: &id008 !ruby/object:Gem::Requirement
115
129
  none: false
116
130
  requirements:
117
131
  - - ">="
@@ -120,12 +134,12 @@ dependencies:
120
134
  segments:
121
135
  - 0
122
136
  version: "0"
123
- requirement: *id007
137
+ requirement: *id008
124
138
  prerelease: false
125
139
  type: :development
126
140
  name: activesupport
127
141
  - !ruby/object:Gem::Dependency
128
- version_requirements: &id008 !ruby/object:Gem::Requirement
142
+ version_requirements: &id009 !ruby/object:Gem::Requirement
129
143
  none: false
130
144
  requirements:
131
145
  - - "="
@@ -138,12 +152,12 @@ dependencies:
138
152
  - prerails3
139
153
  - 2
140
154
  version: 0.2.0.prerails3.2
141
- requirement: *id008
155
+ requirement: *id009
142
156
  prerelease: false
143
157
  type: :development
144
158
  name: genspec
145
159
  - !ruby/object:Gem::Dependency
146
- version_requirements: &id009 !ruby/object:Gem::Requirement
160
+ version_requirements: &id010 !ruby/object:Gem::Requirement
147
161
  none: false
148
162
  requirements:
149
163
  - - ">="
@@ -152,12 +166,12 @@ dependencies:
152
166
  segments:
153
167
  - 0
154
168
  version: "0"
155
- requirement: *id009
169
+ requirement: *id010
156
170
  prerelease: false
157
171
  type: :development
158
172
  name: thor
159
173
  - !ruby/object:Gem::Dependency
160
- version_requirements: &id010 !ruby/object:Gem::Requirement
174
+ version_requirements: &id011 !ruby/object:Gem::Requirement
161
175
  none: false
162
176
  requirements:
163
177
  - - ">="
@@ -166,12 +180,12 @@ dependencies:
166
180
  segments:
167
181
  - 0
168
182
  version: "0"
169
- requirement: *id010
183
+ requirement: *id011
170
184
  prerelease: false
171
185
  type: :development
172
186
  name: rails
173
187
  - !ruby/object:Gem::Dependency
174
- version_requirements: &id011 !ruby/object:Gem::Requirement
188
+ version_requirements: &id012 !ruby/object:Gem::Requirement
175
189
  none: false
176
190
  requirements:
177
191
  - - ~>
@@ -182,44 +196,44 @@ dependencies:
182
196
  - 3
183
197
  - 0
184
198
  version: 2.3.0
185
- requirement: *id011
199
+ requirement: *id012
186
200
  prerelease: false
187
201
  type: :runtime
188
202
  name: aws
189
203
  - !ruby/object:Gem::Dependency
190
- version_requirements: &id012 !ruby/object:Gem::Requirement
204
+ version_requirements: &id013 !ruby/object:Gem::Requirement
191
205
  none: false
192
206
  requirements:
193
207
  - - ~>
194
208
  - !ruby/object:Gem::Version
195
- hash: 1
209
+ hash: 9
196
210
  segments:
197
211
  - 3
198
212
  - 0
199
- - 3
200
- version: 3.0.3
201
- requirement: *id012
213
+ - 7
214
+ version: 3.0.7
215
+ requirement: *id013
202
216
  prerelease: false
203
217
  type: :runtime
204
218
  name: activerecord
205
219
  - !ruby/object:Gem::Dependency
206
- version_requirements: &id013 !ruby/object:Gem::Requirement
220
+ version_requirements: &id014 !ruby/object:Gem::Requirement
207
221
  none: false
208
222
  requirements:
209
223
  - - ~>
210
224
  - !ruby/object:Gem::Version
211
- hash: 1
225
+ hash: 9
212
226
  segments:
213
227
  - 3
214
228
  - 0
215
- - 3
216
- version: 3.0.3
217
- requirement: *id013
229
+ - 7
230
+ version: 3.0.7
231
+ requirement: *id014
218
232
  prerelease: false
219
233
  type: :runtime
220
234
  name: activesupport
221
235
  - !ruby/object:Gem::Dependency
222
- version_requirements: &id014 !ruby/object:Gem::Requirement
236
+ version_requirements: &id015 !ruby/object:Gem::Requirement
223
237
  none: false
224
238
  requirements:
225
239
  - - ~>
@@ -230,23 +244,23 @@ dependencies:
230
244
  - 1
231
245
  - 1
232
246
  version: 2.1.1
233
- requirement: *id014
247
+ requirement: *id015
234
248
  prerelease: false
235
249
  type: :runtime
236
250
  name: uuidtools
237
251
  - !ruby/object:Gem::Dependency
238
- version_requirements: &id015 !ruby/object:Gem::Requirement
252
+ version_requirements: &id016 !ruby/object:Gem::Requirement
239
253
  none: false
240
254
  requirements:
241
255
  - - ~>
242
256
  - !ruby/object:Gem::Version
243
- hash: 1
257
+ hash: 9
244
258
  segments:
245
259
  - 3
246
260
  - 0
247
- - 3
248
- version: 3.0.3
249
- requirement: *id015
261
+ - 7
262
+ version: 3.0.7
263
+ requirement: *id016
250
264
  prerelease: false
251
265
  type: :runtime
252
266
  name: rails
@@ -266,11 +280,20 @@ files:
266
280
  - README.md
267
281
  - activerecord-simpledb-adapter.gemspec
268
282
  - lib/active_record/connection_adapters/simpledb_adapter.rb
283
+ - lib/active_record/connection_adapters/simpledb_adapter/adapter.rb
284
+ - lib/active_record/connection_adapters/simpledb_adapter/base.rb
285
+ - lib/active_record/connection_adapters/simpledb_adapter/column.rb
286
+ - lib/active_record/connection_adapters/simpledb_adapter/finder_methods.rb
287
+ - lib/active_record/connection_adapters/simpledb_adapter/misc/aws_overrides.rb
288
+ - lib/active_record/connection_adapters/simpledb_adapter/misc/simpledb_logger.rb
289
+ - lib/active_record/connection_adapters/simpledb_adapter/table_definition.rb
290
+ - lib/active_record/connection_adapters/simpledb_adapter/validations.rb
269
291
  - lib/activerecord-simpledb-adapter.rb
270
292
  - lib/arel/visitors/simpledb.rb
271
293
  - lib/generators/active_record/model/model_generator.rb
272
294
  - lib/generators/active_record/model/templates/model.rb
273
295
  - lib/tasks/simpledb.rake
296
+ - spec/active_record/batch_spec.rb
274
297
  - spec/active_record/connection_adapters/simpledb_adapter_spec.rb
275
298
  - spec/active_record/core_actions_spec.rb
276
299
  - spec/active_record/defaults_spec.rb
@@ -317,6 +340,7 @@ signing_key:
317
340
  specification_version: 3
318
341
  summary: ActiveRecord SimpleDB adapter
319
342
  test_files:
343
+ - spec/active_record/batch_spec.rb
320
344
  - spec/active_record/connection_adapters/simpledb_adapter_spec.rb
321
345
  - spec/active_record/core_actions_spec.rb
322
346
  - spec/active_record/defaults_spec.rb