= AWS Simple DB Demo
In trying to make AWS' SimpleDB via ActiveResource behave like ActiveRecord,
I have found several insecure situations so I won't be using it 'til
they have been figured out.
The biggest problem that I have found is the inability to protect attributes from
mass assignment. I thought that I had it resolved by pulling in some
ActiveRecord code, but it only worked on updates.
I may look into using http://github.com/hungryblank/aws_sdb_bare to see if it is
any different.
I may also try to implement some form of save_with_attribute_filter
to remove any undesired attributes. I'm not sure how to deal with
partially blocking id.
== Requires ..
- {aws_sdb gem}[http://rubygems.org/gems/aws-sdb] (included).
- {aws_sdb_proxy plugin}[http://github.com/bansalakhil/aws_sdb_proxy] (included).
- AWS Simple DB access keys in config/aws_sdb_proxy.yml
- tmp/pids directory to exist
- ruby >= 1.8.6
- ruby < 1.9
==
- rake aws_sdb:start_proxy
- script/console
- Post.create(:title => 'My first SimpleDB post')
- Post.find(:all)
- rake aws_sdb:stop_proxy
== FYI
I wrote this a few years ago and don't maintain it.
There appears to be a significant difference between ruby 1.8.6 and 1.9.1 's parsing of the simple db response.
If you run the proxy in the foreground with
rake aws_sdb:start_proxy_in_foreground
and watch the log of a Post.find(:all) call ...
On 1.8.6, we see something like ...
<?xml version="1.0" encoding="UTF-8"?>
3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990
some title
whereas on 1.9.1 we see ...
["3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990"]
["some title"]
Ruby attempts to convert ["3070 ... 90"] into an integer which turns out to be a big fat 0.
Also, after an update, sometimes the new attributes aren't visible. Reload the page and ok.
This seems to be true for a create as well. If you create a new post, the controller redirects to show, but it is occassionally too fast and doesn't exist.
ActiveResource::ResourceNotFound (Failed with 404 Not Found ):
block id as an attribute on create
add some type of attr_accessible ???
overwrite generate_id to check if exists?(id) to more ensure not a 'update'
assert_difference('Post.make_a_count_method',1) do
Post.create(:id => 1, :title => 'A')
Post.create(:id => 1, :title => 'A')
end
== Examples
> script/console
Loading development environment (Rails 2.2.2)
>> Post.find(:all)
=> []
>> Post.create(:title => "some title", :another_attribute => "with this value")
=> #"with this value", "updated_at"=>Mon Mar 30 04:43:48 UTC 2009, "title"=>"some title", "id"=>3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990, "created_at"=>Mon Mar 30 04:43:48 UTC 2009}, @prefix_options={}>
>> Post.find(:all)
=> [#"with this value", "updated_at"=>Mon Mar 30 04:43:48 UTC 2009, "title"=>"some title", "id"=>3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990, "created_at"=>Mon Mar 30 04:43:48 UTC 2009}, @prefix_options={}>]
>> Post.create(:number => 42)
=> #Mon Mar 30 04:45:47 UTC 2009, "number"=>"42", "id"=>6730970697940184632424274273340356421654395684903717024059482148711421411407017712064778238408507808663446246276405059758497765309615533943674564396229620, "created_at"=>Mon Mar 30 04:45:47 UTC 2009}, @prefix_options={}>
>> Post.find(:all)
=> [#"with this value", "updated_at"=>Mon Mar 30 04:43:48 UTC 2009, "title"=>"some title", "id"=>3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990, "created_at"=>Mon Mar 30 04:43:48 UTC 2009}, @prefix_options={}>, #Mon Mar 30 04:45:47 UTC 2009, "number"=>"42", "id"=>6730970697940184632424274273340356421654395684903717024059482148711421411407017712064778238408507808663446246276405059758497765309615533943674564396229620, "created_at"=>Mon Mar 30 04:45:47 UTC 2009}, @prefix_options={}>]
>> Post.find(:all,:params => { :number => 42})
=> [#Mon Mar 30 04:45:47 UTC 2009, "number"=>"42", "id"=>6730970697940184632424274273340356421654395684903717024059482148711421411407017712064778238408507808663446246276405059758497765309615533943674564396229620, "created_at"=>Mon Mar 30 04:45:47 UTC 2009}, @prefix_options={}>]
>> Post.find(:all,:from => :query,:params => "['number' = '42']")
=> [#Mon Mar 30 04:45:47 UTC 2009, "number"=>"42", "id"=>6730970697940184632424274273340356421654395684903717024059482148711421411407017712064778238408507808663446246276405059758497765309615533943674564396229620, "created_at"=>Mon Mar 30 04:45:47 UTC 2009}, @prefix_options={}>]
>> Post.find(:all,:params => { :number => "42"})
=> [#Mon Mar 30 04:45:47 UTC 2009, "number"=>"42", "id"=>6730970697940184632424274273340356421654395684903717024059482148711421411407017712064778238408507808663446246276405059758497765309615533943674564396229620, "created_at"=>Mon Mar 30 04:45:47 UTC 2009}, @prefix_options={}>]
>> Post.create(:title => 'My first SimpleDB post')
=> #Mon Mar 30 05:20:19 UTC 2009, "title"=>"My first SimpleDB post", "id"=>12791631632270010776716522894134888431567736022563989299219747378974000763529893565597431979600452367979536867163497402252700359837429975624057872370738492, "created_at"=>Mon Mar 30 05:20:19 UTC 2009}, @prefix_options={}>
>> Post.find(:all).length
=> 3
>> Post.find(:all, :from => :query, :params => "['title' starts-with 'My']")
=> [#Mon Mar 30 05:20:19 UTC 2009, "title"=>"My first SimpleDB post", "id"=>12791631632270010776716522894134888431567736022563989299219747378974000763529893565597431979600452367979536867163497402252700359837429975624057872370738492, "created_at"=>Mon Mar 30 05:20:19 UTC 2009}, @prefix_options={}>]
>> Post.find(:all,:from => :query,:params => "['number' = '42']")
=> [#Mon Mar 30 04:45:47 UTC 2009, "number"=>"42", "id"=>6730970697940184632424274273340356421654395684903717024059482148711421411407017712064778238408507808663446246276405059758497765309615533943674564396229620, "created_at"=>Mon Mar 30 04:45:47 UTC 2009}, @prefix_options={}>]
>> Post.find(:first)
=> #"with this value", "updated_at"=>Mon Mar 30 04:43:48 UTC 2009, "title"=>"some title", "id"=>3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990, "created_at"=>Mon Mar 30 04:43:48 UTC 2009}, @prefix_options={}>
>> Post.find(:all,:from => :query,:params => "['title' starts-with 'some']")
=> [#"with this value", "updated_at"=>Mon Mar 30 04:43:48 UTC 2009, "title"=>"some title", "id"=>3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990, "created_at"=>Mon Mar 30 04:43:48 UTC 2009}, @prefix_options={}>]
>> Post.find(:all,:from => :query,:params => "['title' starts-with 'some'] union ['another_attribute' starts-with 'with']") => [#"with this value", "updated_at"=>Mon Mar 30 04:43:48 UTC 2009, "title"=>"some title", "id"=>3070023448845834759679156993834957142448442244023777094136626700066094145387725909362088146542518510890329503558677917343691483214769109031107739779099990, "created_at"=>Mon Mar 30 04:43:48 UTC 2009}, @prefix_options={}>]
# the ":from => :query" doesn't seem to be necessary as it's the default
== Reference
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1242
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=2542