pg_rls 0.0.2 → 0.0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pg_rls/tenant.rb +28 -9
- data/lib/pg_rls/version.rb +1 -1
- data/lib/pg_rls.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96f6c928e78088e4781845c1243c1af8ceee143f99863af133f0aae2d40b1df8
|
4
|
+
data.tar.gz: 4bb596321621511c913d8ee98a71918df75b84e3c4738ae44e9f1ee0974c74af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ab1085addd4d796fb221e2850459b26c0e520192916a7285fe5c7f0ddd6298cb5576f826d133f06bd9be5c2d89d0895c9446ebcc7b6c5413e0d86b9972b2721
|
7
|
+
data.tar.gz: f0b744bc68654c77afeabadf2af756fe5787602b2f26262172acd6ddca51928a405b75f46f0c7824dfdfbe5c3fde0d4b03bec45c803680b37ce4a903947f06c9
|
data/lib/pg_rls/tenant.rb
CHANGED
@@ -9,19 +9,26 @@ module PgRls
|
|
9
9
|
def switch(resource)
|
10
10
|
switch_tenant!(resource)
|
11
11
|
rescue StandardError => e
|
12
|
-
|
13
|
-
|
12
|
+
Rails.logger.info('connection was not made')
|
13
|
+
Rails.logger.info(e)
|
14
14
|
end
|
15
15
|
|
16
16
|
def switch!(resource)
|
17
17
|
switch_tenant!(resource)
|
18
18
|
rescue StandardError => e
|
19
|
-
|
19
|
+
Rails.logger.info('connection was not made')
|
20
20
|
raise e
|
21
21
|
end
|
22
22
|
|
23
|
+
def with_tenant(resource)
|
24
|
+
switch_tenant!(resource)
|
25
|
+
yield
|
26
|
+
ensure
|
27
|
+
reset_rls!
|
28
|
+
end
|
29
|
+
|
23
30
|
def fetch
|
24
|
-
@fetch ||= PgRls.main_model.find_by_tenant_id(
|
31
|
+
@fetch ||= PgRls.main_model.find_by_tenant_id!(
|
25
32
|
PgRls.connection_class.connection.execute(
|
26
33
|
"SELECT current_setting('rls.tenant_id')"
|
27
34
|
).getvalue(0, 0)
|
@@ -30,10 +37,15 @@ module PgRls
|
|
30
37
|
'no tenant is selected'
|
31
38
|
end
|
32
39
|
|
40
|
+
def reset_rls!
|
41
|
+
@fetch = nil
|
42
|
+
@tenant = nil
|
43
|
+
PgRls.connection_class.connection.execute('RESET rls.tenant_id')
|
44
|
+
end
|
45
|
+
|
33
46
|
private
|
34
47
|
|
35
48
|
def switch_tenant!(resource)
|
36
|
-
@fetch = nil
|
37
49
|
connection_adapter = PgRls.connection_class
|
38
50
|
find_tenant(resource)
|
39
51
|
|
@@ -45,14 +57,21 @@ module PgRls
|
|
45
57
|
end
|
46
58
|
|
47
59
|
def find_tenant(resource)
|
48
|
-
|
60
|
+
reset_rls!
|
49
61
|
|
50
62
|
PgRls.search_methods.each do |method|
|
63
|
+
return if @tenant.present?
|
64
|
+
|
51
65
|
@method = method
|
52
|
-
@tenant
|
53
|
-
rescue NoMethodError, ActiveRecord::RecordNotFound => e
|
54
|
-
@error = e
|
66
|
+
@tenant = find_tenant_by_method(resource, method)
|
55
67
|
end
|
68
|
+
|
69
|
+
raise PgRls::Errors::TenantNotFound if tenant.nil?
|
70
|
+
end
|
71
|
+
|
72
|
+
def find_tenant_by_method(resource, method)
|
73
|
+
PgRls.main_model.send("find_by_#{method}!", resource)
|
74
|
+
rescue ActiveRecord::RecordNotFound => e
|
56
75
|
end
|
57
76
|
end
|
58
77
|
end
|
data/lib/pg_rls/version.rb
CHANGED
data/lib/pg_rls.rb
CHANGED
@@ -48,9 +48,9 @@ module PgRls
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def establish_new_connection
|
51
|
-
|
52
|
-
|
53
|
-
)
|
51
|
+
ActiveRecord::Base.connection.disconnect!
|
52
|
+
|
53
|
+
connection_class.establish_connection(**database_configuration)
|
54
54
|
end
|
55
55
|
|
56
56
|
def admin_execute(query = nil)
|
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
|
4
|
+
version: 0.0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Laloush
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|