pg_rls 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/pg_rls/version.rb +1 -1
- data/lib/pg_rls.rb +23 -19
- 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: 5296cf19938df79d340f6da931acf0aee13e967720d964ec979773189634c155
|
4
|
+
data.tar.gz: ebf0278848a707e692198346dc734e8da9024c3b5e1325da760c4d7f7cdc30f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97465ed5ea998a4c6e6ae39aae82d08ae0663733ef0f442d4716431adb607f13b96fb44139d5c5be83696cbb044cfa778a918a74aa842906ae798869f0fb3145
|
7
|
+
data.tar.gz: c85948bfbcd16f610b2095f2900fee54d3abc3cd27b581eb282144e4b444341a0cfb50c2b1ef98e8cd9d395c22e6218d81fa19d2281df956bb3d8e5cf4e39204
|
data/Gemfile.lock
CHANGED
data/lib/pg_rls/version.rb
CHANGED
data/lib/pg_rls.rb
CHANGED
@@ -62,20 +62,19 @@ module PgRls
|
|
62
62
|
|
63
63
|
def admin_execute(query = nil, &)
|
64
64
|
current_tenant = PgRls::Tenant.fetch
|
65
|
-
|
66
|
-
self.as_db_admin = true
|
67
|
-
establish_new_connection!
|
65
|
+
establish_new_connection!(admin: true)
|
68
66
|
|
69
67
|
return ensure_block_execution(&) if block_given?
|
70
68
|
|
71
69
|
execute(query)
|
72
70
|
ensure
|
73
|
-
self.as_db_admin = false
|
74
71
|
establish_new_connection!
|
75
72
|
PgRls::Tenant.switch(current_tenant) if current_tenant.present?
|
76
73
|
end
|
77
74
|
|
78
|
-
def establish_new_connection!
|
75
|
+
def establish_new_connection!(admin: false)
|
76
|
+
self.as_db_admin = admin
|
77
|
+
|
79
78
|
execute_rls_in_shards do |connection_class, pool|
|
80
79
|
connection_class.remove_connection
|
81
80
|
connection_class.establish_connection(pool.db_config)
|
@@ -87,24 +86,20 @@ module PgRls
|
|
87
86
|
end
|
88
87
|
|
89
88
|
def on_each_tenant(&)
|
90
|
-
|
89
|
+
with_rls_connection do
|
90
|
+
result = []
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
Tenant.switch tenant.send(allowed_search_fields.first)
|
96
|
-
|
97
|
-
result << { tenant:, result: ensure_block_execution(tenant, &) }
|
98
|
-
end
|
92
|
+
main_model.find_each do |tenant|
|
93
|
+
allowed_search_fields = search_methods.map(&:to_s).intersection(main_model.column_names)
|
94
|
+
Tenant.switch tenant.send(allowed_search_fields.first)
|
99
95
|
|
100
|
-
|
96
|
+
result << { tenant:, result: ensure_block_execution(tenant, &) }
|
97
|
+
end
|
101
98
|
|
102
|
-
|
103
|
-
end
|
99
|
+
PgRls::Tenant.reset_rls!
|
104
100
|
|
105
|
-
|
106
|
-
|
107
|
-
establish_new_connection!
|
101
|
+
result
|
102
|
+
end
|
108
103
|
end
|
109
104
|
|
110
105
|
def execute_rls_in_shards
|
@@ -138,6 +133,15 @@ module PgRls
|
|
138
133
|
|
139
134
|
attr_writer :as_db_admin
|
140
135
|
|
136
|
+
def with_rls_connection(&)
|
137
|
+
reset_connection = admin_connection?
|
138
|
+
|
139
|
+
establish_new_connection! if reset_connection
|
140
|
+
ensure_block_execution(&)
|
141
|
+
ensure
|
142
|
+
establish_new_connection!(admin: true) if reset_connection
|
143
|
+
end
|
144
|
+
|
141
145
|
def ensure_block_execution(*, **)
|
142
146
|
yield(*, **).presence
|
143
147
|
end
|