toy-dynamo 0.0.2 → 0.0.3

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: f54db7e521096d9ee23c898eaa57a8932f8cf299
4
- data.tar.gz: add67a16a2bad5923b97e4916820d8bb00bab878
3
+ metadata.gz: 2c505b06ca1b99d16eb355a403fa7dcc40b9653a
4
+ data.tar.gz: 010db3bf334c8759fc922cd5801b235b76cf3f05
5
5
  SHA512:
6
- metadata.gz: 6038f327a79d71a8b6575dc4aaef030528ae5f303d0317c462c15d2fa30ea6bd85e57e8e5a879eb8d83d599ea4fdd9fc83fb4c0fe83ccc3530d42945cc287742
7
- data.tar.gz: 2a973d5349a42a2b85c0e79877123f08496984c7642beca613033021a34efd20ad49a37af9baa02accbca80dfc7209ba2a659d71b5005d79e7088fdd550d7804
6
+ metadata.gz: 7c76caefca0f5ecb58c5230afa9940b9e8efbc1c3c840d788f04296c4a40afdb064c3fd843a8dab837fedf3a4e7b0f4c89359ac4615a9139f791ef65afae947a
7
+ data.tar.gz: 1a48d0ef0c3efd90e26bb154850500ca462bc229b2fb6328522d2a5a453970e1400ce67a7f8cd3b632b875e04560d1e65cb99c6bce072c29d813b8ca9a4ad015
data/README.md CHANGED
@@ -24,7 +24,7 @@ end
24
24
  * Can also specify an Array of attributes to project besides the primary keys ex:
25
25
  * :projection => [:subject, :commenter_name]
26
26
 
27
- ## Usage
27
+ ## Basic Usage
28
28
  * **Read Hash Key Only**
29
29
  * Example:
30
30
  * Model.read("xyz")
@@ -59,6 +59,16 @@ end
59
59
  * Model.count_range("xyz", :range => {:comment_id.eq => "123"})
60
60
  * Returns the number of total results (no attributes)
61
61
 
62
+ ## Extras Usage
63
+ * **Init a UUID value**
64
+ * attribute :user_guid, String, :default => lambda { SimpleUUID::UUID.new.to_guid }
65
+ * **Use with fake_dynamo**
66
+ * adapter :dynamo, AWS::DynamoDB::ClientV2.new({
67
+ :use_ssl => false,
68
+ :dynamo_db_endpoint => 'localhost',
69
+ :dynamo_db_port => 4567
70
+ }), {:model => self}
71
+
62
72
  ## Compatibility
63
73
  * Tested with
64
74
  * Rails 4
@@ -68,3 +78,4 @@ end
68
78
  * raise error if trying to use an attribute that wasn't 'select'ed (defaulting to selecting all attributes which loads everything with an extra read)
69
79
  * while loop for situation where batch_get_item returns batched results
70
80
  * error out on mismatch of table schema from dynamo_table schema (changed?)
81
+ * allow changes to read write capacity
@@ -22,8 +22,11 @@ module Toy
22
22
  else
23
23
  @dynamo_table_config_block.call unless @dynamo_table_configged
24
24
 
25
- @dynamo_table ||= Table.new(table_schema, self.adapter.client)
26
- @dynamo_table_configged = true
25
+ unless @dynamo_table && @dynamo_table_configged
26
+ @dynamo_table = Table.new(table_schema, self.adapter.client)
27
+ validate_key_schema if @dynamo_table.schema_loaded_from_dynamo
28
+ @dynamo_table_configged = true
29
+ end
27
30
  @dynamo_table
28
31
  end
29
32
  end
@@ -158,6 +161,28 @@ module Toy
158
161
  end
159
162
  end
160
163
 
164
+ def validate_key_schema
165
+ if (@dynamo_table.schema_loaded_from_dynamo[:table][:key_schema] != table_schema[:key_schema])
166
+ raise ArgumentError, "It appears your key schema (Hash Key/Range Key) have changed from the table definition. Rebuilding the table is necessary."
167
+ end
168
+
169
+ if (@dynamo_table.schema_loaded_from_dynamo[:table][:attribute_definitions] != table_schema[:attribute_definitions])
170
+ raise ArgumentError, "It appears your attribute definition (types?) have changed from the table definition. Rebuilding the table is necessary."
171
+ end
172
+
173
+ if (@dynamo_table.schema_loaded_from_dynamo[:table][:local_secondary_indexes].dup.collect {|i| i.delete_if{|k, v| [:index_size_bytes, :item_count].include?(k) }; i } != table_schema[:local_secondary_indexes])
174
+ raise ArgumentError, "It appears your local secondary indexes have changed from the table definition. Rebuilding the table is necessary."
175
+ end
176
+
177
+ if @dynamo_table.schema_loaded_from_dynamo[:table][:provisioned_throughput][:read_capacity_units] != read_provision
178
+ puts "read_capacity_units mismatch. Need to update table?"
179
+ end
180
+
181
+ if @dynamo_table.schema_loaded_from_dynamo[:table][:provisioned_throughput][:write_capacity_units] != write_provision
182
+ puts "write_capacity_units mismatch. Need to update table?"
183
+ end
184
+ end
185
+
161
186
  def local_secondary_indexes
162
187
  @local_secondary_indexes ||= []
163
188
  end
@@ -2,7 +2,7 @@ module Toy
2
2
  module Dynamo
3
3
  class Table
4
4
 
5
- attr_reader :table_schema, :client, :schema_loaded, :hash_key, :range_keys
5
+ attr_reader :table_schema, :client, :schema_loaded_from_dynamo, :hash_key, :range_keys
6
6
 
7
7
  RETURNED_CONSUMED_CAPACITY = {
8
8
  :none => "NONE",
@@ -40,19 +40,21 @@ module Toy
40
40
  begin
41
41
  self.load_schema
42
42
  rescue AWS::DynamoDB::Errors::ResourceNotFoundException => e
43
- puts "No table found! Creating..."
44
- self.create
45
- self.load_schema
43
+ puts "No table found!"
44
+ #self.create
45
+ #self.load_schema
46
46
  end
47
47
  end
48
48
 
49
49
  def load_schema
50
- @schema_loaded = @client.describe_table(:table_name => @table_schema[:table_name])
50
+ @schema_loaded_from_dynamo = self.describe
51
51
 
52
- @schema_loaded[:table][:key_schema].each do |key|
52
+ @schema_loaded_from_dynamo[:table][:key_schema].each do |key|
53
+ key_attr = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == key[:attribute_name]}
54
+ next if key_attr.nil?
53
55
  key_schema_attr = {
54
56
  :attribute_name => key[:attribute_name],
55
- :attribute_type => @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == key[:attribute_name]}[:attribute_type]
57
+ :attribute_type => key_attr[:attribute_type]
56
58
  }
57
59
 
58
60
  if key[:key_type] == "HASH"
@@ -62,8 +64,8 @@ module Toy
62
64
  end
63
65
  end
64
66
 
65
- if @schema_loaded[:table][:local_secondary_indexes]
66
- @schema_loaded[:table][:local_secondary_indexes].each do |key|
67
+ if @schema_loaded_from_dynamo[:table][:local_secondary_indexes]
68
+ @schema_loaded_from_dynamo[:table][:local_secondary_indexes].each do |key|
67
69
  lsi_range_key = key[:key_schema].find{|h| h[:key_type] == "RANGE" }
68
70
  (@range_keys ||= []) << {
69
71
  :attribute_name => lsi_range_key[:attribute_name],
@@ -73,7 +75,7 @@ module Toy
73
75
  end
74
76
  end
75
77
 
76
- @schema_loaded
78
+ @schema_loaded_from_dynamo
77
79
  end
78
80
 
79
81
  def hash_key_item_param(value)
@@ -331,14 +333,14 @@ module Toy
331
333
  :table_name => options[:table_name] || @table_schema[:table_name]
332
334
  }))
333
335
 
334
- while (table_metadata = self.describe)[:table][:table_status] == "CREATING"
336
+ while (table_metadata = self.describe(options))[:table][:table_status] == "CREATING"
335
337
  sleep 1
336
338
  end
337
339
  table_metadata
338
340
  end
339
341
 
340
- def describe
341
- @client.describe_table(:table_name => @table_schema[:table_name])
342
+ def describe(options={})
343
+ @client.describe_table(:table_name => options[:table_name] || @table_schema[:table_name])
342
344
  end
343
345
 
344
346
  def delete(options={})
@@ -0,0 +1,18 @@
1
+ require 'rake'
2
+
3
+ namespace :ddb do
4
+ desc 'Create a DynamoDB table'
5
+ task :create => :environment do
6
+ raise "expected usage: rake ddb:create CLASS=User" unless ENV['CLASS']
7
+ options = {}
8
+ options.merge!(:table_name => ENV['TABLE']) if ENV['TABLE']
9
+ ENV['CLASS'].constantize.dynamo_table.create(options)
10
+ end
11
+
12
+ task :destroy => :environment do
13
+ raise "expected usage: rake ddb:destroy CLASS=User" unless ENV['CLASS']
14
+ options = {}
15
+ options.merge!(:table_name => ENV['TABLE']) if ENV['TABLE']
16
+ ENV['CLASS'].constantize.dynamo_table.delete(options)
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  module Toy
2
2
  module Dynamo
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/toy/dynamo.rb CHANGED
@@ -2,6 +2,7 @@ require "toy/dynamo/version"
2
2
  require "toy/dynamo/adapter"
3
3
  require "toy/dynamo/schema"
4
4
  require "toy/dynamo/table"
5
+ require "toy/dynamo/tasks"
5
6
  require "toy/dynamo/querying"
6
7
  require "toy/dynamo/response"
7
8
  require "toy/dynamo/persistence"
data/toy-dynamo.gemspec CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
22
 
24
23
  spec.add_dependency 'adapter', '~> 0.7.0'
24
+ spec.add_dependency 'rake'
25
25
  spec.add_dependency 'aws-sdk', '~> 1.9'
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toy-dynamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cary Dunn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-16 00:00:00.000000000 Z
11
+ date: 2013-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -25,33 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: adapter
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
33
+ version: 0.7.0
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.7.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: adapter
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 0.7.0
47
+ version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
- version: 0.7.0
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: aws-sdk
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -93,6 +93,7 @@ files:
93
93
  - lib/toy/dynamo/response.rb
94
94
  - lib/toy/dynamo/schema.rb
95
95
  - lib/toy/dynamo/table.rb
96
+ - lib/toy/dynamo/tasks.rb
96
97
  - lib/toy/dynamo/version.rb
97
98
  - toy-dynamo.gemspec
98
99
  homepage: https://github.com/cdunn/toy-dynamo