active_record_proxy_adapters 0.1.3 → 0.1.5
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 +4 -4
- data/CHANGELOG.md +23 -6
- data/Dockerfile +1 -1
- data/README.md +2 -0
- data/db/postgresql_structure.sql +16 -6
- data/lib/active_record_proxy_adapters/primary_replica_proxy.rb +31 -2
- data/lib/active_record_proxy_adapters/version.rb +1 -1
- data/postgres_primary.dockerfile +1 -1
- data/postgres_replica.dockerfile +1 -1
- metadata +2 -3
- data/Rakefile +0 -81
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b80e3ab832bc332967c0f4840f9f35563725497e61a48339c46e4f65a15b2458
|
4
|
+
data.tar.gz: fc85aed94e09aa25bd15e46102ae318f9b790bc2bb67a24243c2340952ff4fa2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a81cf2441d0f1548311474fbaa3326b9d2fbccc0b25c481408f0f99ce6634710dc589e0aed60f63ca55e5debe2635eb4cd6a75c2a0cf444fb7929292dc02dac
|
7
|
+
data.tar.gz: c4bd34a35fa68b8257a5839b99055efdd11d18ce436afea98fd695475c4dae7cfda7ecc26fc2aed61f0981258c65684f22185c6d8fddc143ceb06b400b9219b1
|
data/CHANGELOG.md
CHANGED
@@ -1,20 +1,37 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
-
|
4
|
-
-
|
3
|
+
- Handle PendingMigrationConnection introduced by Rails 7.2 and backported to Rails 7.1
|
4
|
+
- Stick to same connection throughout request span
|
5
|
+
|
6
|
+
## [0.2.1] - 2025-01-02
|
7
|
+
|
8
|
+
- Fix replica connection pool getter when specific connection name is not found https://github.com/Nasdaq/active_record_proxy_adapters/commit/847e150dd21c5bc619745ee1d9d8fcaa9b8f2eea
|
9
|
+
|
10
|
+
## [0.2.0] - 2024-12-24
|
11
|
+
|
12
|
+
- Add custom log subscriber to tag queries based on the adapter being used https://github.com/Nasdaq/active_record_proxy_adapters/commit/68b8c1f4191388eb957bf12e0f84289da667e940
|
13
|
+
|
14
|
+
## [0.1.4] - 2025-01-02
|
15
|
+
|
16
|
+
- Fix replica connection pool getter when specific connection name is not found https://github.com/Nasdaq/active_record_proxy_adapters/commit/88b32a282b54d420e652f638656dbcf063ac8796
|
17
|
+
|
18
|
+
## [0.1.3] - 2024-12-24
|
19
|
+
|
20
|
+
- Fix replica connection pool getter when database configurations have multiple replicas https://github.com/Nasdaq/active_record_proxy_adapters/commit/ea5a33997da45ac073f166b3fbd2d12426053cd6
|
21
|
+
- Retrieve replica pool without checking out a connection https://github.com/Nasdaq/active_record_proxy_adapters/commit/6470ef58e851082ae1f7a860ecdb5b451ef903c8
|
5
22
|
|
6
23
|
## [0.1.2] - 2024-12-16
|
7
24
|
|
8
|
-
- Fix CTE regex matcher
|
25
|
+
- Fix CTE regex matcher https://github.com/Nasdaq/active_record_proxy_adapters/commit/4b1d10bfd952fb1f5b102de8cc1a5bd05d25f5e9
|
9
26
|
|
10
27
|
## [0.1.1] - 2024-11-27
|
11
28
|
|
12
|
-
- Enable RubyGems MFA
|
29
|
+
- Enable RubyGems MFA https://github.com/Nasdaq/active_record_proxy_adapters/commit/2a71b1f4354fb966cc0aa68231ca5837814e07ee
|
13
30
|
|
14
31
|
## [0.1.0] - 2024-11-19
|
15
32
|
|
16
|
-
- Add PostgreSQLProxyAdapter
|
33
|
+
- Add PostgreSQLProxyAdapter https://github.com/Nasdaq/active_record_proxy_adapters/commit/2b3bb9f7359139519b32af3018ceb07fed8c6b33
|
17
34
|
|
18
35
|
## [0.1.0.rc2] - 2024-10-28
|
19
36
|
|
20
|
-
- Add PostgreSQLProxyAdapter
|
37
|
+
- Add PostgreSQLProxyAdapter https://github.com/Nasdaq/active_record_proxy_adapters/commit/2b3bb9f7359139519b32af3018ceb07fed8c6b33
|
data/Dockerfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# ActiveRecordProxyAdapters
|
2
2
|
|
3
|
+
[](https://github.com/Nasdaq/active_record_proxy_adapters/actions/workflows/test.yml)
|
4
|
+
|
3
5
|
A set of ActiveRecord adapters that leverage Rails native multiple database setup to allow automatic connection switching from _one_ primary pool to _one_ replica pool at the database statement level.
|
4
6
|
|
5
7
|
## Installation
|
data/db/postgresql_structure.sql
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
SET statement_timeout = 0;
|
2
2
|
SET lock_timeout = 0;
|
3
3
|
SET idle_in_transaction_session_timeout = 0;
|
4
|
+
SET transaction_timeout = 0;
|
4
5
|
SET client_encoding = 'UTF8';
|
5
6
|
SET standard_conforming_strings = on;
|
6
7
|
SELECT pg_catalog.set_config('search_path', '', false);
|
@@ -9,16 +10,18 @@ SET xmloption = content;
|
|
9
10
|
SET client_min_messages = warning;
|
10
11
|
SET row_security = off;
|
11
12
|
|
12
|
-
|
13
|
-
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
|
14
|
-
--
|
13
|
+
SET default_tablespace = '';
|
15
14
|
|
16
|
-
|
15
|
+
SET default_table_access_method = heap;
|
17
16
|
|
17
|
+
--
|
18
|
+
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
|
19
|
+
--
|
18
20
|
|
19
|
-
|
21
|
+
CREATE TABLE public.schema_migrations (
|
22
|
+
version character varying(255) NOT NULL
|
23
|
+
);
|
20
24
|
|
21
|
-
SET default_table_access_method = heap;
|
22
25
|
|
23
26
|
--
|
24
27
|
-- Name: users; Type: TABLE; Schema: public; Owner: -
|
@@ -68,6 +71,13 @@ ALTER TABLE ONLY public.users
|
|
68
71
|
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
|
69
72
|
|
70
73
|
|
74
|
+
--
|
75
|
+
-- Name: unique_schema_migrations; Type: INDEX; Schema: public; Owner: -
|
76
|
+
--
|
77
|
+
|
78
|
+
CREATE UNIQUE INDEX unique_schema_migrations ON public.schema_migrations USING btree (version);
|
79
|
+
|
80
|
+
|
71
81
|
--
|
72
82
|
-- PostgreSQL database dump complete
|
73
83
|
--
|
@@ -62,7 +62,7 @@ module ActiveRecordProxyAdapters
|
|
62
62
|
|
63
63
|
attr_reader :primary_connection, :last_write_at, :active_record_context
|
64
64
|
|
65
|
-
delegate :connection_handler,
|
65
|
+
delegate :connection_handler, to: :connection_class
|
66
66
|
delegate :reading_role, :writing_role, to: :active_record_context
|
67
67
|
|
68
68
|
def replica_pool_unavailable?
|
@@ -70,9 +70,18 @@ module ActiveRecordProxyAdapters
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def replica_pool
|
73
|
+
# use default handler if the connection pool for specific class is not found
|
74
|
+
specific_replica_pool || default_replica_pool
|
75
|
+
end
|
76
|
+
|
77
|
+
def specific_replica_pool
|
73
78
|
connection_handler.retrieve_connection_pool(connection_class.name, role: reading_role)
|
74
79
|
end
|
75
80
|
|
81
|
+
def default_replica_pool
|
82
|
+
connection_handler.retrieve_connection_pool(ActiveRecord::Base.name, role: reading_role)
|
83
|
+
end
|
84
|
+
|
76
85
|
def connection_class
|
77
86
|
active_record_context.connection_class_for(primary_connection)
|
78
87
|
end
|
@@ -111,11 +120,25 @@ module ActiveRecordProxyAdapters
|
|
111
120
|
[reading_role, writing_role].include?(role) ? role : nil
|
112
121
|
end
|
113
122
|
|
123
|
+
def connected_to_stack
|
124
|
+
return connection_class.connected_to_stack if connection_class.respond_to?(:connected_to_stack)
|
125
|
+
|
126
|
+
# handle Rails 7.2+ pending migrations Connection
|
127
|
+
return [{ role: writing_role }] if pending_migration_connection?
|
128
|
+
|
129
|
+
[]
|
130
|
+
end
|
131
|
+
|
132
|
+
def pending_migration_connection?
|
133
|
+
active_record_context.active_record_v7_1_or_greater? &&
|
134
|
+
connection_class.name == "ActiveRecord::PendingMigrationConnection"
|
135
|
+
end
|
136
|
+
|
114
137
|
def connection_for(role, sql_string)
|
115
138
|
connection = primary_connection if role == writing_role || replica_pool_unavailable?
|
116
139
|
connection ||= checkout_replica_connection
|
117
140
|
|
118
|
-
result = yield
|
141
|
+
result = connected_to(role:) { yield connection }
|
119
142
|
|
120
143
|
update_primary_latest_write_timestamp if !replica_connection?(connection) && write_statement?(sql_string)
|
121
144
|
|
@@ -124,6 +147,12 @@ module ActiveRecordProxyAdapters
|
|
124
147
|
replica_connection?(connection) && replica_pool.checkin(connection)
|
125
148
|
end
|
126
149
|
|
150
|
+
def connected_to(role:, &block)
|
151
|
+
return block.call unless connection_class.respond_to?(:connected_to)
|
152
|
+
|
153
|
+
connection_class.connected_to(role:, &block)
|
154
|
+
end
|
155
|
+
|
127
156
|
def replica_connection?(connection)
|
128
157
|
connection && connection != primary_connection
|
129
158
|
end
|
data/postgres_primary.dockerfile
CHANGED
data/postgres_replica.dockerfile
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_proxy_adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Cruz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -67,7 +67,6 @@ files:
|
|
67
67
|
- Dockerfile
|
68
68
|
- LICENSE.txt
|
69
69
|
- README.md
|
70
|
-
- Rakefile
|
71
70
|
- db/postgresql_structure.sql
|
72
71
|
- docker-compose.yml
|
73
72
|
- docker/postgres_replica/cmd.sh
|
data/Rakefile
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "bundler/gem_tasks"
|
4
|
-
require "rspec/core/rake_task"
|
5
|
-
|
6
|
-
RSpec::Core::RakeTask.new(:spec)
|
7
|
-
|
8
|
-
require "rubocop/rake_task"
|
9
|
-
|
10
|
-
RuboCop::RakeTask.new
|
11
|
-
|
12
|
-
task default: %i[spec rubocop]
|
13
|
-
|
14
|
-
desc "Prepares the database environment for use"
|
15
|
-
task :environment do
|
16
|
-
$LOAD_PATH << File.expand_path("lib", __dir__)
|
17
|
-
require "active_record_proxy_adapters"
|
18
|
-
require "active_record_proxy_adapters/connection_handling"
|
19
|
-
ActiveRecord::Base.extend ActiveRecordProxyAdapters::ConnectionHandling
|
20
|
-
require_relative "spec/test_helper"
|
21
|
-
|
22
|
-
TestHelper.setup_active_record_config
|
23
|
-
|
24
|
-
$stdout.puts "Environment loaded: #{TestHelper.env_name}"
|
25
|
-
end
|
26
|
-
|
27
|
-
namespace :db do # rubocop:disable Metrics/BlockLength
|
28
|
-
desc "Drops all databases"
|
29
|
-
task drop: %i[drop:postgresql]
|
30
|
-
|
31
|
-
namespace :drop do
|
32
|
-
desc "Drops the postgresql database"
|
33
|
-
task postgresql: :environment do
|
34
|
-
TestHelper.drop_database
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
desc "Creates all databases"
|
39
|
-
task create: %i[create:postgresql]
|
40
|
-
|
41
|
-
namespace :create do
|
42
|
-
desc "Creates the postgresql database"
|
43
|
-
task postgresql: :environment do
|
44
|
-
TestHelper.create_database
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
namespace :schema do
|
49
|
-
desc "Loads all schemas onto their respective databases"
|
50
|
-
task load: %i[load:postgresql]
|
51
|
-
|
52
|
-
namespace :load do
|
53
|
-
desc "Loads the schema into the postgresql database from schema_path. Default is db/postgresql_structure.sql"
|
54
|
-
task :postgresql, [:schema_path] => :environment do |_task, args|
|
55
|
-
args.with_defaults(schema_path: "db/postgresql_structure.sql")
|
56
|
-
TestHelper.load_schema(args.schema_path)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
desc "Creates a all databases and loads their schemas"
|
62
|
-
task setup: %i[create schema:load]
|
63
|
-
|
64
|
-
namespace :setup do
|
65
|
-
desc "Creates the postgresql database and loads the schema"
|
66
|
-
task postgresql: %i[create:postgresql schema:load:postgresql]
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
namespace :coverage do
|
71
|
-
desc "Collates all result sets generated by the different test runners"
|
72
|
-
task :report do
|
73
|
-
require "simplecov"
|
74
|
-
|
75
|
-
SimpleCov.collate Dir["coverage/**/.resultset.json"] do
|
76
|
-
add_group "PostgreSQL" do |src_file|
|
77
|
-
[/postgresql/, /postgre_sql/].any? { |pattern| pattern.match?(src_file.filename) }
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|