pg_rls 1.0.2 → 1.0.3

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: db02967ab12e8433f7e39680cfd25887676d84e6a3ce8cc00f53d3d46cba5720
4
- data.tar.gz: 03d1f834b316efeaf14abf0ad071e9e750c9fd00d92ca7d7d789b456161763c3
3
+ metadata.gz: a75a646d5cd7efb87d4a4343d3577a307b2a4c8750a9a536d448205f3e4c59b8
4
+ data.tar.gz: 0b5f950a922336c4b8e343a21611dc1f299c934da8829a0b0d9f37862f04ef3e
5
5
  SHA512:
6
- metadata.gz: 8a28cda116e949a2be70858c9f0902e280251fed6ae3677dfd6e6e0028771ccc4958eabb0f0594980850010ea04843ad4ea617052f5638750eee7c879b26a18d
7
- data.tar.gz: fbd8bb581698f2936630053c1090f041922cc7e4ad9dbb3dc52058dd60daad51ab94895d123379e49dbcb5fd3ba7430b497d653d537cc744624910d805d49549
6
+ metadata.gz: 9697b3afdb0b1489fbcf349c44c751f5fc8c495a8a3e10f59c91f2d23be4cc7576f44960c21964817c3d1dea7dbef88954907fdb0fb3641c1ada2eb3f723742b
7
+ data.tar.gz: e8894ef39e132e970d7527ed01057e737900be8f44014331442eb2e4397aa6b805f563ff89c38c575b355a480258305c0a446fd890d707b34735f6dee9068579
data/CHANGELOG.md CHANGED
@@ -1,6 +1,25 @@
1
1
 
2
2
  # [Released]
3
3
 
4
+ ## [1.0.3] - 2026-10-03
5
+
6
+ ### Fixes
7
+
8
+ - **A connection handed between threads could keep another tenant.** `Tenant#set_rls` skipped the `SET` when the
9
+ thread had set that connection before, going by a per-thread record of the connections it had set. If a connection
10
+ changed threads while that record still listed it — released mid-request, or used across threads, as in system
11
+ tests where the test thread and the server's threads share a pool — the first thread got it back already switched
12
+ by another one and ran as that tenant. Apps that hold the connection for the whole request (pg_rls's own
13
+ `PgRls::Record.connection` calls lease it) were not exposed. The tenant is now recorded on the connection itself
14
+ (`rls_tenant_id`), so the `SET` is skipped only when the connection is already on that tenant: no more statements
15
+ than before, and none skipped that were needed.
16
+ - A reconnect or a reset (`DISCARD ALL`) drops the session's tenant; the connection now knows it and sets it again.
17
+ - `Tenant.reset_rls_used_connections` takes the tenant off the connection it is given, whichever thread set it.
18
+
19
+ ### Removed
20
+
21
+ - `Tenant.rls_connection_object_cache_by_thread` and its writer, the per-thread record the fix replaces.
22
+
4
23
  ## [1.0.1] - 2024-10-10
5
24
 
6
25
  ### Major Changes
@@ -15,28 +15,23 @@ module PgRls
15
15
  end
16
16
 
17
17
  class_methods do
18
+ # Takes the tenant off a connection, when it has one (PgRls::ActiveRecord::ConnectionAdapters::RlsTenant).
18
19
  def reset_rls_used_connections(connection = PgRls::Record.connection)
19
- return connection if rls_connection_object_cache_by_thread.nil?
20
+ return connection if connection.rls_tenant_id.nil?
20
21
 
21
22
  connection.exec_query("SET rls.tenant_id TO DEFAULT")
22
- self.rls_connection_object_cache_by_thread = nil
23
+ connection.rls_tenant_id = nil
23
24
  connection
24
25
  end
25
-
26
- def rls_connection_object_cache_by_thread=(value)
27
- ::ActiveSupport::IsolatedExecutionState[:active_record_rls_used_connections] = value
28
- end
29
-
30
- def rls_connection_object_cache_by_thread
31
- ::ActiveSupport::IsolatedExecutionState[:active_record_rls_used_connections]
32
- end
33
26
  end
34
27
 
28
+ # Sets the connection's session to this tenant, unless it already is: what decides is the tenant the
29
+ # connection itself is on, never which thread set it last.
35
30
  def set_rls(connection = PgRls::Record.connection)
36
- self.class.reset_rls_used_connections(connection) if new_tenant?
37
- return self if reused_connection?(connection)
31
+ return self if connection.rls_tenant_id == tenant_id
38
32
 
39
33
  connection.exec_query("SET rls.tenant_id = '#{tenant_id}'")
34
+ connection.rls_tenant_id = tenant_id
40
35
 
41
36
  self
42
37
  end
@@ -44,24 +39,6 @@ module PgRls
44
39
  def readonly?
45
40
  true
46
41
  end
47
-
48
- private
49
-
50
- def new_tenant?
51
- !rls_used_connections.add?(tenant_id).nil?
52
- end
53
-
54
- def reused_connection?(conn)
55
- rls_used_connections.add?(conn.object_id).nil?
56
- end
57
-
58
- def rls_used_connections
59
- if self.class.rls_connection_object_cache_by_thread.nil?
60
- self.class.rls_connection_object_cache_by_thread = Set.new([tenant_id])
61
- end
62
-
63
- self.class.rls_connection_object_cache_by_thread
64
- end
65
42
  end
66
43
  end
67
44
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgRls
4
+ module ActiveRecord
5
+ module ConnectionAdapters
6
+ # Which tenant a connection's session is set to.
7
+ #
8
+ # It belongs to the connection rather than to the thread that set it: a pooled connection moves between
9
+ # threads, and a thread going by its own record of what it once set would skip a connection another thread
10
+ # has since switched to another tenant — and run as that tenant.
11
+ module RlsTenant
12
+ attr_accessor :rls_tenant_id
13
+
14
+ private
15
+
16
+ # A new session has no tenant, whatever the connection's last one had: this runs on every connect,
17
+ # reconnect and reset (DISCARD ALL).
18
+ def configure_connection
19
+ super
20
+ self.rls_tenant_id = nil
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(PgRls::ActiveRecord::ConnectionAdapters::RlsTenant)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "connection_adapters/postgre_sql"
4
4
  require_relative "connection_adapters/connection_pool"
5
+ require_relative "connection_adapters/rls_tenant"
5
6
 
6
7
  module PgRls
7
8
  module ActiveRecord
@@ -2,6 +2,6 @@
2
2
 
3
3
  # :nocov:
4
4
  module PgRls
5
- VERSION = "1.0.2"
5
+ VERSION = "1.0.3"
6
6
  end
7
7
  # :nocov:
@@ -3,6 +3,12 @@
3
3
  module PgRls
4
4
  module ActiveRecord
5
5
  module ConnectionAdapters
6
+ module RlsTenant
7
+ attr_accessor rls_tenant_id: String?
8
+
9
+ private def configure_connection: () -> void
10
+ end
11
+
6
12
  module ConnectionPool
7
13
  attr_reader checkout_timeout: untyped
8
14
 
@@ -23,15 +29,6 @@ module PgRls
23
29
 
24
30
  def set_rls: (ActiveRecord::ConnectionAdapters::PostgreSQLAdapter conn) -> Tenant
25
31
 
26
- def new_tenant?: () -> bool
27
-
28
- def reused_connection?: (ActiveRecord::ConnectionAdapters::PostgreSQLAdapter conn) -> bool
29
-
30
- def rls_connection_object_cache_by_thread=: (untyped value) -> untyped
31
-
32
- def rls_connection_object_cache_by_thread: () -> untyped
33
-
34
- def rls_used_connections: () -> untyped
35
32
  end
36
33
 
37
34
  module Current
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rls
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-10-12 00:00:00.000000000 Z
11
+ date: 2026-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -138,6 +138,7 @@ files:
138
138
  - lib/pg_rls/active_record/connection_adapters/postgre_sql/schema_dumper.rb
139
139
  - lib/pg_rls/active_record/connection_adapters/postgre_sql/schema_statements.rb
140
140
  - lib/pg_rls/active_record/connection_adapters/postgre_sql/sql_helper_method.rb
141
+ - lib/pg_rls/active_record/connection_adapters/rls_tenant.rb
141
142
  - lib/pg_rls/active_record/database_shards.rb
142
143
  - lib/pg_rls/active_record/migration.rb
143
144
  - lib/pg_rls/active_record/migration/command_recorder.rb