Automatic reconnection on MySQL read-only errors for ActiveRecord's Trilogy adapter.
When a MySQL server switches to read-only mode (e.g., Aurora failover, ProxySQL routing change, RDS Multi-AZ switchover), existing connections receive ER_OPTION_PREVENTS_STATEMENT (1290) on write attempts. This gem translates that error into ActiveRecord::ConnectionFailed, enabling Rails' built-in retry mechanism to transparently reconnect to the new writer.
The gem prepends a thin patch to TrilogyAdapter#translate_exception:
Write attempt → 1290 error → ConnectionFailed → Rails retry → reconnect! → new writer
- SELECT (with
allow_retry: true): Automatically retried and reconnected - INSERT / UPDATE / DELETE: Error propagates to the caller (no double-execution risk)
This is the correct behavior — writes should not be silently retried.
Add to your Gemfile:
gem "activerecord-trilogy-failover"With Rails: No configuration needed. The Railtie automatically patches TrilogyAdapter on load.
Without Rails: Manually prepend the patch:
require "activerecord_trilogy_failover"
ActiveRecord::ConnectionAdapters::TrilogyAdapter.prepend(
ActiveRecordTrilogyFailover::AdapterPatch
)- Ruby >= 3.2
- ActiveRecord >= 7.1
- Trilogy >= 2.0
- MySQL 8.0 (other versions are not tested)
bundle install
bundle exec rspecIntegration tests run against a real MySQL instance using Docker:
docker compose up -d # start MySQL on port 3307
MYSQL_PORT=3307 bundle exec rspec spec/integration/
docker compose down -v # cleanupMIT