diff --git a/README.md b/README.md index 3a80450..1c9226b 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Each wrapper's documentation is grouped into the same sections: | Section | What's in it | |--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| **Overview** | What the wrapper is and how to install it. | +| **Overview** | What the wrapper is, how to install it, and one complete program that connects and runs a query. The page to read first. | | **Set up by Scenario** | An interactive builder — answer a few questions about your application and get a working configuration. | | **Using Plugins** | One page per feature plugin (Failover, IAM Authentication, AWS Secrets Manager, Blue/Green, KMS Encryption, and so on), describing what the plugin does and every parameter it accepts. | | **Shared Configuration** | Cross-cutting settings — TLS/SSL, AWS credentials resolution, host selection strategies — that more than one plugin relies on. | diff --git a/content/failover.yaml b/content/failover.yaml index cf647ae..b631e13 100644 --- a/content/failover.yaml +++ b/content/failover.yaml @@ -70,11 +70,22 @@ wrappers: # optional heading and an ordered list of blocks; a block is either prose # (`text`) or a fenced code sample (`code` + `lang`). # - # Active Record comes first because the adapters are the wrapper's primary - # integration: changing the adapter name is the whole change. The gem name - # matches its entry point file, so Bundler's default require loads it. + # Active Record leads because most Ruby users arrive through it: the adapters + # are the wrapper's primary integration and changing the adapter name is the + # whole change. The driver-direct path follows under an unnumbered "Or ..." + # heading, which is where an alternative goes in this style. + # + # The driver-direct block is a whole working program, not a fragment. A + # reader who copies it has a process that connects, queries and exits, with + # no gaps to fill from elsewhere on the site: the exact gem name, the exact + # require path, the constructor, the query call, and the teardown are all + # present. Fragments cost a reader (and an agent reading on their behalf) an + # edit-and-rerun loop per gap, which is the most expensive thing a + # getting-started page can do. The Active Record path cannot be shown that + # way because it needs a Rails application around it, so the two paths are + # ordered by who the reader is, not by what fits in one file. gettingStarted: - note: "Requires Ruby 3.3 or later. Tested with Active Record 7.2, 8.0, and 8.1, `pg` 1.6.3 or later, and `mysql2` 0.5.7 or later." + note: "Requires Ruby 3.3 or later. The gem is `aws_advanced_ruby_driver_wrapper` on RubyGems. Tested with Active Record 7.2, 8.0, and 8.1, `pg` 1.6.3 or later, and `mysql2` 0.5.7 or later." steps: - title: "1. Add the gem" blocks: @@ -87,17 +98,67 @@ wrappers: code: | bundle install - text: >- - Bundler requires the gem for you (in a plain Ruby script, add - `require 'aws_advanced_ruby_driver_wrapper'`). That single require - registers the Active Record adapters and makes the connection classes, - error classes, and other public API available for direct use. + Without Bundler, `gem install aws_advanced_ruby_driver_wrapper pg` + does the same thing. + # The house pattern for "this needs another gem" is a warning with + # its own snippet, as on the IAM and Secrets Manager pages, rather + # than commented lines inside the main Gemfile. + - text: | + :::warning + Which extra gems your Gemfile needs depends on your Ruby version, your platform, and whether you are using Active Record: + + | Version | Using Active Record | Extra gems | Reason | + | --- | --- | --- | --- | + | Ruby 3.3 | Either | None | `base64`, `bigdecimal` and `logger` are all still default gems | + | Ruby 3.4 | No | `base64`, `bigdecimal` | Ruby 3.4 removed both from the default gems | + | Ruby 3.4 | Yes | None | Active Record declares both itself | + | Ruby 4.0 or later | No | `base64`, `bigdecimal`, `logger` | Ruby 4.0 removed `logger` as well | + | Ruby 4.0 or later | Yes | None | Active Record declares all three itself | + | Ruby 4.0 or later, on Windows with `mysql2` | Either | `fiddle` | `mysql2` loads `libmysql.dll` through `fiddle` without declaring it, and neither does Active Record | + + A bundle missing any of them raises `LoadError` on the first require. + ::: - title: "2. Point Active Record at your cluster" blocks: - text: >- The wrapper registers two adapters, `aws_postgresql` and `aws_mysql2`, - which are drop-in replacements for `postgresql` and `mysql2`. Change the - adapter name and point the host at your cluster endpoint: + which are drop-in replacements for `postgresql` and `mysql2`. + # Without this, Active Record raises AdapterNotFound and names only + # its own built-in adapters, which does not hint that a require is + # missing. Bundler covers the Rails case; a plain script does not. + - text: >- + Bundler requires the gem for you. In a plain Ruby script, add + `require 'aws_advanced_ruby_driver_wrapper'` first: that single require + is what registers both adapters, and without it Active Record reports + `aws_postgresql` as a nonexistent adapter. + - text: >- + If you are starting without a Rails application, this is a complete + program. Save it as `main.rb` and run it: + - lang: ruby + code: | + # main.rb + require 'active_record' + require 'aws_advanced_ruby_driver_wrapper' + + ActiveRecord::Base.establish_connection( + adapter: 'aws_postgresql', + host: 'my-cluster.cluster-xyz.us-east-1.rds.amazonaws.com', + database: 'mydb', + username: '', + password: '', + wrapper_plugins: 'failover' + ) + + puts ActiveRecord::Base.connection.select_value('SELECT version()') + - lang: bash + code: | + bundle exec ruby main.rb + - text: >- + In a Rails application the same keys go in `config/database.yml` + instead, and Bundler has already done the require. The adapter name + is the only key that changes; leave the host as whichever endpoint + you already connect to: - lang: yaml code: | # config/database.yml @@ -108,13 +169,10 @@ wrappers: username: password: - text: >- - That is the whole change — no application code moves. When - `wrapper_plugins` is not set, the wrapper loads `failover` and - `initial_connection`, so connections made through this adapter already - recover when the cluster promotes a new writer. Enable more by adding - wrapper properties alongside the Active Record keys. Setting - `wrapper_plugins` replaces the default list rather than adding to it, so - list the defaults you want to keep: + That is the whole change — no application code moves, and + connections made through this adapter already recover when the + cluster promotes a new writer. Wrapper properties go alongside the + Active Record keys, under the same connection: - lang: yaml code: | production: @@ -123,6 +181,10 @@ wrappers: wrapper_plugins: failover,initial_connection,iam iam_region: us-east-1 failover_timeout_sec: 300.0 # seconds + - text: >- + `wrapper_plugins` is optional — leaving it out loads + `failover,initial_connection`. Setting it replaces the default list + rather than adding to it, so list the defaults you want to keep. - text: >- Active Record keeps its own connection pool and its reading and writing roles; the wrapper's plugins run underneath both. @@ -134,16 +196,27 @@ wrappers: (`aws_postgresql`/`aws_mysql2`), a custom type registered for one is shared with the other, because both resolve under the same underlying adapter key. ::: + - text: | + :::note Keeping the password out of your source + Read it from the environment with `ENV.fetch('PGPASSWORD')`, or have + AWS manage it: add `secrets_manager` to `wrapper_plugins` and drop the + `password:` key, as described on + [AWS Secrets Manager Plugin](using-plugins/aws-secrets-manager). For a + database user with no password at all, consider using `iam` instead; see + [IAM Authentication Plugin](using-plugins/iam-authentication). + ::: - title: "Or connect with the driver directly" blocks: - text: >- - If you use the `pg` or `mysql2` gem without Active Record, the wrapper - provides connection classes that stand in for `PG::Connection` and - `Mysql2::Client`: + Without Active Record, the wrapper provides connection classes that + stand in for `PG::Connection` and `Mysql2::Client`. This is a complete + program; save it as `main.rb` and run it: - lang: ruby code: | - # PostgreSQL (pg) + # main.rb + require 'aws_advanced_ruby_driver_wrapper/postgresql' + conn = AwsAdvancedRubyDriverWrapper::WrapperPgConnection.new( host: 'my-cluster.cluster-xyz.us-east-1.rds.amazonaws.com', dbname: 'mydb', @@ -152,27 +225,37 @@ wrappers: wrapper_plugins: 'failover', failover_timeout_sec: 300.0 ) - - lang: ruby + + begin + result = conn.exec('SELECT version()') + puts result.first['version'] + ensure + conn.close + end + - lang: bash code: | - # MySQL (mysql2) - conn = AwsAdvancedRubyDriverWrapper::WrapperMysql2Client.new( - host: 'my-cluster.cluster-xyz.us-east-1.rds.amazonaws.com', - database: 'mydb', - username: '', - password: '', - wrapper_plugins: 'failover', - failover_timeout_sec: 300.0 - ) + bundle exec ruby main.rb - text: >- - The calls your code already makes on the driver connection keep working, - and the plugins act on each one. - + For PostgreSQL, `require 'aws_advanced_ruby_driver_wrapper/postgresql'` + and use `AwsAdvancedRubyDriverWrapper::WrapperPgConnection`, which + stands in for `PG::Connection` and takes `dbname:` and `user:`. + For MySQL, `require 'aws_advanced_ruby_driver_wrapper/mysql'` and use + `AwsAdvancedRubyDriverWrapper::WrapperMysql2Client`, which stands in + for `Mysql2::Client` and takes `database:` and `username:`. Either + way the calls your code already makes keep working, and the plugins + act on each one. - title: "Next" blocks: + - text: >- + The wrapper supports other plugins and wrapper properties. For + further information see the plugin pages, starting with + [Enhanced Failover Plugin](enhanced-failover). Every wrapper property + the examples did not use is listed with its type, default and unit on + the plugin page that reads it. To check a combination before shipping, + see [Plugin Compatibility](compatibility). - text: >- Not sure which plugins you need? [Set up by Scenario](setup) builds a - configuration from a few questions about your application. To check a - combination before shipping, see [Plugin Compatibility](compatibility). + configuration from a few questions about your application. parameters: - concept: failoverMode diff --git a/site/docs-ruby/overview.md b/site/docs-ruby/overview.md index 13c7535..537786c 100644 --- a/site/docs-ruby/overview.md +++ b/site/docs-ruby/overview.md @@ -21,7 +21,7 @@ will follow your choice where an equivalent page exists. ## Getting started -Requires Ruby 3.3 or later. Tested with Active Record 7.2, 8.0, and 8.1, `pg` 1.6.3 or later, and `mysql2` 0.5.7 or later. +Requires Ruby 3.3 or later. The gem is `aws_advanced_ruby_driver_wrapper` on RubyGems. Tested with Active Record 7.2, 8.0, and 8.1, `pg` 1.6.3 or later, and `mysql2` 0.5.7 or later. ### 1. Add the gem @@ -35,11 +35,54 @@ gem 'pg' # or: gem 'mysql2' bundle install ``` -Bundler requires the gem for you (in a plain Ruby script, add `require 'aws_advanced_ruby_driver_wrapper'`). That single require registers the Active Record adapters and makes the connection classes, error classes, and other public API available for direct use. +Without Bundler, `gem install aws_advanced_ruby_driver_wrapper pg` does the same thing. + +:::warning +Which extra gems your Gemfile needs depends on your Ruby version, your platform, and whether you are using Active Record: + +| Version | Using Active Record | Extra gems | Reason | +| --- | --- | --- | --- | +| Ruby 3.3 | Either | None | `base64`, `bigdecimal` and `logger` are all still default gems | +| Ruby 3.4 | No | `base64`, `bigdecimal` | Ruby 3.4 removed both from the default gems | +| Ruby 3.4 | Yes | None | Active Record declares both itself | +| Ruby 4.0 or later | No | `base64`, `bigdecimal`, `logger` | Ruby 4.0 removed `logger` as well | +| Ruby 4.0 or later | Yes | None | Active Record declares all three itself | +| Ruby 4.0 or later, on Windows with `mysql2` | Either | `fiddle` | `mysql2` loads `libmysql.dll` through `fiddle` without declaring it, and neither does Active Record | + +A bundle missing any of them raises `LoadError` on the first require. +::: + ### 2. Point Active Record at your cluster -The wrapper registers two adapters, `aws_postgresql` and `aws_mysql2`, which are drop-in replacements for `postgresql` and `mysql2`. Change the adapter name and point the host at your cluster endpoint: +The wrapper registers two adapters, `aws_postgresql` and `aws_mysql2`, which are drop-in replacements for `postgresql` and `mysql2`. + +Bundler requires the gem for you. In a plain Ruby script, add `require 'aws_advanced_ruby_driver_wrapper'` first: that single require is what registers both adapters, and without it Active Record reports `aws_postgresql` as a nonexistent adapter. + +If you are starting without a Rails application, this is a complete program. Save it as `main.rb` and run it: + +```ruby +# main.rb +require 'active_record' +require 'aws_advanced_ruby_driver_wrapper' + +ActiveRecord::Base.establish_connection( + adapter: 'aws_postgresql', + host: 'my-cluster.cluster-xyz.us-east-1.rds.amazonaws.com', + database: 'mydb', + username: '', + password: '', + wrapper_plugins: 'failover' +) + +puts ActiveRecord::Base.connection.select_value('SELECT version()') +``` + +```bash +bundle exec ruby main.rb +``` + +In a Rails application the same keys go in `config/database.yml` instead, and Bundler has already done the require. The adapter name is the only key that changes; leave the host as whichever endpoint you already connect to: ```yaml # config/database.yml @@ -51,7 +94,7 @@ production: password: ``` -That is the whole change — no application code moves. When `wrapper_plugins` is not set, the wrapper loads `failover` and `initial_connection`, so connections made through this adapter already recover when the cluster promotes a new writer. Enable more by adding wrapper properties alongside the Active Record keys. Setting `wrapper_plugins` replaces the default list rather than adding to it, so list the defaults you want to keep: +That is the whole change — no application code moves, and connections made through this adapter already recover when the cluster promotes a new writer. Wrapper properties go alongside the Active Record keys, under the same connection: ```yaml production: @@ -62,6 +105,8 @@ production: failover_timeout_sec: 300.0 # seconds ``` +`wrapper_plugins` is optional — leaving it out loads `failover,initial_connection`. Setting it replaces the default list rather than adding to it, so list the defaults you want to keep. + Active Record keeps its own connection pool and its reading and writing roles; the wrapper's plugins run underneath both. :::note Mixing wrapped and standard adapters in one application @@ -73,12 +118,24 @@ with the other, because both resolve under the same underlying adapter key. ::: +:::note Keeping the password out of your source +Read it from the environment with `ENV.fetch('PGPASSWORD')`, or have +AWS manage it: add `secrets_manager` to `wrapper_plugins` and drop the +`password:` key, as described on +[AWS Secrets Manager Plugin](using-plugins/aws-secrets-manager). For a +database user with no password at all, consider using `iam` instead; see +[IAM Authentication Plugin](using-plugins/iam-authentication). +::: + + ### Or connect with the driver directly -If you use the `pg` or `mysql2` gem without Active Record, the wrapper provides connection classes that stand in for `PG::Connection` and `Mysql2::Client`: +Without Active Record, the wrapper provides connection classes that stand in for `PG::Connection` and `Mysql2::Client`. This is a complete program; save it as `main.rb` and run it: ```ruby -# PostgreSQL (pg) +# main.rb +require 'aws_advanced_ruby_driver_wrapper/postgresql' + conn = AwsAdvancedRubyDriverWrapper::WrapperPgConnection.new( host: 'my-cluster.cluster-xyz.us-east-1.rds.amazonaws.com', dbname: 'mydb', @@ -87,23 +144,24 @@ conn = AwsAdvancedRubyDriverWrapper::WrapperPgConnection.new( wrapper_plugins: 'failover', failover_timeout_sec: 300.0 ) + +begin + result = conn.exec('SELECT version()') + puts result.first['version'] +ensure + conn.close +end ``` -```ruby -# MySQL (mysql2) -conn = AwsAdvancedRubyDriverWrapper::WrapperMysql2Client.new( - host: 'my-cluster.cluster-xyz.us-east-1.rds.amazonaws.com', - database: 'mydb', - username: '', - password: '', - wrapper_plugins: 'failover', - failover_timeout_sec: 300.0 -) +```bash +bundle exec ruby main.rb ``` -The calls your code already makes on the driver connection keep working, and the plugins act on each one. +For PostgreSQL, `require 'aws_advanced_ruby_driver_wrapper/postgresql'` and use `AwsAdvancedRubyDriverWrapper::WrapperPgConnection`, which stands in for `PG::Connection` and takes `dbname:` and `user:`. For MySQL, `require 'aws_advanced_ruby_driver_wrapper/mysql'` and use `AwsAdvancedRubyDriverWrapper::WrapperMysql2Client`, which stands in for `Mysql2::Client` and takes `database:` and `username:`. Either way the calls your code already makes keep working, and the plugins act on each one. ### Next -Not sure which plugins you need? [Set up by Scenario](setup) builds a configuration from a few questions about your application. To check a combination before shipping, see [Plugin Compatibility](compatibility). +The wrapper supports other plugins and wrapper properties. For further information see the plugin pages, starting with [Enhanced Failover Plugin](enhanced-failover). Every wrapper property the examples did not use is listed with its type, default and unit on the plugin page that reads it. To check a combination before shipping, see [Plugin Compatibility](compatibility). + +Not sure which plugins you need? [Set up by Scenario](setup) builds a configuration from a few questions about your application.