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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b51a33bf2c5a0518a96142e8b07e303b32419719dfce1715ee237a0348d19c9
4
- data.tar.gz: 3bba1aaa681ab9a6d0defff9110c5f2077b08ff6a07332ad429cd040d8c84a7a
3
+ metadata.gz: 5296cf19938df79d340f6da931acf0aee13e967720d964ec979773189634c155
4
+ data.tar.gz: ebf0278848a707e692198346dc734e8da9024c3b5e1325da760c4d7f7cdc30f2
5
5
  SHA512:
6
- metadata.gz: 3ff073704aeca1d9e1b3e8673353deeca756d06b95a32f62fe85ac65cd3f145007c68d6b0b980b78221bdd0f4330421fc540d8efdc7e11600d8adaada1aa105c
7
- data.tar.gz: 11711ea29d21eef5d0dccbdfc5916074fe1a98089752f7fc0095ca1d514b40f5c40cb4b0a7a56688160a8afeb67414c3902d88152732774ad62fcbd6c7294140
6
+ metadata.gz: 97465ed5ea998a4c6e6ae39aae82d08ae0663733ef0f442d4716431adb607f13b96fb44139d5c5be83696cbb044cfa778a918a74aa842906ae798869f0fb3145
7
+ data.tar.gz: c85948bfbcd16f610b2095f2900fee54d3abc3cd27b581eb282144e4b444341a0cfb50c2b1ef98e8cd9d395c22e6218d81fa19d2281df956bb3d8e5cf4e39204
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pg_rls (0.1.1)
4
+ pg_rls (0.1.2)
5
5
  bundler (~> 2.2)
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRls
4
- VERSION = '0.1.1'
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,24 +86,20 @@ module PgRls
87
86
  end
88
87
 
89
88
  def on_each_tenant(&)
90
- set_rls_connection! if admin_connection?
89
+ with_rls_connection do
90
+ result = []
91
91
 
92
- result = []
93
- main_model.find_each do |tenant|
94
- allowed_search_fields = search_methods.map(&:to_s).intersection(main_model.column_names)
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
- PgRls::Tenant.reset_rls!
96
+ result << { tenant:, result: ensure_block_execution(tenant, &) }
97
+ end
101
98
 
102
- result
103
- end
99
+ PgRls::Tenant.reset_rls!
104
100
 
105
- def set_rls_connection!
106
- self.as_db_admin = false
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
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush