rls_multi_tenant 0.2.8 → 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 +4 -4
- data/lib/rls_multi_tenant/concerns/tenant_context.rb +10 -26
- data/lib/rls_multi_tenant/version.rb +1 -1
- metadata +1 -1
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
|
|
@@ -14,22 +14,17 @@ module RlsMultiTenant
|
|
|
14
14
|
"rls.#{RlsMultiTenant.tenant_id_column}"
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def tenant_stack
|
|
18
|
-
Thread.current[:"#{name}_tenant_stack"] ||= []
|
|
19
|
-
end
|
|
20
|
-
|
|
21
17
|
# Switch tenant context for a block
|
|
22
18
|
def switch(tenant_or_id)
|
|
23
|
-
tenant_id = extract_tenant_id(tenant_or_id)
|
|
24
|
-
validate_tenant_exists!(tenant_id)
|
|
25
|
-
|
|
26
19
|
previous_tenant_id = current_tenant_id
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
connection.execute format(SET_TENANT_ID_SQL, tenant_session_var, connection.quote(tenant_id))
|
|
20
|
+
switch!(tenant_or_id)
|
|
30
21
|
yield
|
|
31
22
|
ensure
|
|
32
|
-
|
|
23
|
+
begin
|
|
24
|
+
switch!(previous_tenant_id)
|
|
25
|
+
rescue StandardError => _e
|
|
26
|
+
reset!
|
|
27
|
+
end
|
|
33
28
|
end
|
|
34
29
|
|
|
35
30
|
# Switch tenant context permanently (until reset)
|
|
@@ -41,7 +36,6 @@ module RlsMultiTenant
|
|
|
41
36
|
|
|
42
37
|
# Reset tenant context
|
|
43
38
|
def reset!
|
|
44
|
-
tenant_stack.clear
|
|
45
39
|
connection.execute format(RESET_TENANT_ID_SQL, tenant_session_var)
|
|
46
40
|
end
|
|
47
41
|
|
|
@@ -49,8 +43,8 @@ module RlsMultiTenant
|
|
|
49
43
|
def current
|
|
50
44
|
return nil unless connection.active?
|
|
51
45
|
|
|
52
|
-
result = connection.execute("
|
|
53
|
-
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')
|
|
54
48
|
|
|
55
49
|
return nil if tenant_id.blank?
|
|
56
50
|
|
|
@@ -64,22 +58,12 @@ module RlsMultiTenant
|
|
|
64
58
|
def current_tenant_id
|
|
65
59
|
return nil unless connection.active?
|
|
66
60
|
|
|
67
|
-
result = connection.execute("
|
|
68
|
-
result.first&.dig(
|
|
61
|
+
result = connection.execute("SELECT current_setting('#{tenant_session_var}', true) AS tenant_id")
|
|
62
|
+
result.first&.dig('tenant_id')
|
|
69
63
|
rescue ActiveRecord::StatementInvalid, PG::Error
|
|
70
64
|
nil
|
|
71
65
|
end
|
|
72
66
|
|
|
73
|
-
def restore_tenant_context!
|
|
74
|
-
previous_tenant_id = tenant_stack.pop
|
|
75
|
-
|
|
76
|
-
if previous_tenant_id.present?
|
|
77
|
-
connection.execute format(SET_TENANT_ID_SQL, tenant_session_var, connection.quote(previous_tenant_id))
|
|
78
|
-
else
|
|
79
|
-
connection.execute format(RESET_TENANT_ID_SQL, tenant_session_var)
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
67
|
def extract_tenant_id(tenant_or_id)
|
|
84
68
|
case tenant_or_id
|
|
85
69
|
when ->(obj) { obj.is_a?(RlsMultiTenant.tenant_class) }
|