pg_rls 0.1.2 → 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: 5296cf19938df79d340f6da931acf0aee13e967720d964ec979773189634c155
4
- data.tar.gz: ebf0278848a707e692198346dc734e8da9024c3b5e1325da760c4d7f7cdc30f2
3
+ metadata.gz: 0e0133ebb0867d0bfeeea122f6ddef47f861c6b17e2b8b65a07acdca4768c66a
4
+ data.tar.gz: 5bd700b9dd72150b01e72086f07795772cbb3f3b512e7106dbf58b2850713414
5
5
  SHA512:
6
- metadata.gz: 97465ed5ea998a4c6e6ae39aae82d08ae0663733ef0f442d4716431adb607f13b96fb44139d5c5be83696cbb044cfa778a918a74aa842906ae798869f0fb3145
7
- data.tar.gz: c85948bfbcd16f610b2095f2900fee54d3abc3cd27b581eb282144e4b444341a0cfb50c2b1ef98e8cd9d395c22e6218d81fa19d2281df956bb3d8e5cf4e39204
6
+ metadata.gz: af0c6529cd940354cbaf1432c7086e1ae336f0d160df036108820873f31699c580833b7b153956e1b599b26946de15e8bfd5eda2dc94e10fa8b0b757fc94bcf1
7
+ data.tar.gz: 04feda60ca3f7fa0e98f096246db9ba2d4eecac158af08f719631e7f078b224e5dc33b660e92129a6eb70e217bb7e550fdf74843015b1df988272954d756192c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pg_rls (0.1.2)
4
+ pg_rls (0.1.1)
5
5
  bundler (~> 2.2)
6
6
 
7
7
  GEM
@@ -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.2'
4
+ VERSION = '0.1.3'
5
5
  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.2
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: []