pg_rls 0.0.2.6.1 → 0.0.2.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b766acd4413c9b9fad35fbc7f79e8f50cbcd97abf6644a1c2976584174db33ca
4
- data.tar.gz: 8aa3f8df6e04599b94c3677e58db9bb99c8207fbe2a195f5da92555cad52e758
3
+ metadata.gz: 87f7139b6e22ced3f4e32092df320ff675a995cfb5139b786da32de98087893e
4
+ data.tar.gz: 4840e5dcab17c0ed382734fff92d3ddaf9a905b61aeba05a763fcb43fc68fc01
5
5
  SHA512:
6
- metadata.gz: d059f46edf63a45f3ebf2fb4cbacd53b3342d52a627edd596cacfb289857adcc8a0be3ff39c1e7f6a9fe268d280df119487c1760f884e87b784d9d945138509f
7
- data.tar.gz: b431079863091428e09272154e91bdffe50e7e375e35f03984698857b4e80983171cee4fb0db3674b054da299d551135e585c090f356c17e16642edf1262eb88
6
+ metadata.gz: 03d7a09af5bf048f6aa0b1473d5b3c6ccc0a83c0068efa2e84dd75a016a4e39c353fa6bdc3e5d213961129860f08c3b9a79f2824b05d33a90fafdfd4d26e3d0b
7
+ data.tar.gz: 89e3ced934194385d2d851880c4ef675020d5a706135dd4cef4de8127106ddb158385586be559ac84b1648a23965e50e764d76fe5beace11c583912fdc7d30f8
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nocov:
4
+ module PgRls
5
+ module Middleware
6
+ module Sidekiq
7
+ # Set PgRls Policies
8
+ class Client
9
+ def call(_job_class, msg, _queue, _redis_pool)
10
+ msg['pg_rls'] = PgRls::Tenant.fetch.id
11
+ yield
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nocov:
4
+ module PgRls
5
+ module Middleware
6
+ module Sidekiq
7
+ # Set PgRls Policies
8
+ class Server
9
+ def call(_job_instance, msg, _queue)
10
+ PgRls::Tenant.with_tenant(msg['pg_rls']) do
11
+ process_class = msg['args'].first['job_class']
12
+ yield
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sidekiq/client'
4
+ require_relative 'sidekiq/server'
5
+
6
+ module PgRls
7
+ module Middleware
8
+ module Sidekiq
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'middleware/sidekiq'
4
+
5
+ module PgRls
6
+ module Middleware
7
+ end
8
+ end
data/lib/pg_rls/tenant.rb CHANGED
@@ -7,22 +7,34 @@ module PgRls
7
7
  attr_reader :tenant
8
8
 
9
9
  def switch(resource)
10
- switch_tenant!(resource)
10
+ tenant = switch_tenant!(resource)
11
+
12
+ "RLS changed to '#{tenant.id}'"
11
13
  rescue StandardError => e
12
14
  Rails.logger.info('connection was not made')
13
15
  Rails.logger.info(e)
16
+ nil
14
17
  end
15
18
 
16
19
  def switch!(resource)
17
- switch_tenant!(resource)
20
+ tenant = switch_tenant!(resource)
21
+
22
+ "RLS changed to '#{tenant.id}'"
18
23
  rescue StandardError => e
19
24
  Rails.logger.info('connection was not made')
20
25
  raise e
21
26
  end
22
27
 
23
- def with_tenant(resource)
24
- switch_tenant!(resource)
25
- yield
28
+ def find_each(&block)
29
+ PgRls.main_model.find_each do |tenant|
30
+ with_tenant(tenant, &block)
31
+ end
32
+ end
33
+
34
+ def with_tenant(resource, &block)
35
+ tenant = switch_tenant!(resource)
36
+
37
+ block.call(tenant) if block_given?
26
38
  ensure
27
39
  reset_rls!
28
40
  end
@@ -62,9 +74,12 @@ module PgRls
62
74
 
63
75
  raise PgRls::Errors::TenantNotFound if tenant.blank?
64
76
 
65
- connection_adapter.connection.execute(format('SET rls.tenant_id = %s',
66
- connection_adapter.connection.quote(tenant.tenant_id)))
67
- "RLS changed to '#{tenant.send(@method)}'"
77
+ connection_adapter.connection.transaction do
78
+ connection_adapter.connection.execute(format('SET rls.tenant_id = %s',
79
+ connection_adapter.connection.quote(tenant.tenant_id)))
80
+ end
81
+
82
+ tenant
68
83
  end
69
84
 
70
85
  def find_tenant(resource)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRls
4
- VERSION = '0.0.2.6.1'
4
+ VERSION = '0.0.2.6.2'
5
5
  end
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: 0.0.2.6.1
4
+ version: 0.0.2.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-13 00:00:00.000000000 Z
11
+ date: 2023-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -64,6 +64,10 @@ files:
64
64
  - lib/pg_rls/database/prepared.rb
65
65
  - lib/pg_rls/database/tasks/admin_database.rake
66
66
  - lib/pg_rls/errors/tenant_not_found.rb
67
+ - lib/pg_rls/middleware.rb
68
+ - lib/pg_rls/middleware/sidekiq.rb
69
+ - lib/pg_rls/middleware/sidekiq/client.rb
70
+ - lib/pg_rls/middleware/sidekiq/server.rb
67
71
  - lib/pg_rls/multi_tenancy.rb
68
72
  - lib/pg_rls/railtie.rb
69
73
  - lib/pg_rls/schema/down_statements.rb