ros-apartment 2.6.0 → 2.6.1

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: d0f6acd23ac1eab76581c3753f6833cb1a1b7b317e9652582721a2f6ad84500c
4
- data.tar.gz: 0fc60c72b7148707b3d7530b41418873487cbe9de825b4e0e9bc8b82bc66a60e
3
+ metadata.gz: e57016e050d57190e3baa0693ccd7e2e094db1cdb864f9c75e193ce06fcdddbb
4
+ data.tar.gz: 65b587e5c59e03e957e42c8ca0c0cf4d078859e078eebdcda4594e69284c3175
5
5
  SHA512:
6
- metadata.gz: b08ff81c0a66ba663774b2d96893fcbc1d5451b581b76957195bf7c09735351195368b45a7dda8f1e023f77fb0d5c8eb46c385ecae5e7d35840358cfe849a61d
7
- data.tar.gz: c41e3675d25662bd4806466f97eff40f5f9548676a35450548fcdf98c27beca0c27e19935fe4d101b703f944bcdee09b07183314a140405183c7a192c3b4c062
6
+ metadata.gz: d1ebfaf0a5e4da01bceed2f3cec330aefec6bbfdb5e98dd706ec7b0305cb8bc820e58106ebb60854fc7f5d8735c02d426c87a17a9f16c4b12f34104059644419
7
+ data.tar.gz: 1a72b322cf5913313aedfb1f84d3117c5cbbd5ba36a7b62ab1cf10d2e3987d8eca8b465cfc38538b86482a08fd8412e43e1d39708d3a952e4fad8c946b0b0203
@@ -46,7 +46,6 @@ Gem::Specification.new do |s|
46
46
  s.add_development_dependency 'activerecord-jdbcpostgresql-adapter'
47
47
  s.add_development_dependency 'jdbc-mysql'
48
48
  s.add_development_dependency 'jdbc-postgres'
49
- s.add_development_dependency 'jruby-openssl'
50
49
  else
51
50
  s.add_development_dependency 'mysql2', '~> 0.5'
52
51
  s.add_development_dependency 'pg', '~> 1.2'
@@ -35,6 +35,12 @@ module Apartment
35
35
  end
36
36
  end
37
37
 
38
+ # Initialize Apartment config options such as excluded_models
39
+ #
40
+ def init
41
+ process_excluded_models
42
+ end
43
+
38
44
  # Note alias_method here doesn't work with inheritence apparently ??
39
45
  #
40
46
  def current
@@ -37,6 +37,12 @@ module Apartment
37
37
  def reset
38
38
  @current = default_tenant
39
39
  Apartment.connection.schema_search_path = full_search_path
40
+ reset_sequence_names
41
+ end
42
+
43
+ def init
44
+ super
45
+ Apartment.connection.schema_search_path = full_search_path
40
46
  end
41
47
 
42
48
  def current
@@ -73,6 +79,7 @@ module Apartment
73
79
  # there is a issue for prepared statement with changing search_path.
74
80
  # https://www.postgresql.org/docs/9.3/static/sql-prepare.html
75
81
  Apartment.connection.clear_cache! if postgresql_version < 90_300
82
+ reset_sequence_names
76
83
  rescue *rescuable_exceptions
77
84
  raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}"
78
85
  end
@@ -104,6 +111,19 @@ module Apartment
104
111
  # public from Rails 5.0.
105
112
  Apartment.connection.send(:postgresql_version)
106
113
  end
114
+
115
+ def reset_sequence_names
116
+ # sequence_name contains the schema, so it must be reset after switch
117
+ # There is `reset_sequence_name`, but that method actually goes to the database
118
+ # to find out the new name. Therefore, we do this hack to only unset the name,
119
+ # and it will be dynamically found the next time it is needed
120
+ ActiveRecord::Base.descendants
121
+ .select { |c| c.instance_variable_defined?(:@sequence_name) }
122
+ .reject { |c| c.instance_variable_defined?(:@explicit_sequence_name) && c.instance_variable_get(:@explicit_sequence_name) }
123
+ .each do |c|
124
+ c.remove_instance_variable :@sequence_name
125
+ end
126
+ end
107
127
  end
108
128
 
109
129
  # Another Adapter for Postgresql when using schemas and SQL
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'console'
4
+
3
5
  module Apartment
4
6
  module CustomConsole
5
7
  begin
@@ -24,26 +24,28 @@ module Apartment
24
24
  ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
25
25
  end
26
26
 
27
- # Hook into ActionDispatch::Reloader to ensure Apartment is properly initialized
28
- # Note that this doens't entirely work as expected in Development, because this is called before classes are reloaded
29
- # See the middleware/console declarations below to help with this. Hope to fix that soon.
30
- #
27
+ # Make sure Apartment is reconfigured when the code is reloaded
31
28
  config.to_prepare do
32
- next if ARGV.any? { |arg| arg =~ /\Aassets:(?:precompile|clean)\z/ }
33
- next if ARGV.any? { |arg| arg == 'webpacker:compile' }
29
+ Apartment::Tenant.reinitialize
30
+ end
34
31
 
35
- begin
36
- Apartment.connection_class.connection_pool.with_connection do
37
- Apartment::Tenant.init
38
- end
39
- rescue ::ActiveRecord::NoDatabaseError, PG::ConnectionBad
40
- # Since `db:create` and other tasks invoke this block from Rails 5.2.0,
41
- # we need to swallow the error to execute `db:create` properly.
42
- Rails.logger.warn do
43
- 'Failed to initialize Apartment because a database connection could not be established.'
32
+ #
33
+ # Ensure that Apartment::Tenant.init is called when
34
+ # a new connection is requested.
35
+ #
36
+ module ApartmentInitializer
37
+ def connection
38
+ super.tap do
39
+ Apartment::Tenant.init_once
44
40
  end
45
41
  end
42
+
43
+ def arel_table
44
+ Apartment::Tenant.init_once
45
+ super
46
+ end
46
47
  end
48
+ ActiveRecord::Base.singleton_class.prepend ApartmentInitializer
47
49
 
48
50
  #
49
51
  # Ensure rake tasks are loaded
@@ -7,7 +7,7 @@ module Apartment
7
7
  class RakeTaskEnhancer
8
8
  module TASKS
9
9
  ENHANCE_BEFORE = %w[db:drop].freeze
10
- ENHANCE_AFTER = %w[db:migrate db:rollback db:migrate:up db:migrate:down db:migrate:redo db:seed].freeze
10
+ ENHANCE_AFTER = %w[db:create db:migrate db:rollback db:migrate:up db:migrate:down db:migrate:redo db:seed].freeze
11
11
  freeze
12
12
  end
13
13
 
@@ -10,15 +10,23 @@ module Apartment
10
10
  extend Forwardable
11
11
 
12
12
  def_delegators :adapter, :create, :drop, :switch, :switch!, :current, :each,
13
- :reset, :set_callback, :seed, :current_tenant,
13
+ :reset, :init, :set_callback, :seed, :current_tenant,
14
14
  :default_tenant, :environmentify
15
15
 
16
16
  attr_writer :config
17
17
 
18
- # Initialize Apartment config options such as excluded_models
19
- #
20
- def init
21
- adapter.process_excluded_models
18
+ def init_once
19
+ return if @already_initialized
20
+
21
+ # To avoid infinite loops in work init is doing,
22
+ # we need to set @already_initialized to true
23
+ # before init is called
24
+ @already_initialized = true
25
+ init
26
+ end
27
+
28
+ def reinitialize
29
+ @already_initialized = false
22
30
  end
23
31
 
24
32
  # Fetch the proper multi-tenant adapter based on Rails config
@@ -53,6 +61,7 @@ module Apartment
53
61
  #
54
62
  def reload!(config = nil)
55
63
  Thread.current[:apartment_adapter] = nil
64
+ reinitialize
56
65
  @config = config
57
66
  end
58
67
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Apartment
4
- VERSION = '2.6.0'
4
+ VERSION = '2.6.1'
5
5
  end
@@ -121,11 +121,15 @@ apartment_namespace = namespace :apartment do
121
121
  end
122
122
 
123
123
  def each_tenant(&block)
124
- Parallel.each(tenants, in_threads: Apartment.parallel_migration_threads) do |tenant|
124
+ Parallel.each(tenants_without_default, in_threads: Apartment.parallel_migration_threads) do |tenant|
125
125
  block.call(tenant)
126
126
  end
127
127
  end
128
128
 
129
+ def tenants_without_default
130
+ tenants - [Apartment.default_schema]
131
+ end
132
+
129
133
  def tenants
130
134
  ENV['DB'] ? ENV['DB'].split(',').map(&:strip) : Apartment.tenant_names || []
131
135
  end
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.6.0
4
+ version: 2.6.1
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-14 00:00:00.000000000 Z
13
+ date: 2020-06-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord