pg_rls 0.1.0 → 0.1.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/.rubocop.yml +3 -0
- data/Gemfile.lock +1 -1
- data/lib/pg_rls/middleware/sidekiq/client.rb +3 -3
- data/lib/pg_rls/tenant.rb +1 -5
- data/lib/pg_rls/version.rb +1 -1
- data/lib/pg_rls.rb +28 -13
- 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/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -12,11 +12,11 @@ module PgRls
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def load_tenant_attribute!(msg)
|
15
|
-
if PgRls.
|
15
|
+
if PgRls.admin_connection?
|
16
|
+
msg['admin'] = true
|
17
|
+
else
|
16
18
|
tenant = PgRls::Tenant.fetch!
|
17
19
|
msg['pg_rls'] = tenant.id
|
18
|
-
else
|
19
|
-
msg['admin'] = true
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/pg_rls/tenant.rb
CHANGED
@@ -81,7 +81,7 @@ module PgRls
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def find_tenant(resource)
|
84
|
-
raise PgRls::Errors::AdminUsername if
|
84
|
+
raise PgRls::Errors::AdminUsername if PgRls.admin_connection?
|
85
85
|
|
86
86
|
reset_rls!
|
87
87
|
|
@@ -95,10 +95,6 @@ module PgRls
|
|
95
95
|
raise PgRls::Errors::TenantNotFound if tenant.blank?
|
96
96
|
end
|
97
97
|
|
98
|
-
def admin_username?
|
99
|
-
PgRls.username != PgRls.current_db_username
|
100
|
-
end
|
101
|
-
|
102
98
|
def find_tenant_by_method(resource, method)
|
103
99
|
look_up_value = resource.is_a?(PgRls.main_model) ? resource.send(method) : resource
|
104
100
|
PgRls.main_model.send("find_by_#{method}!", look_up_value)
|
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,17 +86,20 @@ module PgRls
|
|
87
86
|
end
|
88
87
|
|
89
88
|
def on_each_tenant(&)
|
90
|
-
|
91
|
-
|
92
|
-
allowed_search_fields = search_methods.map(&:to_s).intersection(main_model.column_names)
|
93
|
-
Tenant.switch tenant.send(allowed_search_fields.first)
|
89
|
+
with_rls_connection do
|
90
|
+
result = []
|
94
91
|
|
95
|
-
|
96
|
-
|
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)
|
97
95
|
|
98
|
-
|
96
|
+
result << { tenant:, result: ensure_block_execution(tenant, &) }
|
97
|
+
end
|
99
98
|
|
100
|
-
|
99
|
+
PgRls::Tenant.reset_rls!
|
100
|
+
|
101
|
+
result
|
102
|
+
end
|
101
103
|
end
|
102
104
|
|
103
105
|
def execute_rls_in_shards
|
@@ -123,10 +125,23 @@ module PgRls
|
|
123
125
|
@as_db_admin || false
|
124
126
|
end
|
125
127
|
|
128
|
+
def admin_connection?
|
129
|
+
current_db_username != username
|
130
|
+
end
|
131
|
+
|
126
132
|
private
|
127
133
|
|
128
134
|
attr_writer :as_db_admin
|
129
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
|
+
|
130
145
|
def ensure_block_execution(*, **)
|
131
146
|
yield(*, **).presence
|
132
147
|
end
|