rls_multi_tenant 0.2.7 → 0.2.9
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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a289abc220420817eb9b817ea2b40cdc44021491fe362f05d2d0dd982ffb21ab
|
|
4
|
+
data.tar.gz: 65c2f9f291a39026ac829f7252298eb527cd0d25c79a4ead1bc461a978d4d3df
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 896ae1cd8155bc3c2c47064776a1780459e0f2a29c5f307e186514b0a3a2be21ff682a940e6c09e989b6a9033f230fdf872865b02f8152d34af07888f041175c
|
|
7
|
+
data.tar.gz: d40bd80268219f951db636be6f8e05eefa9ab82c172ed4085eb2e18597e94cf3cc902d76bf70fb803c497e2a8c629e4c13982c9fcd67e2311a61e90fd1b5b226
|
|
@@ -8,6 +8,7 @@ module RlsMultiTenant
|
|
|
8
8
|
SET_TENANT_ID_SQL = 'SET %s = %s'
|
|
9
9
|
RESET_TENANT_ID_SQL = 'RESET %s'
|
|
10
10
|
|
|
11
|
+
# rubocop:disable Metrics/BlockLength
|
|
11
12
|
class_methods do
|
|
12
13
|
def tenant_session_var
|
|
13
14
|
"rls.#{RlsMultiTenant.tenant_id_column}"
|
|
@@ -15,12 +16,15 @@ module RlsMultiTenant
|
|
|
15
16
|
|
|
16
17
|
# Switch tenant context for a block
|
|
17
18
|
def switch(tenant_or_id)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
connection.execute format(SET_TENANT_ID_SQL, tenant_session_var, connection.quote(tenant_id))
|
|
19
|
+
previous_tenant_id = current_tenant_id
|
|
20
|
+
switch!(tenant_or_id)
|
|
21
21
|
yield
|
|
22
22
|
ensure
|
|
23
|
-
|
|
23
|
+
begin
|
|
24
|
+
switch!(previous_tenant_id)
|
|
25
|
+
rescue StandardError => _e
|
|
26
|
+
reset!
|
|
27
|
+
end
|
|
24
28
|
end
|
|
25
29
|
|
|
26
30
|
# Switch tenant context permanently (until reset)
|
|
@@ -39,8 +43,8 @@ module RlsMultiTenant
|
|
|
39
43
|
def current
|
|
40
44
|
return nil unless connection.active?
|
|
41
45
|
|
|
42
|
-
result = connection.execute("
|
|
43
|
-
tenant_id = result.first&.dig(
|
|
46
|
+
result = connection.execute("SELECT current_setting('#{tenant_session_var}', true) AS tenant_id")
|
|
47
|
+
tenant_id = result.first&.dig('tenant_id')
|
|
44
48
|
|
|
45
49
|
return nil if tenant_id.blank?
|
|
46
50
|
|
|
@@ -51,6 +55,15 @@ module RlsMultiTenant
|
|
|
51
55
|
|
|
52
56
|
private
|
|
53
57
|
|
|
58
|
+
def current_tenant_id
|
|
59
|
+
return nil unless connection.active?
|
|
60
|
+
|
|
61
|
+
result = connection.execute("SELECT current_setting('#{tenant_session_var}', true) AS tenant_id")
|
|
62
|
+
result.first&.dig('tenant_id')
|
|
63
|
+
rescue ActiveRecord::StatementInvalid, PG::Error
|
|
64
|
+
nil
|
|
65
|
+
end
|
|
66
|
+
|
|
54
67
|
def extract_tenant_id(tenant_or_id)
|
|
55
68
|
case tenant_or_id
|
|
56
69
|
when ->(obj) { obj.is_a?(RlsMultiTenant.tenant_class) }
|
|
@@ -71,6 +84,7 @@ module RlsMultiTenant
|
|
|
71
84
|
raise StandardError, "#{RlsMultiTenant.tenant_class_name} with id '#{tenant_id}' not found"
|
|
72
85
|
end
|
|
73
86
|
end
|
|
87
|
+
# rubocop:enable Metrics/BlockLength
|
|
74
88
|
|
|
75
89
|
# Instance methods
|
|
76
90
|
def switch(tenant_or_id, &block)
|