active_record_proxy_adapters 0.1.3 → 0.2.0

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: 70b1c4f2efd94125b178e043fa1162ab6d8d8edb7a9041eb10b8244c606b3c59
4
- data.tar.gz: fb74c76ed0dc0865c9756c3188a92dd248ce58ef4832811704c15a59b728991e
3
+ metadata.gz: 5312effcf68a3305eb5e7804440112433f5b6837d6b71e7d122145eac4741619
4
+ data.tar.gz: 50586657daa02e76dfffebbe1faf8df5093198471faf847cec97cd16634c0c17
5
5
  SHA512:
6
- metadata.gz: 3730cca00342dd7f44944305bfdd53f158da2bc8d09b39883645a2b1a581297904dce121c10de5bc351e30b999b1bcd59457bd3309280074fb36d716a22bdcae
7
- data.tar.gz: 07d42f0c57bdd203a260a7d55010159318db260f95ed5483049e8a6ac771908df7696d05692a3043f0a54d77f4e2623c6aa08b7ddda2db5a79e634224db78b12
6
+ metadata.gz: 707b80a3618328639126d13f3f76bb8123105019b3e30c75f15c66a66a88d49aa5de4301fe70b25ff5356ef37e54ebff896f4cab0160cadbb79d08ae9cf0dc4e
7
+ data.tar.gz: 6b84984e33cf87abe9a12f5553bccffc32e2a9563a0c859c488cdae4be4c3a019ffce01242648620c67bb85a16dc61d982f0be8ecba83943f01c718e067743dc
data/CHANGELOG.md CHANGED
@@ -1,20 +1,26 @@
1
1
  ## [Unreleased]
2
2
 
3
- - Fix replica connection pool getter when database configurations have multiple replicas
4
- - Retrieve replica pool without checking out a connection
3
+ ## [0.2.0] - 2024-12-24
4
+
5
+ - Add custom log subscriber to tag queries based on the adapter being used (68b8c1f4191388eb957bf12e0f84289da667e940)
6
+
7
+ ## [0.1.3] - 2024-12-24
8
+
9
+ - Fix replica connection pool getter when database configurations have multiple replicas (ea5a33997da45ac073f166b3fbd2d12426053cd6)
10
+ - Retrieve replica pool without checking out a connection (6470ef58e851082ae1f7a860ecdb5b451ef903c8)
5
11
 
6
12
  ## [0.1.2] - 2024-12-16
7
13
 
8
- - Fix CTE regex matcher (4b1d10b)
14
+ - Fix CTE regex matcher (4b1d10bfd952fb1f5b102de8cc1a5bd05d25f5e9)
9
15
 
10
16
  ## [0.1.1] - 2024-11-27
11
17
 
12
- - Enable RubyGems MFA (2a71b1f)
18
+ - Enable RubyGems MFA (2a71b1f4354fb966cc0aa68231ca5837814e07ee)
13
19
 
14
20
  ## [0.1.0] - 2024-11-19
15
21
 
16
- - Add PostgreSQLProxyAdapter (2b3bb9f)
22
+ - Add PostgreSQLProxyAdapter (2b3bb9f7359139519b32af3018ceb07fed8c6b33)
17
23
 
18
24
  ## [0.1.0.rc2] - 2024-10-28
19
25
 
20
- - Add PostgreSQLProxyAdapter (2b3bb9f)
26
+ - Add PostgreSQLProxyAdapter (2b3bb9f7359139519b32af3018ceb07fed8c6b33)
data/docker-compose.yml CHANGED
@@ -50,6 +50,7 @@ services:
50
50
  - POSTGRES_LOG_STATEMENT=${POSTGRES_LOG_STATEMENT:-}
51
51
  - REPLICA_USER=replicator
52
52
  - REPLICA_PASSWORD=replicator
53
+ container_name: postgres_primary
53
54
  environment:
54
55
  POSTGRES_DB: postgres
55
56
  POSTGRES_USER: postgres_primary_test
@@ -5,8 +5,10 @@ require "active_support/core_ext/integer/time"
5
5
  module ActiveRecordProxyAdapters
6
6
  # Provides a global configuration object to configure how the proxy should behave.
7
7
  class Configuration
8
- PROXY_DELAY = 2.seconds.freeze
9
- CHECKOUT_TIMEOUT = 2.seconds.freeze
8
+ PROXY_DELAY = 2.seconds.freeze
9
+ CHECKOUT_TIMEOUT = 2.seconds.freeze
10
+ LOG_SUBSCRIBER_PRIMARY_PREFIX = proc { |event| "#{event.payload[:connection].class::ADAPTER_NAME} Primary" }.freeze
11
+ LOG_SUBSCRIBER_REPLICA_PREFIX = proc { |event| "#{event.payload[:connection].class::ADAPTER_NAME} Replica" }.freeze
10
12
 
11
13
  # @return [ActiveSupport::Duration] How long the proxy should reroute all read requests to the primary database
12
14
  # since the latest write. Defaults to PROXY_DELAY.
@@ -15,9 +17,25 @@ module ActiveRecordProxyAdapters
15
17
  # Defaults to CHECKOUT_TIMEOUT.
16
18
  attr_accessor :checkout_timeout
17
19
 
20
+ # @return [Proc] Prefix for the log subscriber when the primary database is used.
21
+ attr_reader :log_subscriber_primary_prefix
22
+
23
+ # @return [Proc] Prefix for the log subscriber when the replica database is used.
24
+ attr_reader :log_subscriber_replica_prefix
25
+
18
26
  def initialize
19
- self.proxy_delay = PROXY_DELAY
20
- self.checkout_timeout = CHECKOUT_TIMEOUT
27
+ self.proxy_delay = PROXY_DELAY
28
+ self.checkout_timeout = CHECKOUT_TIMEOUT
29
+ self.log_subscriber_primary_prefix = LOG_SUBSCRIBER_PRIMARY_PREFIX
30
+ self.log_subscriber_replica_prefix = LOG_SUBSCRIBER_REPLICA_PREFIX
31
+ end
32
+
33
+ def log_subscriber_primary_prefix=(prefix)
34
+ @log_subscriber_primary_prefix = prefix.is_a?(Proc) ? prefix : proc { prefix.to_s }
35
+ end
36
+
37
+ def log_subscriber_replica_prefix=(prefix)
38
+ @log_subscriber_replica_prefix = prefix.is_a?(Proc) ? prefix : proc { prefix.to_s }
21
39
  end
22
40
  end
23
41
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordProxyAdapters
4
+ class LogSubscriber < ActiveRecord::LogSubscriber # rubocop:disable Style/Documentation
5
+ attach_to :active_record
6
+
7
+ IGNORE_PAYLOAD_NAMES = %w[SCHEMA EXPLAIN].freeze
8
+
9
+ def sql(event)
10
+ payload = event.payload
11
+ name = payload[:name]
12
+ unless IGNORE_PAYLOAD_NAMES.include?(name)
13
+ name = [database_instance_prefix_for(event), name].compact.join(" ")
14
+ payload[:name] = name
15
+ end
16
+ super
17
+ end
18
+
19
+ protected
20
+
21
+ def database_instance_prefix_for(event)
22
+ connection = event.payload[:connection]
23
+ config = connection.instance_variable_get(:@config)
24
+ prefix = if config[:replica] || config["replica"]
25
+ log_subscriber_replica_prefix
26
+ else
27
+ log_subscriber_primary_prefix
28
+ end
29
+
30
+ "[#{prefix.call(event)}]"
31
+ end
32
+
33
+ private
34
+
35
+ delegate :log_subscriber_primary_prefix, :log_subscriber_replica_prefix, to: :config
36
+
37
+ def config
38
+ ActiveRecordProxyAdapters.config
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordProxyAdapters
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_proxy_adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Cruz
@@ -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
@@ -78,6 +77,7 @@ files:
78
77
  - lib/active_record_proxy_adapters/configuration.rb
79
78
  - lib/active_record_proxy_adapters/connection_handling.rb
80
79
  - lib/active_record_proxy_adapters/hijackable.rb
80
+ - lib/active_record_proxy_adapters/log_subscriber.rb
81
81
  - lib/active_record_proxy_adapters/postgresql_proxy.rb
82
82
  - lib/active_record_proxy_adapters/primary_replica_proxy.rb
83
83
  - lib/active_record_proxy_adapters/railtie.rb
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