pg_rls 1.0.2 → 1.0.4
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 +44 -0
- data/app/models/pg_rls/tenant/securable.rb +7 -30
- data/lib/pg_rls/active_record/connection_adapters/rls_tenant.rb +68 -0
- data/lib/pg_rls/active_record/connection_adapters.rb +1 -0
- data/lib/pg_rls/active_record/test_databases.rb +10 -4
- data/lib/pg_rls/version.rb +1 -1
- data/sig/pg_rls/active_record/connection_adapters.rbs +18 -9
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8a6d6413f258e20cc03352e1327614f91bb5bcba00ce958a9a9082115e803d18
|
|
4
|
+
data.tar.gz: a89ecb17edd4fda275f660560fa33f3c15de9b7cb96e0ffdd9cb226a952d7ce4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9c749cc2d7a7f88f5ae996d8b24b30589f3329cd156cd7900600a5be0ae45766408e5d870eb042819a5a4548b02ac4f865685b6829ea9d77792fd56a4944d02f
|
|
7
|
+
data.tar.gz: 4a9fd2b0b6c4dee5a458d6871bb6c51f27ff6a88fefa22ef1aba94504a3c44d3020728a75ff9b59488a42dd36e1ac95e23dfe14d7c4e61771f5e5fe40402c7e9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
|
|
2
2
|
# [Released]
|
|
3
3
|
|
|
4
|
+
## [1.0.4] - 2026-10-06
|
|
5
|
+
|
|
6
|
+
### Fixes
|
|
7
|
+
|
|
8
|
+
- **A rolled-back transaction could leave a connection on another tenant than pg_rls recorded.** Postgres undoes a
|
|
9
|
+
`SET` made inside a transaction or a savepoint that rolls back, but `rls_tenant_id` kept the tenant set inside it.
|
|
10
|
+
The next switch to that tenant skipped its `SET` and ran on whatever tenant the session had before the
|
|
11
|
+
transaction, and a reset after it was skipped as if the connection had none. Only a tenant switched without being
|
|
12
|
+
restored inside a transaction that then rolls back is affected (`run_within` restores its own before the
|
|
13
|
+
rollback); a transactional test switching the tenant is exactly that, and a concurrency test then read nothing.
|
|
14
|
+
After anything that rolls back — `ROLLBACK`, `ROLLBACK TO SAVEPOINT`, `ROLLBACK AND CHAIN` (Rails restarting a
|
|
15
|
+
transaction that had not written), and a `COMMIT` of a transaction that had already failed, which Postgres turns
|
|
16
|
+
into a rollback — the connection's tenant is now unknown (`RlsTenant::UNKNOWN`): the next switch sets it, and a
|
|
17
|
+
reset resets it. The cost is one `SET` after a rollback; whether a transaction had failed is asked of the client
|
|
18
|
+
library, with no round trip. The rollback cases and the CI matrix below come from #42 (@david-pulgarin-skydropx).
|
|
19
|
+
- **Parallel tests on Rails 8.1.** `PgRls::ActiveRecord::TestDatabases` named every worker's RLS databases after it,
|
|
20
|
+
but Rails 8.1 names hidden configurations itself, so they were named twice (`test_db_1-1`) and no worker came up.
|
|
21
|
+
It now names only the ones Rails left alone, on every version.
|
|
22
|
+
|
|
23
|
+
### Compatibility
|
|
24
|
+
|
|
25
|
+
- The suite runs in CI on Rails 7.2, 8.0 and 8.1 — every version the gemspec allows — with parallel workers
|
|
26
|
+
(`gemfiles/`, not packaged). On Rails 8.1 the suite's coverage report is written again: the workers' teardown
|
|
27
|
+
had stopped SimpleCov in the main process too.
|
|
28
|
+
|
|
29
|
+
## [1.0.3] - 2026-10-03
|
|
30
|
+
|
|
31
|
+
### Fixes
|
|
32
|
+
|
|
33
|
+
- **A connection handed between threads could keep another tenant.** `Tenant#set_rls` skipped the `SET` when the
|
|
34
|
+
thread had set that connection before, going by a per-thread record of the connections it had set. If a connection
|
|
35
|
+
changed threads while that record still listed it — released mid-request, or used across threads, as in system
|
|
36
|
+
tests where the test thread and the server's threads share a pool — the first thread got it back already switched
|
|
37
|
+
by another one and ran as that tenant. Apps that hold the connection for the whole request (pg_rls's own
|
|
38
|
+
`PgRls::Record.connection` calls lease it) were not exposed. The tenant is now recorded on the connection itself
|
|
39
|
+
(`rls_tenant_id`), so the `SET` is skipped only when the connection is already on that tenant: no more statements
|
|
40
|
+
than before, and none skipped that were needed.
|
|
41
|
+
- A reconnect or a reset (`DISCARD ALL`) drops the session's tenant; the connection now knows it and sets it again.
|
|
42
|
+
- `Tenant.reset_rls_used_connections` takes the tenant off the connection it is given, whichever thread set it.
|
|
43
|
+
|
|
44
|
+
### Removed
|
|
45
|
+
|
|
46
|
+
- `Tenant.rls_connection_object_cache_by_thread` and its writer, the per-thread record the fix replaces.
|
|
47
|
+
|
|
4
48
|
## [1.0.1] - 2024-10-10
|
|
5
49
|
|
|
6
50
|
### 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
|
|
20
|
+
return connection if connection.rls_tenant_id.nil?
|
|
20
21
|
|
|
21
22
|
connection.exec_query("SET rls.tenant_id TO DEFAULT")
|
|
22
|
-
|
|
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
|
|
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,68 @@
|
|
|
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
|
+
# What the connection's tenant is after a rollback: nobody knows. Postgres undoes a SET made inside a
|
|
13
|
+
# transaction or savepoint that rolls back, so the session is back on whatever tenant it had before —
|
|
14
|
+
# which may be another tenant than the one recorded here. It equals no tenant, so the next switch sets
|
|
15
|
+
# the session again, and it is not nil, so a reset resets it.
|
|
16
|
+
UNKNOWN = Object.new.freeze
|
|
17
|
+
|
|
18
|
+
attr_accessor :rls_tenant_id
|
|
19
|
+
|
|
20
|
+
# A COMMIT rolls back instead when the transaction has already failed; whether it has is the client library's
|
|
21
|
+
# answer, with no round trip. (A COMMIT that fails is rolled back by Rails, which the hook below hears.)
|
|
22
|
+
def commit_db_transaction
|
|
23
|
+
rolls_back = aborted_transaction?
|
|
24
|
+
super
|
|
25
|
+
ensure
|
|
26
|
+
self.rls_tenant_id = UNKNOWN if rolls_back
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
# A new session has no tenant, whatever the connection's last one had: this runs on every connect,
|
|
32
|
+
# reconnect and reset (DISCARD ALL).
|
|
33
|
+
def configure_connection
|
|
34
|
+
super
|
|
35
|
+
self.rls_tenant_id = nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def exec_rollback_db_transaction
|
|
39
|
+
super
|
|
40
|
+
ensure
|
|
41
|
+
self.rls_tenant_id = UNKNOWN
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# ROLLBACK AND CHAIN: a nested transaction rolled back in one that had not written yet.
|
|
45
|
+
def exec_restart_db_transaction
|
|
46
|
+
super
|
|
47
|
+
ensure
|
|
48
|
+
self.rls_tenant_id = UNKNOWN
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def exec_rollback_to_savepoint(name = nil)
|
|
52
|
+
super
|
|
53
|
+
ensure
|
|
54
|
+
self.rls_tenant_id = UNKNOWN
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# A connection the client library cannot ask is left to the COMMIT, which raises the real error.
|
|
58
|
+
def aborted_transaction?
|
|
59
|
+
@raw_connection&.transaction_status == ::PG::PQTRANS_INERROR
|
|
60
|
+
rescue ::PG::Error
|
|
61
|
+
false
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(PgRls::ActiveRecord::ConnectionAdapters::RlsTenant)
|
|
@@ -3,13 +3,19 @@
|
|
|
3
3
|
module PgRls
|
|
4
4
|
module ActiveRecord
|
|
5
5
|
module TestDatabases # :nodoc:
|
|
6
|
+
# Each parallel worker gets databases of its own. Rails names them, but before 8.1 only the configurations it
|
|
7
|
+
# does not hide, and pg_rls's are hidden: those are named here — and only those Rails left alone, because from
|
|
8
|
+
# 8.1 on it names hidden ones too (`<database>_<i>`), and naming them again pointed every worker at a database
|
|
9
|
+
# that does not exist.
|
|
6
10
|
def create_and_load_schema(i, env_name:)
|
|
7
|
-
|
|
11
|
+
configs = PgRls::Record.configurations.configs_for(env_name: env_name, include_hidden: true)
|
|
12
|
+
.reject { |db_config| db_config.name == "primary" }
|
|
13
|
+
named = configs.to_h { |db_config| [db_config, db_config.database] }
|
|
8
14
|
|
|
9
|
-
|
|
10
|
-
next if db_config.name == "primary"
|
|
15
|
+
super
|
|
11
16
|
|
|
12
|
-
|
|
17
|
+
configs.each do |db_config|
|
|
18
|
+
db_config._database = "#{db_config.database}-#{i}" if db_config.database == named[db_config]
|
|
13
19
|
end
|
|
14
20
|
end
|
|
15
21
|
end
|
data/lib/pg_rls/version.rb
CHANGED
|
@@ -3,6 +3,24 @@
|
|
|
3
3
|
module PgRls
|
|
4
4
|
module ActiveRecord
|
|
5
5
|
module ConnectionAdapters
|
|
6
|
+
module RlsTenant
|
|
7
|
+
UNKNOWN: Object
|
|
8
|
+
|
|
9
|
+
attr_accessor rls_tenant_id: String | Object | nil
|
|
10
|
+
|
|
11
|
+
private def configure_connection: () -> void
|
|
12
|
+
|
|
13
|
+
def commit_db_transaction: () -> void
|
|
14
|
+
|
|
15
|
+
private def exec_rollback_db_transaction: () -> void
|
|
16
|
+
|
|
17
|
+
private def exec_restart_db_transaction: () -> void
|
|
18
|
+
|
|
19
|
+
private def exec_rollback_to_savepoint: (?String? name) -> void
|
|
20
|
+
|
|
21
|
+
private def aborted_transaction?: () -> bool
|
|
22
|
+
end
|
|
23
|
+
|
|
6
24
|
module ConnectionPool
|
|
7
25
|
attr_reader checkout_timeout: untyped
|
|
8
26
|
|
|
@@ -23,15 +41,6 @@ module PgRls
|
|
|
23
41
|
|
|
24
42
|
def set_rls: (ActiveRecord::ConnectionAdapters::PostgreSQLAdapter conn) -> Tenant
|
|
25
43
|
|
|
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
44
|
end
|
|
36
45
|
|
|
37
46
|
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.
|
|
4
|
+
version: 1.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Daniel Laloush
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-10-07 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
|