pg_rls 0.1.1 → 0.1.3

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: 0e0133ebb0867d0bfeeea122f6ddef47f861c6b17e2b8b65a07acdca4768c66a
4
+ data.tar.gz: 5bd700b9dd72150b01e72086f07795772cbb3f3b512e7106dbf58b2850713414
5
5
  SHA512:
6
- metadata.gz: 3ff073704aeca1d9e1b3e8673353deeca756d06b95a32f62fe85ac65cd3f145007c68d6b0b980b78221bdd0f4330421fc540d8efdc7e11600d8adaada1aa105c
7
- data.tar.gz: 11711ea29d21eef5d0dccbdfc5916074fe1a98089752f7fc0095ca1d514b40f5c40cb4b0a7a56688160a8afeb67414c3902d88152732774ad62fcbd6c7294140
6
+ metadata.gz: af0c6529cd940354cbaf1432c7086e1ae336f0d160df036108820873f31699c580833b7b153956e1b599b26946de15e8bfd5eda2dc94e10fa8b0b757fc94bcf1
7
+ data.tar.gz: 04feda60ca3f7fa0e98f096246db9ba2d4eecac158af08f719631e7f078b224e5dc33b660e92129a6eb70e217bb7e550fdf74843015b1df988272954d756192c
@@ -16,6 +16,8 @@ module PgRls
16
16
  end
17
17
  super
18
18
  end
19
+ ENVIRONMENT_PATH = 'config/environment.rb'
20
+
19
21
  APPLICATION_LINE = 'class Application < Rails::Application'
20
22
  APPLICATION_PATH = 'config/application.rb'
21
23
 
@@ -50,6 +52,12 @@ module PgRls
50
52
  template 'pg_rls.rb.tt', 'config/initializers/pg_rls.rb'
51
53
  end
52
54
 
55
+ def inject_include_to_environment
56
+ return if environment_already_included?
57
+
58
+ prepend_to_file(ENVIRONMENT_PATH, "\nrequire_relative 'initializers/pg_rls'\n")
59
+ end
60
+
53
61
  def inject_include_to_application
54
62
  return if aplication_already_included?
55
63
 
@@ -74,6 +82,10 @@ module PgRls
74
82
  File.readlines(APPLICATION_PATH).grep(/config.active_record.schema_format = :sql/).any?
75
83
  end
76
84
 
85
+ def environment_already_included?
86
+ File.readlines(ENVIRONMENT_PATH).grep(/require_relative 'initializers\/pg_rls'/).any?
87
+ end
88
+
77
89
  def initialize_error_text
78
90
  <<~ERROR
79
91
  TO DO
@@ -28,19 +28,5 @@ module PgRls
28
28
  session[:_tenant] = nil
29
29
  raise PgRls::Errors::TenantNotFound, 'No tenant was found'
30
30
  end
31
-
32
- def switch_tenant_by_resource!(resource = nil)
33
- Tenant.switch!(resource)
34
- session[:_tenant] = resource
35
- rescue PgRls::Errors::TenantNotFound
36
- Tenant.switch(session[:_tenant])
37
- rescue NoMethodError
38
- session[:tenant] = nil
39
- redirect_to '/'
40
- end
41
-
42
- def tenant_match_session_cookies?
43
- session[:_tenant] == request.subdomain
44
- end
45
31
  end
46
32
  end
data/lib/pg_rls/tenant.rb CHANGED
@@ -4,15 +4,9 @@ module PgRls
4
4
  # Tenant Controller
5
5
  module Tenant
6
6
  class << self
7
- attr_reader :tenant
8
-
9
7
  def switch(resource)
10
- tenant = switch_tenant!(resource)
11
-
12
- "RLS changed to '#{tenant.id}'"
13
- rescue StandardError => e
14
- Rails.logger.info('connection was not made')
15
- Rails.logger.info(e)
8
+ switch!(resource)
9
+ rescue PgRls::Errors::TenantNotFound
16
10
  nil
17
11
  end
18
12
 
@@ -22,15 +16,18 @@ module PgRls
22
16
  "RLS changed to '#{tenant.id}'"
23
17
  rescue StandardError => e
24
18
  Rails.logger.info('connection was not made')
25
- raise e
19
+ raise PgRls::Errors::TenantNotFound
26
20
  end
27
21
 
22
+
28
23
  def with_tenant!(resource)
29
- tenant = switch_tenant!(resource)
24
+ PgRls.main_model.connection_pool.with_connection do
25
+ tenant = switch_tenant!(resource)
30
26
 
31
- yield(tenant) if block_given?
32
- ensure
33
- reset_rls! unless PgRls.test_inline_tenant == true
27
+ yield(tenant).presence if block_given?
28
+ ensure
29
+ reset_rls! unless PgRls.test_inline_tenant == true
30
+ end
34
31
  end
35
32
 
36
33
  def fetch
@@ -39,19 +36,15 @@ module PgRls
39
36
  nil
40
37
  end
41
38
 
42
- def tenant!
43
- @tenant ||= PgRls.main_model.find_by!(
39
+ def fetch!
40
+ PgRls.main_model.find_by!(
44
41
  tenant_id: PgRls.connection_class.connection.execute(
45
42
  "SELECT current_setting('rls.tenant_id')"
46
43
  ).getvalue(0, 0)
47
44
  )
48
45
  end
49
- alias fetch! tenant!
50
46
 
51
47
  def reset_rls!
52
- return if @tenant.blank?
53
-
54
- @tenant = nil
55
48
  PgRls.execute_rls_in_shards do |connection_class|
56
49
  connection_class.transaction do
57
50
  connection_class.connection.execute('RESET rls.tenant_id')
@@ -68,7 +61,7 @@ module PgRls
68
61
  PgRls.main_model.ignored_columns = []
69
62
  # rubocop: enable Rails/IgnoredColumnsAssignment
70
63
 
71
- find_tenant(resource)
64
+ tenant = find_tenant(resource)
72
65
 
73
66
  PgRls.execute_rls_in_shards do |connection_class|
74
67
  connection_class.transaction do
@@ -78,6 +71,8 @@ module PgRls
78
71
  end
79
72
 
80
73
  tenant
74
+ rescue NoMethodError
75
+ raise PgRls::Errors::TenantNotFound
81
76
  end
82
77
 
83
78
  def find_tenant(resource)
@@ -85,14 +80,17 @@ module PgRls
85
80
 
86
81
  reset_rls!
87
82
 
83
+ tenant = nil
84
+
88
85
  PgRls.search_methods.each do |method|
89
- break if @tenant.present?
86
+ break if tenant.present?
90
87
 
91
- @method = method
92
- @tenant = find_tenant_by_method(resource, method)
88
+ tenant = find_tenant_by_method(resource, method)
93
89
  end
94
90
 
95
91
  raise PgRls::Errors::TenantNotFound if tenant.blank?
92
+
93
+ tenant
96
94
  end
97
95
 
98
96
  def find_tenant_by_method(resource, method)
@@ -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.3'
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,14 +1,14 @@
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-14 00:00:00.000000000 Z
11
+ date: 2023-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,7 +85,7 @@ licenses:
85
85
  - MIT
86
86
  metadata:
87
87
  rubygems_mfa_required: 'true'
88
- post_install_message:
88
+ post_install_message:
89
89
  rdoc_options: []
90
90
  require_paths:
91
91
  - lib
@@ -100,8 +100,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
102
  requirements: []
103
- rubygems_version: 3.4.19
104
- signing_key:
103
+ rubygems_version: 3.3.7
104
+ signing_key:
105
105
  specification_version: 4
106
106
  summary: Write a short summary, because RubyGems requires one.
107
107
  test_files: []