pg_rls 1.0.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a75a646d5cd7efb87d4a4343d3577a307b2a4c8750a9a536d448205f3e4c59b8
4
- data.tar.gz: 0b5f950a922336c4b8e343a21611dc1f299c934da8829a0b0d9f37862f04ef3e
3
+ metadata.gz: 8a6d6413f258e20cc03352e1327614f91bb5bcba00ce958a9a9082115e803d18
4
+ data.tar.gz: a89ecb17edd4fda275f660560fa33f3c15de9b7cb96e0ffdd9cb226a952d7ce4
5
5
  SHA512:
6
- metadata.gz: 9697b3afdb0b1489fbcf349c44c751f5fc8c495a8a3e10f59c91f2d23be4cc7576f44960c21964817c3d1dea7dbef88954907fdb0fb3641c1ada2eb3f723742b
7
- data.tar.gz: e8894ef39e132e970d7527ed01057e737900be8f44014331442eb2e4397aa6b805f563ff89c38c575b355a480258305c0a446fd890d707b34735f6dee9068579
6
+ metadata.gz: 9c749cc2d7a7f88f5ae996d8b24b30589f3329cd156cd7900600a5be0ae45766408e5d870eb042819a5a4548b02ac4f865685b6829ea9d77792fd56a4944d02f
7
+ data.tar.gz: 4a9fd2b0b6c4dee5a458d6871bb6c51f27ff6a88fefa22ef1aba94504a3c44d3020728a75ff9b59488a42dd36e1ac95e23dfe14d7c4e61771f5e5fe40402c7e9
data/CHANGELOG.md CHANGED
@@ -1,6 +1,31 @@
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
+
4
29
  ## [1.0.3] - 2026-10-03
5
30
 
6
31
  ### Fixes
@@ -9,8 +9,23 @@ module PgRls
9
9
  # threads, and a thread going by its own record of what it once set would skip a connection another thread
10
10
  # has since switched to another tenant — and run as that tenant.
11
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
+
12
18
  attr_accessor :rls_tenant_id
13
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
+
14
29
  private
15
30
 
16
31
  # A new session has no tenant, whatever the connection's last one had: this runs on every connect,
@@ -19,6 +34,32 @@ module PgRls
19
34
  super
20
35
  self.rls_tenant_id = nil
21
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
22
63
  end
23
64
  end
24
65
  end
@@ -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
- super
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
- PgRls::Record.configurations.configs_for(env_name: env_name, include_hidden: true).each do |db_config|
10
- next if db_config.name == "primary"
15
+ super
11
16
 
12
- db_config._database = "#{db_config.database}-#{i}"
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  # :nocov:
4
4
  module PgRls
5
- VERSION = "1.0.3"
5
+ VERSION = "1.0.4"
6
6
  end
7
7
  # :nocov:
@@ -4,9 +4,21 @@ module PgRls
4
4
  module ActiveRecord
5
5
  module ConnectionAdapters
6
6
  module RlsTenant
7
- attr_accessor rls_tenant_id: String?
7
+ UNKNOWN: Object
8
+
9
+ attr_accessor rls_tenant_id: String | Object | nil
8
10
 
9
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
10
22
  end
11
23
 
12
24
  module ConnectionPool
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.3
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: 2026-10-03 00:00:00.000000000 Z
11
+ date: 2026-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord