Skip to content

fix: let Action Cable's PostgreSQL subscription adapter run on aws_po… - #197

Merged
aaron-congo merged 6 commits into
fix/fork-safe-monitorsfrom
fix/action-cable-pg-adapter
Oct 6, 2026
Merged

aaron-congo merged 6 commits into
fix/fork-safe-monitorsfrom
fix/action-cable-pg-adapter

Conversation

@aaron-congo

Copy link
Copy Markdown
Contributor

Summary

Fix Action Cable's PostgreSQL subscription adapter (adapter: postgresql in cable.yml) refusing to run on the aws_postgresql ActiveRecord adapter.

Description

Stacked on #196 (which is stacked on #195) - review and merge those first; this PR's diff is the single commit on top of them.

Action Cable's PostgreSQL subscription adapter checks that the raw connection it gets from ActiveRecord is a PG::Connection, and raises otherwise. aws_postgresql hands out a WrapperPgConnection, so every subscribe and broadcast failed with The Active Record database must be PostgreSQL in order to use the PostgreSQL Action Cable storage adapter. Turbo Stream broadcasts (broadcasts_to, broadcast_replace_to) go through the same path and failed too. The stock postgresql adapter works.

Changes:

  • New ActiveRecord::ConnectionAdapters::AwsActionCablePostgreSQLSupport, loaded by the aws_postgresql adapter. It registers an ActiveSupport.on_load(:action_cable) hook that loads Action Cable's PostgreSQL subscription adapter and prepends a verify! override. The override accepts a WrapperPgConnection and passes every other connection to Action Cable's own check.
  • The calls Action Cable makes on the connection (exec, escape_identifier, escape_string, wait_for_notify) are already forwarded to the wrapped PG::Connection.
  • WrapperPgConnection#is_a? is unchanged, so code that needs a real PG::Connection is not misled.
  • Applications without Action Cable never run the hook. The hook works whether Action Cable loads before or after the adapter.
  • Action Cable 7.2, 8.0 and 8.1 have the same check and load hook, so all supported versions are covered.

Testing:

  • New spec/unit/aws_action_cable_postgresql_support_spec.rb, using a stand-in for Action Cable's adapter in spec/fixtures/action_cable_stub (Action Cable is not a dependency of the gem). It fails without the fix and checks that non-PostgreSQL connections are still rejected.
  • bundle exec rspec spec/unit: 3138 examples, 0 failures. The new spec also passes under ActiveRecord 7.2 and 8.0. bundle exec rubocop lib spec: no offenses.
  • Manual test against Aurora PostgreSQL with Action Cable 8.1's real PostgreSQL subscription adapter on aws_postgresql: a subscribe followed by a broadcast delivers the message, with Action Cable loaded both before and after the adapter. Before the fix the same test failed with the error above; the stock postgresql adapter passed.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…stgresql

Action Cable's PostgreSQL subscription adapter refuses any raw connection that is
not a PG::Connection, and aws_postgresql hands out a WrapperPgConnection, so every
subscribe and broadcast (including Turbo Stream broadcasts) raised "The Active Record
database must be PostgreSQL in order to use the PostgreSQL Action Cable storage adapter".

The aws_postgresql adapter now registers an :action_cable load hook that prepends a
verify! override to ActionCable::SubscriptionAdapter::PostgreSQL, accepting a
WrapperPgConnection and leaving every other connection to Action Cable's own check.
The calls Action Cable makes on the connection (exec, escape_identifier,
escape_string, wait_for_notify) are forwarded to the wrapped PG::Connection.
@aaron-congo
aaron-congo requested a review from a team as a code owner October 6, 2026 06:24
@aaron-congo
aaron-congo merged commit 8a33092 into fix/fork-safe-monitors Oct 6, 2026
@aaron-congo
aaron-congo deleted the fix/action-cable-pg-adapter branch October 6, 2026 20:46
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.

3 participants