ros-apartment 2.4.0 → 2.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/.rubocop_todo.yml +1 -1
- data/.story_branch.yml +1 -0
- data/.travis.yml +13 -30
- data/Appraisals +4 -17
- data/Gemfile +2 -2
- data/Guardfile +3 -1
- data/HISTORY.md +90 -0
- data/README.md +31 -16
- data/Rakefile +34 -22
- data/TODO.md +0 -1
- data/gemfiles/rails_5_0.gemfile +10 -11
- data/gemfiles/rails_5_1.gemfile +10 -11
- data/gemfiles/rails_5_2.gemfile +9 -10
- data/gemfiles/rails_6_0.gemfile +10 -11
- data/gemfiles/rails_master.gemfile +10 -11
- data/lib/apartment.rb +31 -13
- data/lib/apartment/active_record/connection_handling.rb +17 -0
- data/lib/apartment/active_record/internal_metadata.rb +11 -0
- data/lib/apartment/active_record/log_subscriber.rb +41 -0
- data/lib/apartment/active_record/schema_migration.rb +13 -0
- data/lib/apartment/adapters/abstract_adapter.rb +17 -13
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +9 -4
- data/lib/apartment/adapters/mysql2_adapter.rb +2 -0
- data/lib/apartment/adapters/postgresql_adapter.rb +33 -4
- data/lib/apartment/adapters/sqlite3_adapter.rb +2 -0
- data/lib/apartment/console.rb +7 -0
- data/lib/apartment/custom_console.rb +23 -7
- data/lib/apartment/model.rb +27 -0
- data/lib/apartment/railtie.rb +19 -15
- data/lib/apartment/tasks/task_helper.rb +35 -0
- data/lib/apartment/tenant.rb +16 -5
- data/lib/apartment/version.rb +1 -1
- data/lib/generators/apartment/install/templates/apartment.rb +5 -0
- data/lib/tasks/apartment.rake +16 -37
- data/{apartment.gemspec → ros-apartment.gemspec} +12 -6
- metadata +16 -213
- data/.github/workflows/.rubocop-linter.yml +0 -22
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -20
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -39
- data/spec/adapters/mysql2_adapter_spec.rb +0 -61
- data/spec/adapters/postgresql_adapter_spec.rb +0 -63
- data/spec/adapters/sqlite3_adapter_spec.rb +0 -101
- data/spec/apartment_spec.rb +0 -13
- data/spec/config/database.yml.sample +0 -49
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/controllers/application_controller.rb +0 -7
- data/spec/dummy/app/helpers/application_helper.rb +0 -4
- data/spec/dummy/app/models/application_record.rb +0 -6
- data/spec/dummy/app/models/company.rb +0 -5
- data/spec/dummy/app/models/user.rb +0 -5
- data/spec/dummy/app/views/application/index.html.erb +0 -1
- data/spec/dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/dummy/config.ru +0 -6
- data/spec/dummy/config/application.rb +0 -51
- data/spec/dummy/config/boot.rb +0 -13
- data/spec/dummy/config/database.yml.sample +0 -44
- data/spec/dummy/config/environment.rb +0 -7
- data/spec/dummy/config/environments/development.rb +0 -29
- data/spec/dummy/config/environments/production.rb +0 -53
- data/spec/dummy/config/environments/test.rb +0 -36
- data/spec/dummy/config/initializers/apartment.rb +0 -6
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -8
- data/spec/dummy/config/initializers/inflections.rb +0 -11
- data/spec/dummy/config/initializers/mime_types.rb +0 -6
- data/spec/dummy/config/initializers/secret_token.rb +0 -9
- data/spec/dummy/config/initializers/session_store.rb +0 -10
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -5
- data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
- data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
- data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
- data/spec/dummy/db/schema.rb +0 -55
- data/spec/dummy/db/seeds.rb +0 -5
- data/spec/dummy/db/seeds/import.rb +0 -7
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -26
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +0 -8
- data/spec/dummy_engine/.gitignore +0 -8
- data/spec/dummy_engine/Gemfile +0 -15
- data/spec/dummy_engine/Rakefile +0 -34
- data/spec/dummy_engine/bin/rails +0 -12
- data/spec/dummy_engine/config/initializers/apartment.rb +0 -52
- data/spec/dummy_engine/dummy_engine.gemspec +0 -24
- data/spec/dummy_engine/lib/dummy_engine.rb +0 -6
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -6
- data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -5
- data/spec/dummy_engine/test/dummy/Rakefile +0 -6
- data/spec/dummy_engine/test/dummy/config.ru +0 -6
- data/spec/dummy_engine/test/dummy/config/application.rb +0 -24
- data/spec/dummy_engine/test/dummy/config/boot.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
- data/spec/dummy_engine/test/dummy/config/environment.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -39
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -80
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -41
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -10
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -8
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -6
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -17
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -16
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
- data/spec/dummy_engine/test/dummy/config/routes.rb +0 -58
- data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
- data/spec/examples/connection_adapter_examples.rb +0 -44
- data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -93
- data/spec/examples/generic_adapter_examples.rb +0 -164
- data/spec/examples/schema_adapter_examples.rb +0 -239
- data/spec/integration/apartment_rake_integration_spec.rb +0 -107
- data/spec/integration/query_caching_spec.rb +0 -83
- data/spec/integration/use_within_an_engine_spec.rb +0 -28
- data/spec/schemas/v1.rb +0 -15
- data/spec/schemas/v2.rb +0 -41
- data/spec/schemas/v3.rb +0 -47
- data/spec/spec_helper.rb +0 -63
- data/spec/support/apartment_helpers.rb +0 -47
- data/spec/support/capybara_sessions.rb +0 -15
- data/spec/support/config.rb +0 -13
- data/spec/support/contexts.rb +0 -54
- data/spec/support/requirements.rb +0 -48
- data/spec/support/setup.rb +0 -46
- data/spec/tasks/apartment_rake_spec.rb +0 -124
- data/spec/tenant_spec.rb +0 -194
- data/spec/unit/config_spec.rb +0 -111
- data/spec/unit/elevators/domain_spec.rb +0 -33
- data/spec/unit/elevators/first_subdomain_spec.rb +0 -26
- data/spec/unit/elevators/generic_spec.rb +0 -55
- data/spec/unit/elevators/host_hash_spec.rb +0 -33
- data/spec/unit/elevators/host_spec.rb +0 -89
- data/spec/unit/elevators/subdomain_spec.rb +0 -77
- data/spec/unit/migrator_spec.rb +0 -78
- data/spec/unit/reloader_spec.rb +0 -24
data/TODO.md
CHANGED
data/gemfiles/rails_5_0.gemfile
CHANGED
@@ -1,24 +1,23 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
# This file was generated by Appraisal
|
4
2
|
|
5
|
-
source
|
3
|
+
source "http://rubygems.org"
|
6
4
|
|
7
|
-
gem
|
5
|
+
gem "perx-rubocop", "~> 0.0.3"
|
6
|
+
gem "rails", "~> 5.0.0"
|
8
7
|
|
9
8
|
group :local do
|
10
|
-
gem
|
11
|
-
gem
|
9
|
+
gem "guard-rspec", "~> 4.2"
|
10
|
+
gem "pry"
|
12
11
|
end
|
13
12
|
|
14
13
|
platforms :ruby do
|
15
|
-
gem
|
14
|
+
gem "pg", "< 1.0.0"
|
16
15
|
end
|
17
16
|
|
18
17
|
platforms :jruby do
|
19
|
-
gem
|
20
|
-
gem
|
21
|
-
gem
|
18
|
+
gem "activerecord-jdbc-adapter", "~> 50.0"
|
19
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 50.0"
|
20
|
+
gem "activerecord-jdbcmysql-adapter", "~> 50.0"
|
22
21
|
end
|
23
22
|
|
24
|
-
gemspec path:
|
23
|
+
gemspec path: "../"
|
data/gemfiles/rails_5_1.gemfile
CHANGED
@@ -1,24 +1,23 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
# This file was generated by Appraisal
|
4
2
|
|
5
|
-
source
|
3
|
+
source "http://rubygems.org"
|
6
4
|
|
7
|
-
gem
|
5
|
+
gem "perx-rubocop", "~> 0.0.3"
|
6
|
+
gem "rails", "~> 5.1.0"
|
8
7
|
|
9
8
|
group :local do
|
10
|
-
gem
|
11
|
-
gem
|
9
|
+
gem "guard-rspec", "~> 4.2"
|
10
|
+
gem "pry"
|
12
11
|
end
|
13
12
|
|
14
13
|
platforms :ruby do
|
15
|
-
gem
|
14
|
+
gem "pg", "< 1.0.0"
|
16
15
|
end
|
17
16
|
|
18
17
|
platforms :jruby do
|
19
|
-
gem
|
20
|
-
gem
|
21
|
-
gem
|
18
|
+
gem "activerecord-jdbc-adapter", "~> 51.0"
|
19
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 51.0"
|
20
|
+
gem "activerecord-jdbcmysql-adapter", "~> 51.0"
|
22
21
|
end
|
23
22
|
|
24
|
-
gemspec path:
|
23
|
+
gemspec path: "../"
|
data/gemfiles/rails_5_2.gemfile
CHANGED
@@ -1,20 +1,19 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
# This file was generated by Appraisal
|
4
2
|
|
5
|
-
source
|
3
|
+
source "http://rubygems.org"
|
6
4
|
|
7
|
-
gem
|
5
|
+
gem "perx-rubocop", "~> 0.0.3"
|
6
|
+
gem "rails", "~> 5.2.0"
|
8
7
|
|
9
8
|
group :local do
|
10
|
-
gem
|
11
|
-
gem
|
9
|
+
gem "guard-rspec", "~> 4.2"
|
10
|
+
gem "pry"
|
12
11
|
end
|
13
12
|
|
14
13
|
platforms :jruby do
|
15
|
-
gem
|
16
|
-
gem
|
17
|
-
gem
|
14
|
+
gem "activerecord-jdbc-adapter", "~> 52.0"
|
15
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 52.0"
|
16
|
+
gem "activerecord-jdbcmysql-adapter", "~> 52.0"
|
18
17
|
end
|
19
18
|
|
20
|
-
gemspec path:
|
19
|
+
gemspec path: "../"
|
data/gemfiles/rails_6_0.gemfile
CHANGED
@@ -1,24 +1,23 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
# This file was generated by Appraisal
|
4
2
|
|
5
|
-
source
|
3
|
+
source "http://rubygems.org"
|
6
4
|
|
7
|
-
gem
|
5
|
+
gem "perx-rubocop", "~> 0.0.3"
|
6
|
+
gem "rails", "~> 6.0.0"
|
8
7
|
|
9
8
|
group :local do
|
10
|
-
gem
|
11
|
-
gem
|
9
|
+
gem "guard-rspec", "~> 4.2"
|
10
|
+
gem "pry"
|
12
11
|
end
|
13
12
|
|
14
13
|
platforms :ruby do
|
15
|
-
gem
|
14
|
+
gem "sqlite3", "~> 1.4"
|
16
15
|
end
|
17
16
|
|
18
17
|
platforms :jruby do
|
19
|
-
gem
|
20
|
-
gem
|
21
|
-
gem
|
18
|
+
gem "activerecord-jdbc-adapter", "~> 60.0"
|
19
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 60.0"
|
20
|
+
gem "activerecord-jdbcmysql-adapter", "~> 60.0"
|
22
21
|
end
|
23
22
|
|
24
|
-
gemspec path:
|
23
|
+
gemspec path: "../"
|
@@ -1,24 +1,23 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
# This file was generated by Appraisal
|
4
2
|
|
5
|
-
source
|
3
|
+
source "http://rubygems.org"
|
6
4
|
|
7
|
-
gem
|
5
|
+
gem "perx-rubocop", "~> 0.0.3"
|
6
|
+
gem "rails", git: "https://github.com/rails/rails.git"
|
8
7
|
|
9
8
|
group :local do
|
10
|
-
gem
|
11
|
-
gem
|
9
|
+
gem "guard-rspec", "~> 4.2"
|
10
|
+
gem "pry"
|
12
11
|
end
|
13
12
|
|
14
13
|
platforms :ruby do
|
15
|
-
gem
|
14
|
+
gem "sqlite3", "~> 1.4"
|
16
15
|
end
|
17
16
|
|
18
17
|
platforms :jruby do
|
19
|
-
gem
|
20
|
-
gem
|
21
|
-
gem
|
18
|
+
gem "activerecord-jdbc-adapter", "~> 52.0"
|
19
|
+
gem "activerecord-jdbcpostgresql-adapter", "~> 52.0"
|
20
|
+
gem "activerecord-jdbcmysql-adapter", "~> 52.0"
|
22
21
|
end
|
23
22
|
|
24
|
-
gemspec path:
|
23
|
+
gemspec path: "../"
|
data/lib/apartment.rb
CHANGED
@@ -6,22 +6,40 @@ require 'forwardable'
|
|
6
6
|
require 'active_record'
|
7
7
|
require 'apartment/tenant'
|
8
8
|
|
9
|
+
# require_relative 'apartment/arel/visitors/postgresql'
|
10
|
+
|
11
|
+
require_relative 'apartment/active_record/log_subscriber'
|
12
|
+
require_relative 'apartment/active_record/connection_handling' if ActiveRecord.version.release >= Gem::Version.new('6.0')
|
13
|
+
|
14
|
+
if ActiveRecord.version.release >= Gem::Version.new('6.1')
|
15
|
+
require_relative 'apartment/active_record/schema_migration'
|
16
|
+
require_relative 'apartment/active_record/internal_metadata'
|
17
|
+
end
|
18
|
+
|
9
19
|
module Apartment
|
10
20
|
class << self
|
11
21
|
extend Forwardable
|
12
22
|
|
13
|
-
ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment
|
14
|
-
append_environment with_multi_server_setup].freeze
|
23
|
+
ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment default_tenant
|
24
|
+
append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze
|
15
25
|
|
16
26
|
WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
|
17
|
-
|
18
|
-
|
27
|
+
persistent_schemas connection_class
|
28
|
+
db_migrate_tenants seed_data_file
|
19
29
|
parallel_migration_threads pg_excluded_names].freeze
|
20
30
|
|
21
31
|
attr_accessor(*ACCESSOR_METHODS)
|
22
32
|
attr_writer(*WRITER_METHODS)
|
23
33
|
|
24
|
-
|
34
|
+
if ActiveRecord.version.release >= Gem::Version.new('6.1')
|
35
|
+
def_delegators :connection_class, :connection, :connection_db_config, :establish_connection
|
36
|
+
|
37
|
+
def connection_config
|
38
|
+
connection_db_config.configuration_hash
|
39
|
+
end
|
40
|
+
else
|
41
|
+
def_delegators :connection_class, :connection, :connection_config, :establish_connection
|
42
|
+
end
|
25
43
|
|
26
44
|
# configure apartment with available options
|
27
45
|
def configure
|
@@ -36,8 +54,12 @@ module Apartment
|
|
36
54
|
extract_tenant_config
|
37
55
|
end
|
38
56
|
|
57
|
+
def tld_length=(_)
|
58
|
+
Apartment::Deprecation.warn('`config.tld_length` have no effect because it was removed in https://github.com/influitive/apartment/pull/309')
|
59
|
+
end
|
60
|
+
|
39
61
|
def db_config_for(tenant)
|
40
|
-
(tenants_with_config[tenant] || connection_config)
|
62
|
+
(tenants_with_config[tenant] || connection_config)
|
41
63
|
end
|
42
64
|
|
43
65
|
# Whether or not db:migrate should also migrate tenants
|
@@ -53,15 +75,9 @@ module Apartment
|
|
53
75
|
@excluded_models || []
|
54
76
|
end
|
55
77
|
|
56
|
-
def default_schema
|
57
|
-
@default_schema || 'public' # TODO: 'public' is postgres specific
|
58
|
-
end
|
59
|
-
|
60
78
|
def parallel_migration_threads
|
61
79
|
@parallel_migration_threads || 0
|
62
80
|
end
|
63
|
-
alias default_tenant default_schema
|
64
|
-
alias default_tenant= default_schema=
|
65
81
|
|
66
82
|
def persistent_schemas
|
67
83
|
@persistent_schemas || []
|
@@ -89,7 +105,9 @@ module Apartment
|
|
89
105
|
|
90
106
|
# Reset all the config for Apartment
|
91
107
|
def reset
|
92
|
-
(ACCESSOR_METHODS + WRITER_METHODS).each
|
108
|
+
(ACCESSOR_METHODS + WRITER_METHODS).each do |method|
|
109
|
+
remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}")
|
110
|
+
end
|
93
111
|
end
|
94
112
|
|
95
113
|
def extract_tenant_config
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionHandling
|
5
|
+
def connected_to_with_tenant(database: nil, role: nil, prevent_writes: false, &blk)
|
6
|
+
current_tenant = Apartment::Tenant.current
|
7
|
+
|
8
|
+
connected_to_without_tenant(database: database, role: role, prevent_writes: prevent_writes) do
|
9
|
+
Apartment::Tenant.switch!(current_tenant)
|
10
|
+
yield(blk)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
alias connected_to_without_tenant connected_to
|
15
|
+
alias connected_to connected_to_with_tenant
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# rubocop:disable Rails/ApplicationRecord
|
4
|
+
class InternalMetadata < ActiveRecord::Base # :nodoc:
|
5
|
+
class << self
|
6
|
+
def table_exists?
|
7
|
+
connection.table_exists?(table_name)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
# rubocop:enable Rails/ApplicationRecord
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class LogSubscriber
|
5
|
+
def apartment_log
|
6
|
+
return unless Apartment.active_record_log
|
7
|
+
|
8
|
+
database = color("[#{Apartment.connection.current_database}] ", ActiveSupport::LogSubscriber::MAGENTA, true)
|
9
|
+
schema = nil
|
10
|
+
schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ", ActiveSupport::LogSubscriber::YELLOW, true) unless Apartment.connection.schema_search_path.nil?
|
11
|
+
"#{database}#{schema}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def payload_binds(binds, type_casted_binds)
|
15
|
+
return unless (binds || []).empty?
|
16
|
+
|
17
|
+
casted_params = type_casted_binds(type_casted_binds)
|
18
|
+
binds = ' ' + binds.zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect
|
19
|
+
binds
|
20
|
+
end
|
21
|
+
|
22
|
+
def sql(event)
|
23
|
+
self.class.runtime += event.duration
|
24
|
+
return unless logger.debug?
|
25
|
+
|
26
|
+
payload = event.payload
|
27
|
+
|
28
|
+
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
|
29
|
+
|
30
|
+
name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
|
31
|
+
name = "CACHE #{name}" if payload[:cached]
|
32
|
+
sql = payload[:sql]
|
33
|
+
binds = payload_binds(payload[:binds], payload[:type_casted_binds])
|
34
|
+
|
35
|
+
name = colorize_payload_name(name, payload[:name])
|
36
|
+
sql = color(sql, sql_color(sql), true) if colorize_logging
|
37
|
+
|
38
|
+
debug " #{apartment_log}#{name} #{sql}#{binds}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# rubocop:disable Rails/ApplicationRecord
|
5
|
+
class SchemaMigration < ActiveRecord::Base # :nodoc:
|
6
|
+
class << self
|
7
|
+
def table_exists?
|
8
|
+
connection.table_exists?(table_name)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
# rubocop:enable Rails/ApplicationRecord
|
13
|
+
end
|
@@ -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
|
@@ -48,7 +54,6 @@ module Apartment
|
|
48
54
|
def default_tenant
|
49
55
|
@default_tenant || Apartment.default_tenant
|
50
56
|
end
|
51
|
-
alias default_schema default_tenant # TODO: deprecate default_schema
|
52
57
|
|
53
58
|
# Drop the tenant
|
54
59
|
#
|
@@ -68,8 +73,6 @@ module Apartment
|
|
68
73
|
#
|
69
74
|
def switch!(tenant = nil)
|
70
75
|
run_callbacks :switch do
|
71
|
-
return reset if tenant.nil?
|
72
|
-
|
73
76
|
connect_to_new(tenant).tap do
|
74
77
|
Apartment.connection.clear_query_cache
|
75
78
|
end
|
@@ -103,7 +106,8 @@ module Apartment
|
|
103
106
|
# Establish a new connection for each specific excluded model
|
104
107
|
#
|
105
108
|
def process_excluded_models
|
106
|
-
# All other models will shared a connection (at Apartment.connection_class)
|
109
|
+
# All other models will shared a connection (at Apartment.connection_class)
|
110
|
+
# and we can modify at will
|
107
111
|
Apartment.excluded_models.each do |excluded_model|
|
108
112
|
process_excluded_model(excluded_model)
|
109
113
|
end
|
@@ -129,14 +133,12 @@ module Apartment
|
|
129
133
|
# @return {String} tenant name with Rails environment *optionally* prepended
|
130
134
|
#
|
131
135
|
def environmentify(tenant)
|
132
|
-
if
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
tenant
|
139
|
-
end
|
136
|
+
return tenant if tenant.nil? || tenant.include?(Rails.env)
|
137
|
+
|
138
|
+
if Apartment.prepend_environment
|
139
|
+
"#{Rails.env}_#{tenant}"
|
140
|
+
elsif Apartment.append_environment
|
141
|
+
"#{tenant}_#{Rails.env}"
|
140
142
|
else
|
141
143
|
tenant
|
142
144
|
end
|
@@ -174,6 +176,8 @@ module Apartment
|
|
174
176
|
# @param {String} tenant Database name
|
175
177
|
#
|
176
178
|
def connect_to_new(tenant)
|
179
|
+
return reset if tenant.nil?
|
180
|
+
|
177
181
|
query_cache_enabled = ActiveRecord::Base.connection.query_cache_enabled
|
178
182
|
|
179
183
|
Apartment.establish_connection multi_tenantify(tenant)
|
@@ -230,7 +234,7 @@ module Apartment
|
|
230
234
|
end
|
231
235
|
|
232
236
|
def db_connection_config(tenant)
|
233
|
-
Apartment.db_config_for(tenant).
|
237
|
+
Apartment.db_config_for(tenant).dup
|
234
238
|
end
|
235
239
|
|
236
240
|
def with_neutral_connection(tenant, &_block)
|
@@ -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
|
-
|
41
|
+
tenant = tenant.to_s
|
42
|
+
raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)
|
44
43
|
|
45
|
-
@current = tenant
|
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
|