Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AWS::DynamoDB::Errors::ResourceInUseException: Duplicate table name Error #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/dynamoid/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def create_table(options = {})
#
# @since 0.2.0
def table_exists?(table_name)
Dynamoid::Adapter.tables ? Dynamoid::Adapter.tables.include?(table_name) : false
Dynamoid::Adapter.tables = Dynamoid::Adapter.list_tables unless Dynamoid::Adapter.tables
Dynamoid::Adapter.tables.include?(table_name)
end

def from_database(attrs = {})
Expand Down
47 changes: 27 additions & 20 deletions spec/dynamoid/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@
@address = Address.new
end

context 'without AWS keys' do
unless ENV['ACCESS_KEY'] && ENV['SECRET_KEY']
before do
Dynamoid::Adapter.delete_table(Address.table_name) if Dynamoid::Adapter.list_tables.include?(Address.table_name)
context '.create_table' do
before do
if Dynamoid::Adapter.list_tables.include?(Address.table_name)
Dynamoid::Adapter.delete_table(Address.table_name)
Dynamoid::Adapter.reconnect!
end
end

it 'creates a table' do
Address.create_table(:table_name => Address.table_name)
it 'creates a table' do
Address.create_table(:table_name => Address.table_name)

Dynamoid::Adapter.list_tables.should include 'dynamoid_tests_addresses'
end
Dynamoid::Adapter.list_tables.should include Address.table_name
end

it 'checks if a table already exists' do
Address.create_table(:table_name => Address.table_name)
it 'checks if a table already exists' do
Address.create_table(:table_name => Address.table_name)

Address.table_exists?(Address.table_name).should be_true
Address.table_exists?('crazytable').should be_false
end
Address.table_exists?(Address.table_name).should be_true
Address.table_exists?('crazytable').should be_false
end

it 'should not create when table already exists and Dynamoid::Adapter.tables is nil' do
Address.create_table(:table_name => Address.table_name)
Dynamoid::Adapter.tables = nil
expect{Address.create_table(:table_name => Address.table_name)}.not_to raise_error
end
end

Expand All @@ -33,25 +40,25 @@

Dynamoid::Adapter.read("dynamoid_tests_addresses", @address.id)[:id].should == @address.id
end

it 'prevents concurrent writes to tables with a lock_version' do
@address.save!
a1 = @address
a2 = Address.find(@address.id)

a1.city = 'Seattle'
a2.city = 'San Francisco'

a1.save!
expect { a2.save! }.to raise_exception(Dynamoid::Errors::ConditionalCheckFailedException)
end

configured_with 'partitioning' do
it 'raises an error when attempting to use optimistic locking' do
expect { address.save! }.to raise_exception
end
end

it 'assigns itself an id on save only if it does not have one' do
@address.id = 'test123'
@address.save
Expand Down Expand Up @@ -256,12 +263,12 @@
end
}.to raise_error(Dynamoid::Errors::ConditionalCheckFailedException)
end

it 'prevents concurrent saves to tables with a lock_version' do
@address.save!
a2 = Address.find(@address.id)
a2.update! { |a| a.set(:city => "Chicago") }

expect do
@address.city = "Seattle"
@address.save!
Expand Down