dynomite 1.0.9 → 1.1.0

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
  SHA256:
3
- metadata.gz: 071e6c1ee0bb76726ab666edb5ea866bcd290b310c5e2c3e83f2fa57cca9ca2e
4
- data.tar.gz: 10d559b3073cf2a325aeb9861b80b0cb62c872881f3de96a1eecebc0bc5f1a53
3
+ metadata.gz: b15b33b315b312ab9d3b277d49b91601a2a873de2f381a2cc12085b30f784df0
4
+ data.tar.gz: 46636c01df3e78e6470e01b81fb829e1a379d3193dd276b0905ebd1af36ff94d
5
5
  SHA512:
6
- metadata.gz: 4cadb8ab579bc8097355dbeedd107c3507f27862698a9caacce2fbf0e138d8a93c0855231f6ceb89d5700e85440395324922bf5b05351d4fa2b4585e50b5d9e6
7
- data.tar.gz: 5063b5f792153f649a447fc0676dd74877f1c070ac3fe820f912bf4d596d0218060c443f67e9abe5d452c9c0b52ffe5c6b02bbe29e3db750f562fc5650494cf4
6
+ metadata.gz: 50bc0730c3dad8654ae6323c98984e8c492d8d3bbccd9bf119ca1454ed50e9f4d183f51a822143e2e0bfa88902807d3d52497f913b736c0f126a748d065c0713
7
+ data.tar.gz: f54b0f900e63317ea9dfc9cc68ef6ccda244939f146497d5d783dcf1abc24526b7da0334dbcc67fd5d05a8c0cdd167b324e4b247c6cfe447a9d8faf83dfddb12
data/CHANGELOG.md CHANGED
@@ -3,6 +3,10 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [1.1.0]
7
+ - Merge pull request #5 from tongueroo/fix-index-creation
8
+ - fix index creation dsl among other things
9
+
6
10
  ## [1.0.9]
7
11
  - allow item.replace(hash) to work
8
12
  - Merge pull request #3 from mveer99/patch-1 Update comments: Fixed typo in project_name
@@ -3,14 +3,19 @@ class CreateCommentsMigration < Dynomite::Migration
3
3
  create_table :comments do |t|
4
4
  t.partition_key "post_id:string" # required
5
5
  t.sort_key "created_at:string" # optional
6
- t.provisioned_throughput(5) # sets both read and write, defaults to 5 when not set
6
+ t.provisioned_throughput(1) # sets both read and write, defaults to 5 when not set
7
7
 
8
8
  t.lsi do |i|
9
- i.partition_key "user_id:string"
10
- i.sort_key "updated_at:string" # optional
11
-
12
- i.provisioned_throughput(10)
9
+ i.partition_key "post_id:string"
10
+ i.sort_key "updated_at:string" # required for lsi
11
+ i.provisioned_throughput(2)
13
12
  end
13
+
14
+ # t.gsi do |i|
15
+ # i.partition_key "post_id:string"
16
+ # i.sort_key "deleted_at:string" # optional for gsi
17
+ # i.provisioned_throughput(2)
18
+ # end
14
19
  end
15
20
  end
16
21
  end
@@ -20,13 +25,12 @@ class UpdateCommentsMigration < Dynomite::Migration
20
25
  update_table :comments do |t|
21
26
  t.gsi(:create) do |i|
22
27
  i.partition_key "post_id:string"
23
- i.sort_key "updated_at:string" # optional
24
-
25
- i.provisioned_throughput(10)
28
+ i.sort_key "flagged_at:string" # optional for gsi
29
+ i.provisioned_throughput(3)
26
30
  end
27
31
 
28
32
  t.gsi(:update, "update-me-index") do |i|
29
- i.provisioned_throughput(10)
33
+ i.provisioned_throughput(5)
30
34
  end
31
35
 
32
36
  t.gsi(:delete, "delete-me-index")
@@ -35,7 +35,7 @@ class Dynomite::Migration
35
35
  # i.partition_key = "category:string"
36
36
  # i.sort_key = "created_at:string" # optional
37
37
  # end
38
- def gsi(action, index_name=nil, &block)
38
+ def gsi(action=:create, index_name=nil, &block)
39
39
  gsi_index = GlobalSecondaryIndex.new(action, index_name, &block)
40
40
  @gsi_indexes << gsi_index # store @gsi_index for the parent Dsl to use
41
41
  end
@@ -50,7 +50,7 @@ class Dynomite::Migration
50
50
  lsi_index = LocalSecondaryIndex.new(index_name, &block)
51
51
  @lsi_indexes << lsi_index # store @lsi_index for the parent Dsl to use
52
52
  end
53
- alias_method :local_secondary_index, :gsi
53
+ alias_method :local_secondary_index, :lsi
54
54
 
55
55
  def evaluate
56
56
  return if @evaluated
@@ -74,6 +74,9 @@ class Dynomite::Migration
74
74
  end
75
75
 
76
76
  def params_create_table
77
+ merge_lsi_attribute_definitions!
78
+ merge_gsi_attribute_definitions!
79
+
77
80
  params = {
78
81
  table_name: namespaced_table_name,
79
82
  key_schema: @key_schema,
@@ -81,14 +84,31 @@ class Dynomite::Migration
81
84
  provisioned_throughput: @provisioned_throughput
82
85
  }
83
86
 
84
- params[:local_secondary_index_creates] = local_secondary_index_creates unless @lsi_indexes.empty?
87
+ params[:local_secondary_indexes] = lsi_secondary_index_creates unless @lsi_indexes.empty?
88
+ params[:global_secondary_indexes] = gsi_secondary_index_creates unless @gsi_indexes.empty?
89
+ params
90
+ end
91
+
92
+ def params_update_table
93
+ merge_gsi_attribute_definitions!
94
+
95
+ params = {
96
+ table_name: namespaced_table_name,
97
+ attribute_definitions: @attribute_definitions,
98
+ # update table take values only some values for the "parent" table
99
+ # no key_schema, update_table does not handle key_schema for the "parent" table
100
+ }
101
+ # only set "parent" table provisioned_throughput if user actually invoked
102
+ # it in the dsl
103
+ params[:provisioned_throughput] = @provisioned_throughput if @provisioned_throughput_set_called
104
+ params[:global_secondary_index_updates] = global_secondary_index_updates
85
105
  params
86
106
  end
87
107
 
88
108
  # Goes thorugh all the lsi_indexes that have been built up in memory.
89
109
  # Find the lsi object that creates an index and then grab the
90
110
  # attribute_definitions from it.
91
- def lsi_create_attribute_definitions
111
+ def merge_lsi_attribute_definitions!
92
112
  lsi = @lsi_indexes.first # DynamoDB only supports adding one index at a time anyway. The reason @lsi_indexes is an Array is because we're sharing the same class code for LSI and GSI
93
113
  if lsi
94
114
  lsi.evaluate # force early evaluate since we need the params to
@@ -100,7 +120,7 @@ class Dynomite::Migration
100
120
  else
101
121
  @attribute_definitions
102
122
  end
103
- all_attrs.uniq
123
+ @attribute_definitions = all_attrs.uniq
104
124
  end
105
125
 
106
126
  # maps each lsi to the hash structure expected by dynamodb update_table
@@ -111,40 +131,42 @@ class Dynomite::Migration
111
131
  # { delete: {...} }
112
132
  def lsi_secondary_index_creates
113
133
  @lsi_indexes.map do |lsi|
114
- { lsi.action => lsi.params }
134
+ lsi.params
115
135
  end
116
136
  end
117
137
 
118
- def params_update_table
119
- params = {
120
- table_name: namespaced_table_name,
121
- attribute_definitions: gsi_create_attribute_definitions,
122
- # update table take values only some values for the "parent" table
123
- # no key_schema, update_table does not handle key_schema for the "parent" table
124
- }
125
- # only set "parent" table provisioned_throughput if user actually invoked
126
- # it in the dsl
127
- params[:provisioned_throughput] = @provisioned_throughput if @provisioned_throughput_set_called
128
- params[:global_secondary_index_updates] = global_secondary_index_updates
129
- params
138
+ # maps each lsi to the hash structure expected by dynamodb update_table
139
+ # under the global_secondary_index_updates key:
140
+ #
141
+ # { create: {...} }
142
+ # { update: {...} }
143
+ # { delete: {...} }
144
+ def gsi_secondary_index_creates
145
+ @gsi_indexes.map do |gsi|
146
+ gsi.params
147
+ end
130
148
  end
131
149
 
132
150
  # Goes thorugh all the gsi_indexes that have been built up in memory.
133
151
  # Find the gsi object that creates an index and then grab the
134
152
  # attribute_definitions from it.
135
- def gsi_create_attribute_definitions
136
- gsi = @gsi_indexes.find { |gsi| gsi.action == :create }
153
+ def merge_gsi_attribute_definitions!
154
+ gsi_attrs = []
155
+
156
+ gsi = @gsi_indexes.find { |i| i.action == :create }
137
157
  if gsi
138
158
  gsi.evaluate # force early evaluate since we need the params to
139
159
  # add: gsi_attribute_definitions + gsi_attrs
140
160
  gsi_attrs = gsi.attribute_definitions
141
161
  end
142
- all_attrs = if gsi_attrs
143
- gsi_attribute_definitions + gsi_attrs
144
- else
145
- gsi_attribute_definitions
146
- end
147
- all_attrs.uniq
162
+
163
+ # Merge existing attributes for update table
164
+ all_gsi_attrs = @method_name == :update_table ?
165
+ gsi_attribute_definitions + gsi_attrs :
166
+ gsi_attrs
167
+
168
+ all_attrs = (@attribute_definitions + all_gsi_attrs).uniq
169
+ @attribute_definitions = all_attrs
148
170
  end
149
171
 
150
172
  # maps each gsi to the hash structure expected by dynamodb update_table
@@ -62,7 +62,8 @@ class Dynomite::Migration::Dsl
62
62
  }
63
63
  end
64
64
 
65
- if [:create, :update].include?(@action)
65
+ if self.is_a?(GlobalSecondaryIndex) && [:create, :update].include?(@action)
66
+ # provisioned_throughput is required for gsi index definition
66
67
  params[:provisioned_throughput] = @provisioned_throughput
67
68
  end
68
69
 
@@ -19,11 +19,19 @@ class Dynomite::Migration
19
19
  # Examples:
20
20
  # result = db.create_table(@params)
21
21
  # result = db.update_table(@params)
22
+
23
+ # Leaving this verbose output in here until this DSL is more hardened to help debug
24
+ unless ENV['DYNOMITE_ENV'] == 'test'
25
+ puts "Calling #{@method_name} with params:"
26
+ pp @params
27
+ end
28
+
29
+ return if ENV['DYNOMITE_DRY'] # dry run flag
22
30
  result = db.send(@method_name, @params)
23
31
 
24
32
  puts "DynamoDB Table: #{@table_name} Status: #{result.table_description.table_status}"
25
33
  rescue Aws::DynamoDB::Errors::ServiceError => error
26
- puts "Unable to #{@method_name.to_s.gsub('_',' ')}: #{error.message}"
34
+ puts "Unable to #{@method_name.to_s.gsub('_',' ')}: #{error.message}".colorize(:red)
27
35
  end
28
36
  end
29
37
  end
@@ -1,3 +1,3 @@
1
1
  module Dynomite
2
- VERSION = "1.0.9"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynomite
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-06 00:00:00.000000000 Z
11
+ date: 2019-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport