pg_rls 0.0.2.6.1 → 0.0.2.6.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: b766acd4413c9b9fad35fbc7f79e8f50cbcd97abf6644a1c2976584174db33ca
4
- data.tar.gz: 8aa3f8df6e04599b94c3677e58db9bb99c8207fbe2a195f5da92555cad52e758
3
+ metadata.gz: 995d9d7741a7402f3a17cf8737bdbad60cdb1aae96460058ed1e02eba59af374
4
+ data.tar.gz: 013bc7af4c7ffabf0d9cde928882efa3a35af791673b51539c9957f4a54d6786
5
5
  SHA512:
6
- metadata.gz: d059f46edf63a45f3ebf2fb4cbacd53b3342d52a627edd596cacfb289857adcc8a0be3ff39c1e7f6a9fe268d280df119487c1760f884e87b784d9d945138509f
7
- data.tar.gz: b431079863091428e09272154e91bdffe50e7e375e35f03984698857b4e80983171cee4fb0db3674b054da299d551135e585c090f356c17e16642edf1262eb88
6
+ metadata.gz: 3624e007d9826575c5713285b7c21c6410853c9970f0cf8f79b2ec71c5d99aa0f009fc32c9756de6131801e5cbc190b27b431714e036c2a139217c4f150f7a19
7
+ data.tar.gz: 8631b721048d4478da8d30f1cc294452cfd9a48f22e6eab8bf9748e345b3a5d26f886c92400035e3ff5c1ab993fc7759b26bbb1675b6f90b536dfcf271fe3b96
@@ -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
@@ -5,18 +5,23 @@ module PgRls
5
5
  module MultiTenancy
6
6
  def self.included(base)
7
7
  base.class_eval do
8
- before_action :switch_tenant!
8
+ around_action :switch_tenant!
9
+
10
+ def current_tenant
11
+ @current_tenant ||= request.subdomain
12
+ end
13
+ helper_method :current_tenant
9
14
  end
10
15
  end
11
16
 
12
17
  private
13
18
 
14
- def switch_tenant!
15
- tenant = request.subdomain
16
- Tenant.switch!(tenant)
17
- session[:_tenant] = tenant
18
- rescue PgRls::Errors::TenantNotFound, ActiveRecord::RecordNotFound
19
- Tenant.switch(session[:_tenant])
19
+ def switch_tenant!(&block)
20
+ fetched_tenant = session[:_tenant] || current_tenant
21
+ Tenant.with_tenant(fetched_tenant) do |tenant|
22
+ session[:_tenant] = tenant
23
+ block.call(tenant)
24
+ end
20
25
  rescue NoMethodError
21
26
  session[:_tenant] = nil
22
27
  redirect_to '/'
data/lib/pg_rls/tenant.rb CHANGED
@@ -7,34 +7,50 @@ 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
29
41
 
30
42
  def fetch
43
+ fetch!
44
+ rescue ActiveRecord::StatementInvalid
45
+ 'no tenant is selected'
46
+ end
47
+
48
+ def fetch!
31
49
  @fetch ||= PgRls.main_model.find_by!(
32
50
  tenant_id: PgRls.connection_class.connection.execute(
33
51
  "SELECT current_setting('rls.tenant_id')"
34
52
  ).getvalue(0, 0)
35
53
  )
36
- rescue ActiveRecord::StatementInvalid
37
- 'no tenant is selected'
38
54
  end
39
55
 
40
56
  def find_main_model
@@ -62,9 +78,12 @@ module PgRls
62
78
 
63
79
  raise PgRls::Errors::TenantNotFound if tenant.blank?
64
80
 
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)}'"
81
+ connection_adapter.connection.transaction do
82
+ connection_adapter.connection.execute(format('SET rls.tenant_id = %s',
83
+ connection_adapter.connection.quote(tenant.tenant_id)))
84
+ end
85
+
86
+ tenant
68
87
  end
69
88
 
70
89
  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.3'
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.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: 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