pg_rls 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43fa134f2b679ac9b15406226a50a8b7186a0845c030e931e40f17ba226b5a1c
4
- data.tar.gz: 5580815284fecca5bb67435e54e0a9bb775cc56f8524b2bb01a3b85da5c15901
3
+ metadata.gz: 5296cf19938df79d340f6da931acf0aee13e967720d964ec979773189634c155
4
+ data.tar.gz: ebf0278848a707e692198346dc734e8da9024c3b5e1325da760c4d7f7cdc30f2
5
5
  SHA512:
6
- metadata.gz: 976399696e8c22b692d389c99ae7918e66d67c02f485fa4b6ad157f6940fb215bb1440753086c8ba4e26618edc53de04ad8bd4a27198c03739265cc284598001
7
- data.tar.gz: 4342defbd2a76a1927e93634b26cd1c1103afc011ff2a24066a257a8e0d8dd28bb73b2adf3925781ea8b5a45785301627495293827e27fb43c606e9036999682
6
+ metadata.gz: 97465ed5ea998a4c6e6ae39aae82d08ae0663733ef0f442d4716431adb607f13b96fb44139d5c5be83696cbb044cfa778a918a74aa842906ae798869f0fb3145
7
+ data.tar.gz: c85948bfbcd16f610b2095f2900fee54d3abc3cd27b581eb282144e4b444341a0cfb50c2b1ef98e8cd9d395c22e6218d81fa19d2281df956bb3d8e5cf4e39204
data/.rubocop.yml CHANGED
@@ -21,3 +21,6 @@ Metrics/BlockLength:
21
21
  Security/MarshalLoad:
22
22
  Exclude:
23
23
  - 'lib/pg_rls/middleware/set_reset_connection.rb'
24
+ Metrics/ModuleLength:
25
+ Exclude:
26
+ - 'lib/pg_rls.rb'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pg_rls (2.0.0)
4
+ pg_rls (0.1.2)
5
5
  bundler (~> 2.2)
6
6
 
7
7
  GEM
@@ -12,11 +12,11 @@ module PgRls
12
12
  end
13
13
 
14
14
  def load_tenant_attribute!(msg)
15
- if PgRls.username == PgRls.current_db_username
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 admin_username?
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRls
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
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
- result = []
91
- main_model.find_each do |tenant|
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
- result << { tenant:, result: ensure_block_execution(tenant, &) }
96
- 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)
97
95
 
98
- PgRls::Tenant.reset_rls!
96
+ result << { tenant:, result: ensure_block_execution(tenant, &) }
97
+ end
99
98
 
100
- result
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush