Skip to content

Commit 5cdfcc9

Browse files
committed
Adds a configuration flag to enable create_table on save
1 parent 6d757f1 commit 5cdfcc9

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

lib/dynamoid/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
#

lib/dynamoid/persistence.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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

spec/dynamoid/persistence_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)