active_record_proxy_adapters 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 151caa80f35f53ad02ef3307a55ef0fec4f1a6f369e6afac8d063e4ea2ad5e7f
4
- data.tar.gz: 60213ad52a363c435514038eeb65227bff7132b91a6dbed86b8917459449c39e
3
+ metadata.gz: 58d50939b1410d1d8af3dd6b2c0dd1b2b872104faf48392b2298f897b467d7f5
4
+ data.tar.gz: 03c3373e6d5b7eab78e628c797e35642dbc9e4a314d5b45b55dadbc018f5fcde
5
5
  SHA512:
6
- metadata.gz: 7720b6fe6b2d6848d30444f77c71d2567996f2e45011c79e6c4791e043b7d9333c2d92554cdf5f6b5a48c29c6b384509a00b9f9b3b930402d47c98188744daa7
7
- data.tar.gz: b73be535ad798afb4386d4027e4e92df00d9864a196d4cc1d30ad3a0a4e524c87ad12f67d214ef524a1641800ccf5bc2ab4911acf49c5606d867f51981a18fec
6
+ metadata.gz: 9d19e223654ee5c3bf983e27d5999348ec7458bfe7e9738ff21bac022830b68a372a743b30160c07270548aa4bf68f44ecc26b4b6f9eedd93eec0a480a15e3dd
7
+ data.tar.gz: 04f8f7f579ede01034073f5854c9892fbe4ebfe46ad06876d5fe2fe2c47613b94d38f79a86ddcd2b231557fbfb243e0fd0578a18bda715c5ffddc6c521694c67
data/.rubocop.yml CHANGED
@@ -4,6 +4,8 @@ require:
4
4
  AllCops:
5
5
  TargetRubyVersion: 3.1
6
6
  NewCops: enable
7
+ Exclude:
8
+ - 'gemfiles/**/*'
7
9
 
8
10
  Style/StringLiterals:
9
11
  EnforcedStyle: double_quotes
data/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
- - Fix Active Record adapters dependency loading
3
+ ## [0.4.0] - 2025-02-24
4
+
5
+ - Add load hooks for proxy adapters
6
+ - Add TrilogyProxyAdapter
7
+
8
+ ## [0.3.1] - 2025-02-12
9
+ - Fix Active Record adapters dependency loading https://github.com/Nasdaq/active_record_proxy_adapters/commit/b729f8bdb517cdc80f348c00e1fe4c5b56b76143
4
10
 
5
11
  ## [0.3.0] - 2025-01-17
6
12
 
data/Dockerfile CHANGED
@@ -1,8 +1,8 @@
1
1
  ARG RUBY_VERSION=3.2.3
2
2
  ARG DOCKER_REGISTRY=docker.io
3
3
  FROM $DOCKER_REGISTRY/ruby:$RUBY_VERSION-alpine
4
- ARG RAILS_VERSION="~> 6.1.0"
5
- ENV RAILS_VERSION=$RAILS_VERSION
4
+ ENV RAILS_VERSION="8.0.0"
5
+ ENV RAILS_ENV=test
6
6
 
7
7
  RUN apk --update add \
8
8
  build-base \
@@ -17,4 +17,5 @@ COPY . /app
17
17
  WORKDIR /app
18
18
 
19
19
  RUN bundle install
20
+ RUN bundle exec appraisal install
20
21
 
data/README.md CHANGED
@@ -39,9 +39,9 @@ Currently supported adapters:
39
39
 
40
40
  - `postgresql`
41
41
  - `mysql2`
42
+ - `trilogy`
42
43
 
43
44
  Coming soon:
44
- - `trilogy`
45
45
  - `sqlite`
46
46
 
47
47
 
@@ -87,11 +87,7 @@ end
87
87
  ```ruby
88
88
  # In your application setup
89
89
  require "active_record_proxy_adapters"
90
-
91
- ActiveSupport.on_load :active_record do
92
- require "active_record_proxy_adapters/connection_handling"
93
- ActiveRecord::Base.extend(ActiveRecordProxyAdapters::ConnectionHandling)
94
- end
90
+ require "active_record_proxy_adapters/connection_handling"
95
91
 
96
92
  # in your base model
97
93
  class ApplicationRecord << ActiveRecord::Base
data/docker-compose.yml CHANGED
@@ -16,29 +16,29 @@ services:
16
16
  build:
17
17
  args:
18
18
  - RUBY_VERSION=${RUBY_VERSION:-3.3.6}
19
- - RAILS_VERSION=${RAILS_VERSION:-8.0.0}
20
19
  container_name: app
21
20
  image: active_record_proxy_adapters-app:${ENV_TAG:-latest}
22
21
  tty: true
23
22
  stdin_open: true
24
23
  environment:
25
- PGHOST: postgres_primary
26
- PG_PRIMARY_USER: postgres_primary_test
27
- PG_PRIMARY_PASSWORD: postgres_primary_test
28
- PG_PRIMARY_HOST: postgres_primary
29
- PG_PRIMARY_PORT: 5432
30
- PG_REPLICA_USER: postgres_primary_test
31
- PG_REPLICA_PASSWORD: postgres_primary_test
32
- PG_REPLICA_HOST: postgres_replica
33
- PG_REPLICA_PORT: 5432
34
- MYSQL_PRIMARY_USER: root
35
- MYSQL_PRIMARY_PASSWORD: mysql
36
- MYSQL_PRIMARY_HOST: mysql_primary
37
- MYSQL_PRIMARY_PORT: 3306
38
- MYSQL_REPLICA_USER: root
39
- MYSQL_REPLICA_PASSWORD: mysql
40
- MYSQL_REPLICA_HOST: mysql_primary
41
- MYSQL_REPLICA_PORT: 3306
24
+ - RAILS_VERSION=${RAILS_VERSION:-8.0.0}
25
+ - PGHOST=postgres_primary
26
+ - PG_PRIMARY_USER=postgres_primary_test
27
+ - PG_PRIMARY_PASSWORD=postgres_primary_test
28
+ - PG_PRIMARY_HOST=postgres_primary
29
+ - PG_PRIMARY_PORT=5432
30
+ - PG_REPLICA_USER=postgres_primary_test
31
+ - PG_REPLICA_PASSWORD=postgres_primary_test
32
+ - PG_REPLICA_HOST=postgres_replica
33
+ - PG_REPLICA_PORT=5432
34
+ - MYSQL_PRIMARY_USER=root
35
+ - MYSQL_PRIMARY_PASSWORD=mysql
36
+ - MYSQL_PRIMARY_HOST=mysql_primary
37
+ - MYSQL_PRIMARY_PORT=3306
38
+ - MYSQL_REPLICA_USER=root
39
+ - MYSQL_REPLICA_PASSWORD=mysql
40
+ - MYSQL_REPLICA_HOST=mysql_primary
41
+ - MYSQL_REPLICA_PORT=3306
42
42
  depends_on:
43
43
  - postgres_primary
44
44
  - postgres_replica
@@ -37,3 +37,5 @@ if ActiveRecordProxyAdapters::ActiveRecordContext.active_record_v7_2_or_greater?
37
37
  "active_record/connection_adapters/mysql2_proxy_adapter"
38
38
  )
39
39
  end
40
+
41
+ ActiveSupport.run_load_hooks(:active_record_mysql2proxyadapter, ActiveRecord::ConnectionAdapters::Mysql2ProxyAdapter)
@@ -41,3 +41,6 @@ if ActiveRecordProxyAdapters::ActiveRecordContext.active_record_v7_2_or_greater?
41
41
  "active_record/connection_adapters/postgresql_proxy_adapter"
42
42
  )
43
43
  end
44
+
45
+ ActiveSupport.run_load_hooks(:active_record_postgresqlproxyadapter,
46
+ ActiveRecord::ConnectionAdapters::PostgreSQLProxyAdapter)
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/tasks/trilogy_proxy_database_tasks"
4
+ require "active_record/connection_adapters/trilogy_adapter"
5
+ require "active_record_proxy_adapters/active_record_context"
6
+ require "active_record_proxy_adapters/hijackable"
7
+ require "active_record_proxy_adapters/trilogy_proxy"
8
+
9
+ module ActiveRecord
10
+ module ConnectionAdapters
11
+ # This adapter is a proxy to the original TrilogyAdapter, allowing the use of the
12
+ # ActiveRecordProxyAdapters::PrimaryReplicaProxy.
13
+ class TrilogyProxyAdapter < TrilogyAdapter
14
+ include ActiveRecordProxyAdapters::Hijackable
15
+
16
+ ADAPTER_NAME = "TrilogyProxy"
17
+
18
+ delegate_to_proxy :execute, :exec_query
19
+
20
+ def initialize(...)
21
+ @proxy = ActiveRecordProxyAdapters::TrilogyProxy.new(self)
22
+
23
+ super
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :proxy
29
+ end
30
+ end
31
+ end
32
+
33
+ if ActiveRecordProxyAdapters::ActiveRecordContext.active_record_v7_2_or_greater?
34
+ ActiveRecord::ConnectionAdapters.register(
35
+ "trilogy_proxy",
36
+ "ActiveRecord::ConnectionAdapters::TrilogyProxyAdapter",
37
+ "active_record/connection_adapters/trilogy_proxy_adapter"
38
+ )
39
+ end
40
+
41
+ ActiveSupport.run_load_hooks(:active_record_trilogyproxyadapter, ActiveRecord::ConnectionAdapters::TrilogyProxyAdapter)
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record_proxy_adapters/database_tasks"
4
+
5
+ module ActiveRecord
6
+ module Tasks
7
+ # Defines the trilogy tasks for dropping, creating, loading schema and dumping schema.
8
+ # Bypasses all the proxy logic to send all requests to primary.
9
+ class TrilogyProxyDatabaseTasks < MySQLDatabaseTasks
10
+ include ActiveRecordProxyAdapters::DatabaseTasks
11
+ end
12
+ end
13
+ end
14
+
15
+ # Allow proxy adapter to run rake tasks, i.e. db:drop, db:create, db:schema:load db:migrate, etc...
16
+ ActiveRecord::Tasks::DatabaseTasks.register_task(
17
+ /trilogy_proxy/,
18
+ "ActiveRecord::Tasks::TrilogyProxyDatabaseTasks"
19
+ )
@@ -8,32 +8,38 @@ rescue LoadError
8
8
  end
9
9
 
10
10
  module ActiveRecordProxyAdapters
11
- # Module to extend ActiveRecord::Base with the connection handling methods.
12
- # Required to make adapter work in ActiveRecord versions <= 7.2.x
13
- module ConnectionHandling
14
- def mysql2_proxy_adapter_class
15
- ::ActiveRecord::ConnectionAdapters::Mysql2ProxyAdapter
16
- end
11
+ module Mysql2
12
+ # Module to extend ActiveRecord::Base with the connection handling methods.
13
+ # Required to make adapter work in ActiveRecord versions <= 7.2.x
14
+ module ConnectionHandling
15
+ def mysql2_proxy_adapter_class
16
+ ::ActiveRecord::ConnectionAdapters::Mysql2ProxyAdapter
17
+ end
17
18
 
18
- # This method is a copy and paste from Rails' mysql2_connection,
19
- # replacing Mysql2Adapter by Mysql2ProxyAdapter
20
- # This is required by ActiveRecord versions <= 7.2.x to establish a connection using the adapter.
21
- def mysql2_proxy_connection(config) # rubocop:disable Metrics/MethodLength
22
- config = config.symbolize_keys
23
- config[:flags] ||= 0
19
+ # This method is a copy and paste from Rails' mysql2_connection,
20
+ # replacing Mysql2Adapter by Mysql2ProxyAdapter
21
+ # This is required by ActiveRecord versions <= 7.2.x to establish a connection using the adapter.
22
+ def mysql2_proxy_connection(config) # rubocop:disable Metrics/MethodLength
23
+ config = config.symbolize_keys
24
+ config[:flags] ||= 0
24
25
 
25
- if config[:flags].is_a? Array
26
- config[:flags].push "FOUND_ROWS"
27
- else
28
- config[:flags] |= Mysql2::Client::FOUND_ROWS
29
- end
26
+ if config[:flags].is_a? Array
27
+ config[:flags].push "FOUND_ROWS"
28
+ else
29
+ config[:flags] |= ::Mysql2::Client::FOUND_ROWS
30
+ end
30
31
 
31
- mysql2_proxy_adapter_class.new(
32
- mysql2_proxy_adapter_class.new_client(config),
33
- logger,
34
- nil,
35
- config
36
- )
32
+ mysql2_proxy_adapter_class.new(
33
+ mysql2_proxy_adapter_class.new_client(config),
34
+ logger,
35
+ nil,
36
+ config
37
+ )
38
+ end
37
39
  end
38
40
  end
39
41
  end
42
+
43
+ ActiveSupport.on_load(:active_record) do
44
+ ActiveRecord::Base.extend(ActiveRecordProxyAdapters::Mysql2::ConnectionHandling)
45
+ end
@@ -10,31 +10,37 @@ end
10
10
  module ActiveRecordProxyAdapters
11
11
  # Module to extend ActiveRecord::Base with the connection handling methods.
12
12
  # Required to make adapter work in ActiveRecord versions <= 7.2.x
13
- module ConnectionHandling
14
- def postgresql_proxy_adapter_class
15
- ::ActiveRecord::ConnectionAdapters::PostgreSQLProxyAdapter
16
- end
13
+ module PostgreSQL
14
+ module ConnectionHandling # rubocop:disable Style/Documentation
15
+ def postgresql_proxy_adapter_class
16
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLProxyAdapter
17
+ end
17
18
 
18
- # This method is a copy and paste from Rails' postgresql_connection,
19
- # replacing PostgreSQLAdapter by PostgreSQLProxyAdapter
20
- # This is required by ActiveRecord versions <= 7.2.x to establish a connection using the adapter.
21
- def postgresql_proxy_connection(config) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
22
- conn_params = config.symbolize_keys.compact
19
+ # This method is a copy and paste from Rails' postgresql_connection,
20
+ # replacing PostgreSQLAdapter by PostgreSQLProxyAdapter
21
+ # This is required by ActiveRecord versions <= 7.2.x to establish a connection using the adapter.
22
+ def postgresql_proxy_connection(config) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
23
+ conn_params = config.symbolize_keys.compact
23
24
 
24
- # Map ActiveRecords param names to PGs.
25
- conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
26
- conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
25
+ # Map ActiveRecords param names to PGs.
26
+ conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
27
+ conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
27
28
 
28
- # Forward only valid config params to PG::Connection.connect.
29
- valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
30
- conn_params.slice!(*valid_conn_param_keys)
29
+ # Forward only valid config params to PG::Connection.connect.
30
+ valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
31
+ conn_params.slice!(*valid_conn_param_keys)
31
32
 
32
- postgresql_proxy_adapter_class.new(
33
- postgresql_proxy_adapter_class.new_client(conn_params),
34
- logger,
35
- conn_params,
36
- config
37
- )
33
+ postgresql_proxy_adapter_class.new(
34
+ postgresql_proxy_adapter_class.new_client(conn_params),
35
+ logger,
36
+ conn_params,
37
+ config
38
+ )
39
+ end
38
40
  end
39
41
  end
40
42
  end
43
+
44
+ ActiveSupport.on_load(:active_record) do
45
+ ActiveRecord::Base.extend(ActiveRecordProxyAdapters::PostgreSQL::ConnectionHandling)
46
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require "active_record/connection_adapters/trilogy_proxy_adapter"
5
+ rescue LoadError
6
+ # trilogy not available
7
+ return
8
+ end
9
+
10
+ module ActiveRecordProxyAdapters
11
+ module Trilogy
12
+ # Module to extend ActiveRecord::Base with the connection handling methods.
13
+ # Required to make adapter work in ActiveRecord versions <= 7.2.x
14
+ module ConnectionHandling
15
+ def trilogy_proxy_adapter_class
16
+ ActiveRecord::ConnectionAdapters::TrilogyProxyAdapter
17
+ end
18
+
19
+ def trilogy_proxy_connection(config) # rubocop:disable Metrics/MethodLength
20
+ configuration = config.dup
21
+
22
+ # Set FOUND_ROWS capability on the connection so UPDATE queries returns number of rows
23
+ # matched rather than number of rows updated.
24
+ configuration[:found_rows] = true
25
+
26
+ options = [
27
+ configuration[:host],
28
+ configuration[:port],
29
+ configuration[:database],
30
+ configuration[:username],
31
+ configuration[:password],
32
+ configuration[:socket],
33
+ 0
34
+ ]
35
+
36
+ trilogy_proxy_adapter_class.new nil, logger, options, configuration
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ ActiveSupport.on_load(:active_record) do
43
+ ActiveRecord::Base.extend(ActiveRecordProxyAdapters::Trilogy::ConnectionHandling)
44
+ end
@@ -2,10 +2,4 @@
2
2
 
3
3
  require "active_record_proxy_adapters/connection_handling/postgresql"
4
4
  require "active_record_proxy_adapters/connection_handling/mysql2"
5
-
6
- module ActiveRecordProxyAdapters
7
- # Module to extend ActiveRecord::Base with the connection handling methods.
8
- # Required to make adapter work in ActiveRecord versions <= 7.2.x
9
- module ConnectionHandling
10
- end
11
- end
5
+ require "active_record_proxy_adapters/connection_handling/trilogy"
@@ -5,10 +5,7 @@ require "active_support"
5
5
  module ActiveRecordProxyAdapters
6
6
  # Hooks into rails boot process to extend ActiveRecord with the proxy adapter.
7
7
  class Railtie < Rails::Railtie
8
- ActiveSupport.on_load :active_record do
9
- require "active_record_proxy_adapters/connection_handling"
10
- ActiveRecord::Base.extend(ActiveRecordProxyAdapters::ConnectionHandling)
11
- end
8
+ require "active_record_proxy_adapters/connection_handling"
12
9
 
13
10
  config.to_prepare do
14
11
  Rails.autoloaders.each do |autoloader|
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record_proxy_adapters/mysql2_proxy"
4
+
5
+ module ActiveRecordProxyAdapters
6
+ # Proxy to the Mysql2Proxy, allowing the use of the ActiveRecordProxyAdapters::PrimaryReplicaProxy.
7
+ class TrilogyProxy < Mysql2Proxy
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordProxyAdapters
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_proxy_adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Cruz
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-12 00:00:00.000000000 Z
10
+ date: 2025-02-25 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activerecord
@@ -72,14 +72,17 @@ files:
72
72
  - docker/postgres_replica/cmd.sh
73
73
  - lib/active_record/connection_adapters/mysql2_proxy_adapter.rb
74
74
  - lib/active_record/connection_adapters/postgresql_proxy_adapter.rb
75
+ - lib/active_record/connection_adapters/trilogy_proxy_adapter.rb
75
76
  - lib/active_record/tasks/mysql2_proxy_database_tasks.rb
76
77
  - lib/active_record/tasks/postgresql_proxy_database_tasks.rb
78
+ - lib/active_record/tasks/trilogy_proxy_database_tasks.rb
77
79
  - lib/active_record_proxy_adapters.rb
78
80
  - lib/active_record_proxy_adapters/active_record_context.rb
79
81
  - lib/active_record_proxy_adapters/configuration.rb
80
82
  - lib/active_record_proxy_adapters/connection_handling.rb
81
83
  - lib/active_record_proxy_adapters/connection_handling/mysql2.rb
82
84
  - lib/active_record_proxy_adapters/connection_handling/postgresql.rb
85
+ - lib/active_record_proxy_adapters/connection_handling/trilogy.rb
83
86
  - lib/active_record_proxy_adapters/database_tasks.rb
84
87
  - lib/active_record_proxy_adapters/hijackable.rb
85
88
  - lib/active_record_proxy_adapters/log_subscriber.rb
@@ -87,6 +90,7 @@ files:
87
90
  - lib/active_record_proxy_adapters/postgresql_proxy.rb
88
91
  - lib/active_record_proxy_adapters/primary_replica_proxy.rb
89
92
  - lib/active_record_proxy_adapters/railtie.rb
93
+ - lib/active_record_proxy_adapters/trilogy_proxy.rb
90
94
  - lib/active_record_proxy_adapters/version.rb
91
95
  - postgres_primary.dockerfile
92
96
  - postgres_replica.dockerfile