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

Create a table including TTL setting with :table_name keyword argument #579

Merged
merged 1 commit into from Aug 13, 2022
Merged

Create a table including TTL setting with :table_name keyword argument #579

merged 1 commit into from Aug 13, 2022

Conversation

ta1kt0me
Copy link
Contributor

Thank you for your maintenance.
This PR resolves an error that occurs when it calls .create_table with table_name keyword for a Model with TTL set.

A simple reproduced code is below,

require 'dynamoid'

Aws.config.update(
  region: 'us-west-2',
  credentials: Aws::Credentials.new('sample', 'sample')
)

Dynamoid.configure do |config|
  config.namespace = 'dynamoid_app_development'
  config.endpoint = 'http://127.0.0.1:8000'
  config.region = 'us-west-2'
end

Dynamoid.adapter.list_tables.each do |table|
  Dynamoid.adapter.delete_table(table)
end

Dynamoid.adapter.tables.clear

class User
  include Dynamoid::Document

  table expires: { field: :ttl, after: 60 }

  field :ttl, :integer
end

User.create_table(table_name: 'dynamoid_app_test_users')
# => Cannot do operations on a non-existent table (Aws::DynamoDB::Errors::ResourceNotFoundException)

# or

User.create_table
User.create_table(table_name: 'dynamoid_app_test_users')
# => TimeToLive is already enabled (Aws::DynamoDB::Errors::ValidationException)

This is because when setting TTL with .create_table, the table name based on Dynamoid.config.namespace is used instead of the keyword argument :table_name. In this case, changing to use the value of :table_name would solve this problem.

My practical use case is to create tables during development using the db:create hook in Ruby on Rails. I need to create a table for development and a table for testing during development, and call .create_table for each environment. The :table_name keyword argument is required to create the table for testing.

@andrykonchin andrykonchin merged commit 3668fcf into Dynamoid:master Aug 13, 2022
@andrykonchin
Copy link
Member

Thank you for the fix! Looks good.

@ta1kt0me ta1kt0me deleted the update_ttl_with_table_name branch August 15, 2022 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants