pg_rls 0.0.2.6 → 0.0.2.6.2
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/lib/pg_rls/middleware/sidekiq/client.rb +16 -0
- data/lib/pg_rls/middleware/sidekiq/server.rb +18 -0
- data/lib/pg_rls/middleware/sidekiq.rb +11 -0
- data/lib/pg_rls/middleware.rb +8 -0
- data/lib/pg_rls/tenant.rb +25 -9
- data/lib/pg_rls/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87f7139b6e22ced3f4e32092df320ff675a995cfb5139b786da32de98087893e
|
4
|
+
data.tar.gz: 4840e5dcab17c0ed382734fff92d3ddaf9a905b61aeba05a763fcb43fc68fc01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
24
|
-
|
25
|
-
|
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.
|
66
|
-
|
67
|
-
|
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)
|
@@ -81,7 +96,8 @@ module PgRls
|
|
81
96
|
end
|
82
97
|
|
83
98
|
def find_tenant_by_method(resource, method)
|
84
|
-
PgRls.main_model
|
99
|
+
look_up_value = resource.is_a?(PgRls.main_model) ? resource.send(method) : resource
|
100
|
+
PgRls.main_model.send("find_by_#{method}!", look_up_value)
|
85
101
|
rescue ActiveRecord::RecordNotFound
|
86
102
|
nil
|
87
103
|
end
|
data/lib/pg_rls/version.rb
CHANGED
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
|
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-
|
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
|