File tree Expand file tree Collapse file tree 5 files changed +47
-1
lines changed Expand file tree Collapse file tree 5 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -1303,6 +1303,7 @@ Listed below are all configuration options.
1303
1303
* ` write_capacity ` - is used at table or indices creation. Default is 20
1304
1304
(units)
1305
1305
* ` warn_on_scan ` - log warnings when scan table. Default is ` true `
1306
+ * ` error_on_scan ` - raises an error when scan table. Default is ` false `
1306
1307
* ` endpoint ` - if provided, it communicates with the DynamoDB listening
1307
1308
at the endpoint. This is useful for testing with
1308
1309
[ DynamoDB Local] ( http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html )
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ module Config
36
36
option :read_capacity , default : 100
37
37
option :write_capacity , default : 20
38
38
option :warn_on_scan , default : true
39
+ option :error_on_scan , default : false
39
40
option :endpoint , default : nil
40
41
option :identity_map , default : false
41
42
option :timestamps , default : true
Original file line number Diff line number Diff line change @@ -563,7 +563,7 @@ def raw_pages
563
563
if @key_fields_detector . key_present?
564
564
raw_pages_via_query
565
565
else
566
- issue_scan_warning if Dynamoid :: Config . warn_on_scan && ! @where_conditions . empty?
566
+ validate_scan_conditions
567
567
raw_pages_via_scan
568
568
end
569
569
end
@@ -598,6 +598,12 @@ def raw_pages_via_scan
598
598
end
599
599
end
600
600
601
+ def validate_scan_conditions
602
+ raise Dynamoid ::Errors ::ScanProhibited if Dynamoid ::Config . error_on_scan && !@where_conditions . empty?
603
+
604
+ issue_scan_warning if Dynamoid ::Config . warn_on_scan && !@where_conditions . empty?
605
+ end
606
+
601
607
def issue_scan_warning
602
608
Dynamoid . logger . warn 'Queries without an index are forced to use scan and are generally much slower than indexed queries!'
603
609
Dynamoid . logger . warn "You can index this query by adding index declaration to #{ source . to_s . underscore } .rb:"
Original file line number Diff line number Diff line change @@ -100,5 +100,11 @@ def initialize(model_class, attribute_name)
100
100
class SubclassNotFound < Error ; end
101
101
102
102
class Rollback < Error ; end
103
+
104
+ class ScanProhibited < Error
105
+ def initialize ( _msg = nil )
106
+ super ( 'Scan operations prohibited. Modify Dynamoid::Config.error_on_scan to change this behavior.' )
107
+ end
108
+ end
103
109
end
104
110
end
Original file line number Diff line number Diff line change 117
117
end
118
118
end
119
119
120
+ context 'when scans using non-indexed fields and error_on_scan config option is true' do
121
+ before do
122
+ @error_on_scan = Dynamoid ::Config . error_on_scan
123
+ Dynamoid ::Config . error_on_scan = true
124
+ end
125
+
126
+ after do
127
+ Dynamoid ::Config . error_on_scan = @error_on_scan
128
+ end
129
+
130
+ it 'raises an error' do
131
+ expect {
132
+ User . where ( name : 'x' , password : 'password' ) . all
133
+ } . to raise_error ( Dynamoid ::Errors ::ScanProhibited )
134
+ end
135
+ end
136
+
120
137
context 'when doing intentional, full-table scan (query is empty) and warn_on_scan config option is true' do
121
138
before do
122
139
@warn_on_scan = Dynamoid ::Config . warn_on_scan
133
150
User . all
134
151
end
135
152
end
153
+
154
+ context 'when doing intentional, full-table scan (query is empty) and error_on_scan config option is true' do
155
+ before do
156
+ @error_on_scan = Dynamoid ::Config . error_on_scan
157
+ Dynamoid ::Config . error_on_scan = true
158
+ end
159
+
160
+ after do
161
+ Dynamoid ::Config . error_on_scan = @error_on_scan
162
+ end
163
+
164
+ it 'does not raise an error' do
165
+ expect { User . all } . not_to raise_error ( Dynamoid ::Errors ::ScanProhibited )
166
+ end
167
+ end
136
168
end
You can’t perform that action at this time.
0 commit comments