Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

Using Ruby on Rails to interact with Amazon Aurora DSQL

フォーカスモード
Using Ruby on Rails to interact with Amazon Aurora DSQL - Amazon Aurora DSQL
このページはお客様の言語に翻訳されていません。 翻訳のリクエスト

Amazon Aurora DSQL is provided as a Preview service. To learn more, see Betas and Previews in the AWS Service Terms.

Amazon Aurora DSQL is provided as a Preview service. To learn more, see Betas and Previews in the AWS Service Terms.

This section describes how how to use Ruby on Rails to interact with Aurora DSQL.

Before you begin, make sure that you have completed the following prerequisites.

Install a connection to Aurora DSQL

Aurora DSQL uses IAM as authentication to establish a connection. You can't provide a password directly to rails through the configuration in the {root-directory}/config/database.yml file. Instead, use the aws_rds_iam adapter to use an authentication token to connect to Aurora DSQL. The steps below demonstrate how to do so.

Create a file named {app root directory}/config/initializers/adapter.rb with the following content.

PG::AWS_RDS_IAM.auth_token_generators.add :dsql do DsqlAuthTokenGenerator.new end require "aws-sigv4" require 'aws-sdk-dsql' # This is our custom DB auth token generator # use the ruby sdk to generate token instead. class DsqlAuthTokenGenerator def call(host:, port:, user:) region = "us-east-1" credentials = Aws::SharedCredentials.new() token_generator = Aws::DSQL::AuthTokenGenerator.new({ :credentials => credentials }) # The token expiration time is optional, and the default value 900 seconds # if you are not logging in as admin, use generate_db_connect_auth_token instead token = token_generator.generate_db_connect_admin_auth_token({ :endpoint => host, :region => region }) end end # Monkey-patches to disable unsupported features require "active_record/connection_adapters/postgresql/schema_statements" module ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements # Aurora DSQL does not support setting min_messages in the connection parameters def client_min_messages=(level); end end require "active_record/connection_adapters/postgresql_adapter" class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter def set_standard_conforming_strings; end # Aurora DSQL does not support running multiple DDL or DDL + DML statements in the same transaction def supports_ddl_transactions? false end end

Create the following configuration in the {app root directory}/config/database.yml file. The following is an example configuration. You might create a similar configuration for testing purposes or production databases. This configuration automatically creates a new authentication token so you can connect to your database.

development: <<: *default database: postgres # The specified database role being used to connect to PostgreSQL. # To create additional roles in PostgreSQL see `$ createuser --help`. # When left blank, PostgreSQL will use the default role. This is # the same name as the operating system user running Rails. username: <postgres username> # eg: admin or other postgres users # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have # domain sockets, so uncomment these lines. # host: localhost # Set to Aurora DSQL cluster endpoint # host: <clusterId>.dsql.<region>.on.aws host: <cluster endpoint> # prefer verify-full for production usecases sslmode: require # Remember that we defined dsql token generator in the `{app root directory}/config/initializers/adapter.rb` # We are providing it as the token generator to the adapter here. aws_rds_iam_auth_token_generator: dsql advisory_locks: false prepared_statements: false

Now you can create a data model. The following example creates a model and a migration file. Change the the model file to explicitly define the primary key of the table.

# Execute in the app root directory bin/rails generate model Owner name:string city:string telephone:string
Note

Unlike postgres, Aurora DSQL creates a primary key index by including all columns of the table. This means that active record to search uses all columns of the table instead of just the primary key. So So the <Entity>.find(<primary key>) won't work because the active record tries to search by using all columns in the primary key index.

To make active record search only using primary keys, set the primary key column explicitly in the model.

class Owner < ApplicationRecord self.primary_key = "id" end

Generate the schema from the model files in db/migrate.

bin/rails db:migrate

Finally, disable the plpgsql extension by modifying the {app root directory}/db/schema.rb. In order to disable the plpgsql extension, remove the enable_extension "plgsql" line.

CRUD examples

You can now perform CRUD operations on your database. Run the following example to add owner data to your database.

owner = Owner.new(name: "John Smith", city: "Seattle", telephone: "123-456-7890") owner.save owner

Run the following example to retrieve the data.

Owner.find("<owner id>")

To update the data, use the following example.

Owner.find("<owner id>").update(telephone: "123-456-7891")

Finally, you can delete the data.

Owner.find("<owner id>").destroy

このページの内容

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.