File tree Expand file tree Collapse file tree 3 files changed +52
-1
lines changed Expand file tree Collapse file tree 3 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ module Config
5959 option :http_idle_timeout , default : nil # - default: 5
6060 option :http_open_timeout , default : nil # - default: 15
6161 option :http_read_timeout , default : nil # - default: 60
62+ option :create_table_on_save , default : true
6263
6364 # The default logger for Dynamoid: either the Rails logger or just stdout.
6465 #
Original file line number Diff line number Diff line change @@ -507,7 +507,10 @@ def persisted?
507507 # @return [true|false] Whether saving successful or not
508508 # @since 0.2.0
509509 def save ( options = { } )
510- self . class . create_table ( sync : true )
510+ if Dynamoid . config . create_table_on_save
511+ self . class . create_table ( sync : true )
512+ end
513+
511514 create_or_update = new_record? ? :create : :update
512515
513516 run_callbacks ( :save ) do
Original file line number Diff line number Diff line change @@ -2293,6 +2293,53 @@ def around_update_callback
22932293 end . not_to change { obj . class . count }
22942294 end
22952295
2296+ context 'when disable_create_table_on_save is false' do
2297+ before do
2298+ Dynamoid . configure do |config |
2299+ @original_create_table_on_save = config . create_table_on_save
2300+ config . create_table_on_save = false
2301+ end
2302+ end
2303+
2304+ after do
2305+ Dynamoid . configure do |config |
2306+ config . create_table_on_save = @original_create_table_on_save
2307+ end
2308+ end
2309+
2310+ it 'raises Aws::DynamoDB::Errors::ResourceNotFoundException error' do
2311+ model = klass . new
2312+
2313+ expect ( klass ) . not_to receive ( :create_table )
2314+
2315+ expect { model . save! } . to raise_error ( Aws ::DynamoDB ::Errors ::ResourceNotFoundException )
2316+ end
2317+ end
2318+
2319+ context 'when disable_create_table_on_save is false and the table exists' do
2320+ before do
2321+ Dynamoid . configure do |config |
2322+ @original_create_table_on_save = config . create_table_on_save
2323+ config . create_table_on_save = false
2324+ end
2325+ klass . create_table
2326+ end
2327+
2328+ after do
2329+ Dynamoid . configure do |config |
2330+ config . create_table_on_save = @original_create_table_on_save
2331+ end
2332+ end
2333+
2334+ it 'persists the model' do
2335+ obj = klass . new ( name : 'John' )
2336+ obj . save
2337+
2338+ expect ( klass . exists? ( obj . id ) ) . to eq true
2339+ expect ( klass . find ( obj . id ) . name ) . to eq 'John'
2340+ end
2341+ end
2342+
22962343 describe 'partition key value' do
22972344 it 'generates "id" for new model' do
22982345 obj = klass . new
You can’t perform that action at this time.
0 commit comments