Skip to content

Commit bec5cde

Browse files
authored
Merge pull request rails#50349 from Shopify/trilogy-socket-conn
TrilogyAdapter: ignore host if socket is set
2 parents 8278626 + 0fe087e commit bec5cde

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* TrilogyAdapter: ignore `host` if `socket` parameter is set.
2+
3+
This allows to configure a connection on a UNIX socket via DATABASE_URL:
4+
5+
```
6+
DATABASE_URL=trilogy://does-not-matter/my_db_production?socket=/var/run/mysql.sock
7+
```
8+
9+
*Jean Boussier*
10+
111
* Make `assert_queries` and `assert_no_queries` assertions public.
212
313
To assert the expected number of queries are made, Rails internally uses

activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class << self
3131
def new_client(config)
3232
config[:ssl_mode] = parse_ssl_mode(config[:ssl_mode]) if config[:ssl_mode]
3333
::Trilogy.new(config)
34-
rescue ::Trilogy::ConnectionError, ::Trilogy::ProtocolError => error
34+
rescue ::Trilogy::Error => error
3535
raise translate_connect_error(config, error)
3636
end
3737

@@ -76,6 +76,10 @@ def initialize_type_map(m)
7676
def initialize(config, *)
7777
config = config.dup
7878

79+
# Trilogy ignore `socket` if `host is set. We want the opposite to allow
80+
# configuring UNIX domain sockets via `DATABASE_URL`.
81+
config.delete(:host) if config[:socket]
82+
7983
# Set FOUND_ROWS capability on the connection so UPDATE queries returns number of rows
8084
# matched rather than number of rows updated.
8185
config[:found_rows] = true

activerecord/test/cases/adapters/trilogy/trilogy_adapter_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
351351
ActiveRecord::Base.establish_connection :arunit
352352
end
353353

354+
test "socket has precedence over host" do
355+
error = assert_raises ActiveRecord::ConnectionNotEstablished do
356+
ActiveRecord::ConnectionAdapters::TrilogyAdapter.new(host: "invalid", port: 12345, socket: "/var/invalid.sock").connect!
357+
end
358+
assert_includes error.message, "/var/invalid.sock"
359+
end
360+
354361
# Create a temporary subscription to verify notification is sent.
355362
# Optionally verify the notification payload includes expected types.
356363
def assert_notification(notification, expected_payload = {}, &block)

0 commit comments

Comments
 (0)