ros-apartment 2.5.0 → 2.6.0

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: 4f9084d6e2d03975a90c0b57dbb180872bdf027b1fbbea6ca7c50aec81ee97b5
4
- data.tar.gz: 10ae96d58901ce650fc458ca7451aadc0e726ea2d22fedbc674edbba24ce271c
3
+ metadata.gz: d0f6acd23ac1eab76581c3753f6833cb1a1b7b317e9652582721a2f6ad84500c
4
+ data.tar.gz: 0fc60c72b7148707b3d7530b41418873487cbe9de825b4e0e9bc8b82bc66a60e
5
5
  SHA512:
6
- metadata.gz: 35135a59820ac13e528da16dca7abc6376eb4e2e868a2d00a57169effec73a026ae11726202fc9381160fad7d3cabba764253d592db803d0638a79c2fb8360c0
7
- data.tar.gz: b320c009ee8017fcd33faa1ec4ec26a6c2a74bbd19434c0d73f52e4c8def1ba37b03af97f8477d6d5c174636bcf3fd11883be1fbb46119ee8f1be31c44384254
6
+ metadata.gz: b08ff81c0a66ba663774b2d96893fcbc1d5451b581b76957195bf7c09735351195368b45a7dda8f1e023f77fb0d5c8eb46c385ecae5e7d35840358cfe849a61d
7
+ data.tar.gz: c41e3675d25662bd4806466f97eff40f5f9548676a35450548fcdf98c27beca0c27e19935fe4d101b703f944bcdee09b07183314a140405183c7a192c3b4c062
@@ -1,6 +1,6 @@
1
1
  name: Rubocop Lint
2
2
 
3
- on: [push]
3
+ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  build:
data/.rubocop_todo.yml CHANGED
@@ -21,7 +21,7 @@ Metrics/BlockLength:
21
21
  # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
22
22
  # URISchemes: http, https
23
23
  Metrics/LineLength:
24
- Max: 237
24
+ Max: 200
25
25
 
26
26
  # Offense count: 4
27
27
  # Configuration parameters: CountComments, ExcludedMethods.
data/README.md CHANGED
@@ -324,6 +324,20 @@ Apartment.configure do |config|
324
324
  end
325
325
  ```
326
326
 
327
+ ### Skip tenant schema check
328
+
329
+ This is configurable by setting: `tenant_presence_check`. It defaults to true
330
+ in order to maintain the original gem behavior. This is only checked when using one of the PostgreSQL adapters.
331
+ The original gem behavior, when running `switch` would look for the existence of the schema before switching. This adds an extra query on every context switch. While in the default simple scenarios this is a valid check, in high volume platforms this adds some unnecessary overhead which can be detected in some other ways on the application level.
332
+
333
+ Setting this configuration value to `false` will disable the schema presence check before trying to switch the context.
334
+
335
+ ```ruby
336
+ Apartment.configure do |config|
337
+ tenant_presence_check = false
338
+ end
339
+ ```
340
+
327
341
  ### Excluding models
328
342
 
329
343
  If you have some models that should always access the 'public' tenant, you can specify this by configuring Apartment using `Apartment.configure`. This will yield a config object for you. You can set excluded models like so:
@@ -68,8 +68,6 @@ module Apartment
68
68
  #
69
69
  def switch!(tenant = nil)
70
70
  run_callbacks :switch do
71
- return reset if tenant.nil?
72
-
73
71
  connect_to_new(tenant).tap do
74
72
  Apartment.connection.clear_query_cache
75
73
  end
@@ -130,14 +128,12 @@ module Apartment
130
128
  # @return {String} tenant name with Rails environment *optionally* prepended
131
129
  #
132
130
  def environmentify(tenant)
133
- if !tenant.include?(Rails.env)
134
- if Apartment.prepend_environment
135
- "#{Rails.env}_#{tenant}"
136
- elsif Apartment.append_environment
137
- "#{tenant}_#{Rails.env}"
138
- else
139
- tenant
140
- end
131
+ return tenant if tenant.nil? || tenant.include?(Rails.env)
132
+
133
+ if Apartment.prepend_environment
134
+ "#{Rails.env}_#{tenant}"
135
+ elsif Apartment.append_environment
136
+ "#{tenant}_#{Rails.env}"
141
137
  else
142
138
  tenant
143
139
  end
@@ -175,6 +171,8 @@ module Apartment
175
171
  # @param {String} tenant Database name
176
172
  #
177
173
  def connect_to_new(tenant)
174
+ return reset if tenant.nil?
175
+
178
176
  query_cache_enabled = ActiveRecord::Base.connection.query_cache_enabled
179
177
 
180
178
  Apartment.establish_connection multi_tenantify(tenant)
@@ -37,12 +37,11 @@ module Apartment
37
37
  #
38
38
  def connect_to_new(tenant = nil)
39
39
  return reset if tenant.nil?
40
- # rubocop:disable Style/RaiseArgs
41
- raise ActiveRecord::StatementInvalid.new("Could not find schema #{tenant}") unless Apartment.connection.all_schemas.include? tenant.to_s
42
40
 
43
- # rubocop:enable Style/RaiseArgs
41
+ tenant = tenant.to_s
42
+ raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)
44
43
 
45
- @current = tenant.to_s
44
+ @current = tenant
46
45
  Apartment.connection.schema_search_path = full_search_path
47
46
  rescue ActiveRecord::StatementInvalid, ActiveRecord::JDBCError
48
47
  raise TenantNotFound, "One of the following schema(s) is invalid: #{full_search_path}"
@@ -50,6 +49,12 @@ module Apartment
50
49
 
51
50
  private
52
51
 
52
+ def tenant_exists?(tenant)
53
+ return true unless Apartment.tenant_presence_check
54
+
55
+ Apartment.connection.all_schemas.include? tenant
56
+ end
57
+
53
58
  def rescue_from
54
59
  ActiveRecord::JDBCError
55
60
  end
@@ -62,12 +62,11 @@ module Apartment
62
62
  #
63
63
  def connect_to_new(tenant = nil)
64
64
  return reset if tenant.nil?
65
- # rubocop:disable Style/RaiseArgs
66
- raise ActiveRecord::StatementInvalid.new("Could not find schema #{tenant}") unless Apartment.connection.schema_exists?(tenant.to_s)
67
65
 
68
- # rubocop:enable Style/RaiseArgs
66
+ tenant = tenant.to_s
67
+ raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)
69
68
 
70
- @current = tenant.to_s
69
+ @current = tenant
71
70
  Apartment.connection.schema_search_path = full_search_path
72
71
 
73
72
  # When the PostgreSQL version is < 9.3,
@@ -80,6 +79,12 @@ module Apartment
80
79
 
81
80
  private
82
81
 
82
+ def tenant_exists?(tenant)
83
+ return true unless Apartment.tenant_presence_check
84
+
85
+ Apartment.connection.schema_exists?(tenant)
86
+ end
87
+
83
88
  def create_tenant_command(conn, tenant)
84
89
  conn.execute(%(CREATE SCHEMA "#{tenant}"))
85
90
  end
@@ -33,6 +33,8 @@ module Apartment
33
33
  protected
34
34
 
35
35
  def connect_to_new(tenant)
36
+ return reset if tenant.nil?
37
+
36
38
  unless File.exist?(database_file(tenant))
37
39
  raise TenantNotFound,
38
40
  "The tenant #{environmentify(tenant)} cannot be found."
@@ -35,3 +35,10 @@ def tenant_list
35
35
  tenant_list += Apartment.tenant_names
36
36
  tenant_list.uniq
37
37
  end
38
+
39
+ def tenant_info_msg
40
+ # rubocop:disable Rails/Output
41
+ puts "Available Tenants: #{tenant_list}\n"
42
+ puts "Use `st 'tenant'` to switch tenants & `tenant_list` to see list\n"
43
+ # rubocop:enable Rails/Output
44
+ end
@@ -10,17 +10,31 @@ module Apartment
10
10
  # rubocop:enable Rails/Output
11
11
  end
12
12
 
13
- if Pry::Prompt.respond_to?(:add)
14
- desc = "Includes the current Rails environment and project folder name.\n" \
15
- '[1] [project_name][Rails.env][Apartment::Tenant.current] pry(main)>'
13
+ desc = "Includes the current Rails environment and project folder name.\n" \
14
+ '[1] [project_name][Rails.env][Apartment::Tenant.current] pry(main)>'
16
15
 
16
+ prompt_procs = [
17
+ proc { |target_self, nest_level, pry| prompt_contents(pry, target_self, nest_level, '>') },
18
+ proc { |target_self, nest_level, pry| prompt_contents(pry, target_self, nest_level, '*') }
19
+ ]
20
+
21
+ if Gem::Version.new(Pry::VERSION) >= Gem::Version.new('0.13')
22
+ Pry.config.prompt = Pry::Prompt.new 'ros', desc, prompt_procs
23
+ else
17
24
  Pry::Prompt.add 'ros', desc, %w[> *] do |target_self, nest_level, pry, sep|
18
- "[#{pry.input_ring.size}] [#{PryRails::Prompt.formatted_env}][#{Apartment::Tenant.current}] " \
19
- "#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})" \
20
- "#{":#{nest_level}" unless nest_level.zero?}#{sep} "
25
+ prompt_contents(pry, target_self, nest_level, sep)
21
26
  end
22
-
23
27
  Pry.config.prompt = Pry::Prompt[:ros][:value]
24
28
  end
29
+
30
+ Pry.config.hooks.add_hook(:when_started, 'startup message') do
31
+ tenant_info_msg
32
+ end
33
+
34
+ def self.prompt_contents(pry, target_self, nest_level, sep)
35
+ "[#{pry.input_ring.size}] [#{PryRails::Prompt.formatted_env}][#{Apartment::Tenant.current}] " \
36
+ "#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})" \
37
+ "#{":#{nest_level}" unless nest_level.zero?}#{sep} "
38
+ end
25
39
  end
26
40
  end
@@ -18,6 +18,7 @@ module Apartment
18
18
  config.seed_after_create = false
19
19
  config.prepend_environment = false
20
20
  config.append_environment = false
21
+ config.tenant_presence_check = true
21
22
  end
22
23
 
23
24
  ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Apartment
4
- VERSION = '2.5.0'
4
+ VERSION = '2.6.0'
5
5
  end
data/lib/apartment.rb CHANGED
@@ -20,7 +20,7 @@ module Apartment
20
20
  extend Forwardable
21
21
 
22
22
  ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment
23
- append_environment with_multi_server_setup].freeze
23
+ append_environment with_multi_server_setup tenant_presence_check].freeze
24
24
 
25
25
  WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
26
26
  default_schema persistent_schemas connection_class
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ros-apartment
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Brunner
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-05-05 00:00:00.000000000 Z
13
+ date: 2020-05-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord