pg_rls 0.0.2 → 0.0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ee45b6831bde44dff2a29fa6dd8e832e1e55970833bbbd52a9a30982f6b987a
4
- data.tar.gz: 83013597d4b42fb6b808c8c1b7e90c58eeb3e8dc231923523a01e6454b52507d
3
+ metadata.gz: 96f6c928e78088e4781845c1243c1af8ceee143f99863af133f0aae2d40b1df8
4
+ data.tar.gz: 4bb596321621511c913d8ee98a71918df75b84e3c4738ae44e9f1ee0974c74af
5
5
  SHA512:
6
- metadata.gz: fae8b1317628ddc17c19753eab39937bfc237cda1c0df9b60a9c91bbe58e5203316e305fbe3175d803531e09fc9e43b0e63941cd2153aa9a363a4d4f35e0d308
7
- data.tar.gz: a42e6f8ebc4a93739ab3475a9d436748f1c63fc2e4878011a37d9bb46db17fff79bf66bfb4f374f5cabf8496d93bcac024a285687f19bca5f19f3717fd65f05e
6
+ metadata.gz: 6ab1085addd4d796fb221e2850459b26c0e520192916a7285fe5c7f0ddd6298cb5576f826d133f06bd9be5c2d89d0895c9446ebcc7b6c5413e0d86b9972b2721
7
+ data.tar.gz: f0b744bc68654c77afeabadf2af756fe5787602b2f26262172acd6ddca51928a405b75f46f0c7824dfdfbe5c3fde0d4b03bec45c803680b37ce4a903947f06c9
data/lib/pg_rls/tenant.rb CHANGED
@@ -9,19 +9,26 @@ module PgRls
9
9
  def switch(resource)
10
10
  switch_tenant!(resource)
11
11
  rescue StandardError => e
12
- puts 'connection was not made'
13
- puts e
12
+ Rails.logger.info('connection was not made')
13
+ Rails.logger.info(e)
14
14
  end
15
15
 
16
16
  def switch!(resource)
17
17
  switch_tenant!(resource)
18
18
  rescue StandardError => e
19
- puts 'connection was not made'
19
+ Rails.logger.info('connection was not made')
20
20
  raise e
21
21
  end
22
22
 
23
+ def with_tenant(resource)
24
+ switch_tenant!(resource)
25
+ yield
26
+ ensure
27
+ reset_rls!
28
+ end
29
+
23
30
  def fetch
24
- @fetch ||= PgRls.main_model.find_by_tenant_id(
31
+ @fetch ||= PgRls.main_model.find_by_tenant_id!(
25
32
  PgRls.connection_class.connection.execute(
26
33
  "SELECT current_setting('rls.tenant_id')"
27
34
  ).getvalue(0, 0)
@@ -30,10 +37,15 @@ module PgRls
30
37
  'no tenant is selected'
31
38
  end
32
39
 
40
+ def reset_rls!
41
+ @fetch = nil
42
+ @tenant = nil
43
+ PgRls.connection_class.connection.execute('RESET rls.tenant_id')
44
+ end
45
+
33
46
  private
34
47
 
35
48
  def switch_tenant!(resource)
36
- @fetch = nil
37
49
  connection_adapter = PgRls.connection_class
38
50
  find_tenant(resource)
39
51
 
@@ -45,14 +57,21 @@ module PgRls
45
57
  end
46
58
 
47
59
  def find_tenant(resource)
48
- @tenant = nil
60
+ reset_rls!
49
61
 
50
62
  PgRls.search_methods.each do |method|
63
+ return if @tenant.present?
64
+
51
65
  @method = method
52
- @tenant ||= PgRls.main_model.send("find_by_#{method}!", resource)
53
- rescue NoMethodError, ActiveRecord::RecordNotFound => e
54
- @error = e
66
+ @tenant = find_tenant_by_method(resource, method)
55
67
  end
68
+
69
+ raise PgRls::Errors::TenantNotFound if tenant.nil?
70
+ end
71
+
72
+ def find_tenant_by_method(resource, method)
73
+ PgRls.main_model.send("find_by_#{method}!", resource)
74
+ rescue ActiveRecord::RecordNotFound => e
56
75
  end
57
76
  end
58
77
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRls
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.2.1'
5
5
  end
data/lib/pg_rls.rb CHANGED
@@ -48,9 +48,9 @@ module PgRls
48
48
  end
49
49
 
50
50
  def establish_new_connection
51
- connection_class.establish_connection(
52
- **database_configuration
53
- )
51
+ ActiveRecord::Base.connection.disconnect!
52
+
53
+ connection_class.establish_connection(**database_configuration)
54
54
  end
55
55
 
56
56
  def admin_execute(query = nil)
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.0.2
4
+ version: 0.0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-02 00:00:00.000000000 Z
11
+ date: 2022-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler