ros-apartment 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/.rubocop-linter.yml +22 -0
- data/.pryrc +5 -3
- data/.rubocop.yml +17 -0
- data/.rubocop_todo.yml +29 -0
- data/.story_branch.yml +4 -0
- data/.travis.yml +12 -11
- data/Appraisals +18 -18
- data/Gemfile +4 -1
- data/README.md +33 -5
- data/Rakefile +2 -0
- data/apartment.gemspec +6 -6
- data/gemfiles/rails_4_2.gemfile +12 -10
- data/gemfiles/rails_5_0.gemfile +11 -9
- data/gemfiles/rails_5_1.gemfile +11 -9
- data/gemfiles/rails_5_2.gemfile +10 -8
- data/gemfiles/rails_6_0.gemfile +11 -9
- data/gemfiles/rails_master.gemfile +11 -9
- data/lib/apartment/adapters/abstract_adapter.rb +34 -34
- data/lib/apartment/adapters/abstract_jdbc_adapter.rb +4 -3
- data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +13 -11
- data/lib/apartment/adapters/mysql2_adapter.rb +10 -9
- data/lib/apartment/adapters/postgis_adapter.rb +3 -2
- data/lib/apartment/adapters/postgresql_adapter.rb +28 -25
- data/lib/apartment/adapters/sqlite3_adapter.rb +16 -8
- data/lib/apartment/console.rb +28 -3
- data/lib/apartment/custom_console.rb +26 -0
- data/lib/apartment/deprecation.rb +2 -1
- data/lib/apartment/elevators/domain.rb +4 -3
- data/lib/apartment/elevators/first_subdomain.rb +3 -2
- data/lib/apartment/elevators/generic.rb +4 -3
- data/lib/apartment/elevators/host.rb +6 -1
- data/lib/apartment/elevators/host_hash.rb +6 -2
- data/lib/apartment/elevators/subdomain.rb +9 -5
- data/lib/apartment/migrator.rb +4 -3
- data/lib/apartment/railtie.rb +13 -5
- data/lib/apartment/reloader.rb +2 -1
- data/lib/apartment/tasks/enhancements.rb +4 -6
- data/lib/apartment/tenant.rb +3 -4
- data/lib/apartment/version.rb +3 -1
- data/lib/apartment.rb +15 -9
- data/lib/generators/apartment/install/install_generator.rb +4 -3
- data/lib/generators/apartment/install/templates/apartment.rb +3 -2
- data/lib/tasks/apartment.rake +19 -18
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +5 -4
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +10 -12
- data/spec/adapters/mysql2_adapter_spec.rb +15 -13
- data/spec/adapters/postgresql_adapter_spec.rb +22 -20
- data/spec/adapters/sqlite3_adapter_spec.rb +41 -23
- data/spec/apartment_spec.rb +4 -2
- data/spec/dummy/app/controllers/application_controller.rb +4 -3
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/application_record.rb +6 -0
- data/spec/dummy/app/models/company.rb +4 -2
- data/spec/dummy/app/models/user.rb +4 -2
- data/spec/dummy/config/application.rb +11 -9
- data/spec/dummy/config/boot.rb +4 -2
- data/spec/dummy/config/environment.rb +3 -1
- data/spec/dummy/config/environments/development.rb +2 -1
- data/spec/dummy/config/environments/production.rb +3 -1
- data/spec/dummy/config/environments/test.rb +3 -1
- data/spec/dummy/config/initializers/apartment.rb +4 -2
- data/spec/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/spec/dummy/config/initializers/inflections.rb +1 -0
- data/spec/dummy/config/initializers/mime_types.rb +1 -0
- data/spec/dummy/config/initializers/secret_token.rb +2 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -1
- data/spec/dummy/config/routes.rb +3 -1
- data/spec/dummy/config.ru +3 -1
- data/spec/dummy/db/seeds/import.rb +3 -1
- data/spec/dummy/script/rails +4 -2
- data/spec/dummy_engine/config/initializers/apartment.rb +4 -3
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +2 -0
- data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -1
- data/spec/dummy_engine/lib/dummy_engine.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/application.rb +4 -2
- data/spec/dummy_engine/test/dummy/config/boot.rb +4 -2
- data/spec/dummy_engine/test/dummy/config/environment.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/routes.rb +2 -0
- data/spec/dummy_engine/test/dummy/config.ru +3 -1
- data/spec/examples/connection_adapter_examples.rb +15 -13
- data/spec/examples/generic_adapter_custom_configuration_example.rb +21 -23
- data/spec/examples/generic_adapter_examples.rb +40 -39
- data/spec/examples/schema_adapter_examples.rb +93 -88
- data/spec/integration/apartment_rake_integration_spec.rb +24 -24
- data/spec/integration/query_caching_spec.rb +10 -8
- data/spec/integration/use_within_an_engine_spec.rb +5 -5
- data/spec/schemas/v1.rb +2 -3
- data/spec/schemas/v2.rb +22 -24
- data/spec/schemas/v3.rb +26 -28
- data/spec/spec_helper.rb +8 -6
- data/spec/support/apartment_helpers.rb +9 -5
- data/spec/support/capybara_sessions.rb +5 -5
- data/spec/support/config.rb +5 -2
- data/spec/support/contexts.rb +10 -8
- data/spec/support/requirements.rb +19 -6
- data/spec/support/setup.rb +5 -5
- data/spec/tasks/apartment_rake_spec.rb +30 -35
- data/spec/tenant_spec.rb +48 -44
- data/spec/unit/config_spec.rb +26 -27
- data/spec/unit/elevators/domain_spec.rb +9 -8
- data/spec/unit/elevators/first_subdomain_spec.rb +14 -12
- data/spec/unit/elevators/generic_spec.rb +16 -15
- data/spec/unit/elevators/host_hash_spec.rb +9 -8
- data/spec/unit/elevators/host_spec.rb +26 -26
- data/spec/unit/elevators/subdomain_spec.rb +24 -23
- data/spec/unit/migrator_spec.rb +18 -17
- data/spec/unit/reloader_spec.rb +8 -8
- metadata +40 -22
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/elevators/generic'
|
2
4
|
|
3
5
|
module Apartment
|
@@ -8,14 +10,13 @@ module Apartment
|
|
8
10
|
# eg. example.com => example
|
9
11
|
# www.example.bc.ca => example
|
10
12
|
# a.example.bc.ca => a
|
11
|
-
#
|
13
|
+
#
|
12
14
|
#
|
13
15
|
class Domain < Generic
|
14
|
-
|
15
16
|
def parse_tenant_name(request)
|
16
17
|
return nil if request.host.blank?
|
17
18
|
|
18
|
-
request.host.match(/(www\.)?(?<sld>[^.]*)/)[
|
19
|
+
request.host.match(/(www\.)?(?<sld>[^.]*)/)['sld']
|
19
20
|
end
|
20
21
|
end
|
21
22
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/elevators/subdomain'
|
2
4
|
|
3
5
|
module Apartment
|
@@ -8,10 +10,9 @@ module Apartment
|
|
8
10
|
# - example1.domain.com => example1
|
9
11
|
# - example2.something.domain.com => example2
|
10
12
|
class FirstSubdomain < Subdomain
|
11
|
-
|
12
13
|
def parse_tenant_name(request)
|
13
14
|
super.split('.')[0] unless super.nil?
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
17
|
-
end
|
18
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rack/request'
|
2
4
|
require 'apartment/tenant'
|
3
5
|
|
@@ -6,7 +8,6 @@ module Apartment
|
|
6
8
|
# Provides a rack based tenant switching solution based on request
|
7
9
|
#
|
8
10
|
class Generic
|
9
|
-
|
10
11
|
def initialize(app, processor = nil)
|
11
12
|
@app = app
|
12
13
|
@processor = processor || method(:parse_tenant_name)
|
@@ -24,8 +25,8 @@ module Apartment
|
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
27
|
-
def parse_tenant_name(
|
28
|
-
raise
|
28
|
+
def parse_tenant_name(_request)
|
29
|
+
raise 'Override'
|
29
30
|
end
|
30
31
|
end
|
31
32
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/elevators/generic'
|
2
4
|
|
3
5
|
module Apartment
|
@@ -16,15 +18,18 @@ module Apartment
|
|
16
18
|
@ignored_first_subdomains ||= []
|
17
19
|
end
|
18
20
|
|
21
|
+
# rubocop:disable Style/TrivialAccessors
|
19
22
|
def self.ignored_first_subdomains=(arg)
|
20
23
|
@ignored_first_subdomains = arg
|
21
24
|
end
|
25
|
+
# rubocop:enable Style/TrivialAccessors
|
22
26
|
|
23
27
|
def parse_tenant_name(request)
|
24
28
|
return nil if request.host.blank?
|
29
|
+
|
25
30
|
parts = request.host.split('.')
|
26
31
|
self.class.ignored_first_subdomains.include?(parts[0]) ? parts.drop(1).join('.') : request.host
|
27
32
|
end
|
28
33
|
end
|
29
34
|
end
|
30
|
-
end
|
35
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/elevators/generic'
|
2
4
|
|
3
5
|
module Apartment
|
@@ -12,8 +14,10 @@ module Apartment
|
|
12
14
|
end
|
13
15
|
|
14
16
|
def parse_tenant_name(request)
|
15
|
-
|
16
|
-
|
17
|
+
unless @hash.key?(request.host)
|
18
|
+
raise TenantNotFound,
|
19
|
+
"Cannot find tenant for host #{request.host}"
|
20
|
+
end
|
17
21
|
|
18
22
|
@hash[request.host]
|
19
23
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/elevators/generic'
|
2
4
|
require 'public_suffix'
|
3
5
|
|
@@ -11,9 +13,11 @@ module Apartment
|
|
11
13
|
@excluded_subdomains ||= []
|
12
14
|
end
|
13
15
|
|
16
|
+
# rubocop:disable Style/TrivialAccessors
|
14
17
|
def self.excluded_subdomains=(arg)
|
15
18
|
@excluded_subdomains = arg
|
16
19
|
end
|
20
|
+
# rubocop:enable Style/TrivialAccessors
|
17
21
|
|
18
22
|
def parse_tenant_name(request)
|
19
23
|
request_subdomain = subdomain(request.host)
|
@@ -21,15 +25,15 @@ module Apartment
|
|
21
25
|
# If the domain acquired is set to be excluded, set the tenant to whatever is currently
|
22
26
|
# next in line in the schema search path.
|
23
27
|
tenant = if self.class.excluded_subdomains.include?(request_subdomain)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
+
nil
|
29
|
+
else
|
30
|
+
request_subdomain
|
31
|
+
end
|
28
32
|
|
29
33
|
tenant.presence
|
30
34
|
end
|
31
35
|
|
32
|
-
|
36
|
+
protected
|
33
37
|
|
34
38
|
# *Almost* a direct ripoff of ActionDispatch::Request subdomain methods
|
35
39
|
|
data/lib/apartment/migrator.rb
CHANGED
@@ -1,16 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/tenant'
|
2
4
|
|
3
5
|
module Apartment
|
4
6
|
module Migrator
|
5
|
-
|
6
7
|
extend self
|
7
8
|
|
8
9
|
# Migrate to latest
|
9
10
|
def migrate(database)
|
10
11
|
Tenant.switch(database) do
|
11
|
-
version = ENV[
|
12
|
+
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
12
13
|
|
13
|
-
migration_scope_block = ->
|
14
|
+
migration_scope_block = ->(migration) { ENV['SCOPE'].blank? || (ENV['SCOPE'] == migration.scope) }
|
14
15
|
|
15
16
|
if activerecord_below_5_2?
|
16
17
|
ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, version, &migration_scope_block)
|
data/lib/apartment/railtie.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails'
|
2
4
|
require 'apartment/tenant'
|
3
5
|
require 'apartment/reloader'
|
4
6
|
|
5
7
|
module Apartment
|
6
8
|
class Railtie < Rails::Railtie
|
7
|
-
|
8
9
|
#
|
9
10
|
# Set up our default config options
|
10
11
|
# Do this before the app initializers run so we don't override custom settings
|
@@ -28,14 +29,18 @@ module Apartment
|
|
28
29
|
#
|
29
30
|
config.to_prepare do
|
30
31
|
next if ARGV.any? { |arg| arg =~ /\Aassets:(?:precompile|clean)\z/ }
|
32
|
+
next if ARGV.any? { |arg| arg == 'webpacker:compile' }
|
31
33
|
|
32
34
|
begin
|
33
35
|
Apartment.connection_class.connection_pool.with_connection do
|
34
36
|
Apartment::Tenant.init
|
35
37
|
end
|
36
|
-
rescue ::ActiveRecord::NoDatabaseError
|
38
|
+
rescue ::ActiveRecord::NoDatabaseError, PG::ConnectionBad
|
37
39
|
# Since `db:create` and other tasks invoke this block from Rails 5.2.0,
|
38
40
|
# we need to swallow the error to execute `db:create` properly.
|
41
|
+
Rails.logger.warn do
|
42
|
+
'Failed to initialize Apartment because a database connection could not be established.'
|
43
|
+
end
|
39
44
|
end
|
40
45
|
end
|
41
46
|
|
@@ -48,8 +53,10 @@ module Apartment
|
|
48
53
|
end
|
49
54
|
|
50
55
|
#
|
51
|
-
# The following initializers are a workaround to the fact that I can't
|
52
|
-
#
|
56
|
+
# The following initializers are a workaround to the fact that I can't
|
57
|
+
# properly hook into the rails reloader
|
58
|
+
# Note this is technically valid for any environment where cache_classes
|
59
|
+
# is false, for us, it's just development
|
53
60
|
#
|
54
61
|
if Rails.env.development?
|
55
62
|
|
@@ -58,7 +65,8 @@ module Apartment
|
|
58
65
|
app.config.middleware.use Apartment::Reloader
|
59
66
|
end
|
60
67
|
|
61
|
-
# Overrides reload! to also call Apartment::Tenant.init as well so that the
|
68
|
+
# Overrides reload! to also call Apartment::Tenant.init as well so that the
|
69
|
+
# reloaded classes have the proper table_names
|
62
70
|
console do
|
63
71
|
require 'apartment/console'
|
64
72
|
end
|
data/lib/apartment/reloader.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Apartment
|
2
4
|
class Reloader
|
3
|
-
|
4
5
|
# Middleware used in development to init Apartment for each request
|
5
6
|
# Necessary due to code reload (annoying). When models are reloaded, they no longer have the proper table_name
|
6
7
|
# That is prepended with the schema (if using postgresql schemas)
|
@@ -1,12 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Require this file to append Apartment rake tasks to ActiveRecord db rake tasks
|
2
4
|
# Enabled by default in the initializer
|
3
5
|
|
4
6
|
module Apartment
|
5
7
|
class RakeTaskEnhancer
|
6
|
-
|
7
8
|
module TASKS
|
8
|
-
ENHANCE_BEFORE = %w
|
9
|
-
ENHANCE_AFTER = %w
|
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
11
|
freeze
|
11
12
|
end
|
12
13
|
|
@@ -28,7 +29,6 @@ module Apartment
|
|
28
29
|
task = Rake::Task[name]
|
29
30
|
enhance_after_task(task)
|
30
31
|
end
|
31
|
-
|
32
32
|
end
|
33
33
|
|
34
34
|
def should_enhance?
|
@@ -48,9 +48,7 @@ module Apartment
|
|
48
48
|
def inserted_task_name(task)
|
49
49
|
task.name.sub(/db:/, 'apartment:')
|
50
50
|
end
|
51
|
-
|
52
51
|
end
|
53
|
-
|
54
52
|
end
|
55
53
|
end
|
56
54
|
|
data/lib/apartment/tenant.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'forwardable'
|
2
4
|
|
3
5
|
module Apartment
|
4
6
|
# The main entry point to Apartment functions
|
5
7
|
#
|
6
8
|
module Tenant
|
7
|
-
|
8
9
|
extend self
|
9
10
|
extend Forwardable
|
10
11
|
|
@@ -40,9 +41,7 @@ module Apartment
|
|
40
41
|
raise "The adapter `#{adapter_method}` is not yet supported"
|
41
42
|
end
|
42
43
|
|
43
|
-
unless respond_to?(adapter_method)
|
44
|
-
raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter"
|
45
|
-
end
|
44
|
+
raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter" unless respond_to?(adapter_method)
|
46
45
|
|
47
46
|
send(adapter_method, config)
|
48
47
|
end
|
data/lib/apartment/version.rb
CHANGED
data/lib/apartment.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/railtie' if defined?(Rails)
|
2
4
|
require 'active_support/core_ext/object/blank'
|
3
5
|
require 'forwardable'
|
@@ -5,13 +7,16 @@ require 'active_record'
|
|
5
7
|
require 'apartment/tenant'
|
6
8
|
|
7
9
|
module Apartment
|
8
|
-
|
9
10
|
class << self
|
10
|
-
|
11
11
|
extend Forwardable
|
12
12
|
|
13
|
-
ACCESSOR_METHODS
|
14
|
-
|
13
|
+
ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment
|
14
|
+
append_environment with_multi_server_setup].freeze
|
15
|
+
|
16
|
+
WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
|
17
|
+
default_schema persistent_schemas connection_class
|
18
|
+
tld_length db_migrate_tenants seed_data_file
|
19
|
+
parallel_migration_threads pg_excluded_names].freeze
|
15
20
|
|
16
21
|
attr_accessor(*ACCESSOR_METHODS)
|
17
22
|
attr_writer(*WRITER_METHODS)
|
@@ -49,14 +54,14 @@ module Apartment
|
|
49
54
|
end
|
50
55
|
|
51
56
|
def default_schema
|
52
|
-
@default_schema ||
|
57
|
+
@default_schema || 'public' # TODO: 'public' is postgres specific
|
53
58
|
end
|
54
59
|
|
55
60
|
def parallel_migration_threads
|
56
61
|
@parallel_migration_threads || 0
|
57
62
|
end
|
58
|
-
alias
|
59
|
-
alias
|
63
|
+
alias default_tenant default_schema
|
64
|
+
alias default_tenant= default_schema=
|
60
65
|
|
61
66
|
def persistent_schemas
|
62
67
|
@persistent_schemas || []
|
@@ -75,7 +80,7 @@ module Apartment
|
|
75
80
|
def seed_data_file
|
76
81
|
return @seed_data_file if defined?(@seed_data_file)
|
77
82
|
|
78
|
-
@seed_data_file =
|
83
|
+
@seed_data_file = Rails.root.join('db', 'seeds.rb')
|
79
84
|
end
|
80
85
|
|
81
86
|
def pg_excluded_names
|
@@ -84,11 +89,12 @@ module Apartment
|
|
84
89
|
|
85
90
|
# Reset all the config for Apartment
|
86
91
|
def reset
|
87
|
-
(ACCESSOR_METHODS + WRITER_METHODS).each{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
|
92
|
+
(ACCESSOR_METHODS + WRITER_METHODS).each { |method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
|
88
93
|
end
|
89
94
|
|
90
95
|
def extract_tenant_config
|
91
96
|
return {} unless @tenant_names
|
97
|
+
|
92
98
|
values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
|
93
99
|
unless values.is_a? Hash
|
94
100
|
values = values.each_with_object({}) do |tenant, hash|
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Apartment
|
2
4
|
class InstallGenerator < Rails::Generators::Base
|
3
|
-
source_root File.expand_path('
|
5
|
+
source_root File.expand_path('templates', __dir__)
|
4
6
|
|
5
7
|
def copy_files
|
6
|
-
template
|
8
|
+
template 'apartment.rb', File.join('config', 'initializers', 'apartment.rb')
|
7
9
|
end
|
8
|
-
|
9
10
|
end
|
10
11
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# You can have Apartment route to the appropriate Tenant by adding some Rack middleware.
|
2
4
|
# Apartment can support many different "Elevators" that can take care of this routing to your data.
|
3
5
|
# Require whichever Elevator you're using below or none if you have a custom one.
|
@@ -12,7 +14,6 @@ require 'apartment/elevators/subdomain'
|
|
12
14
|
# Apartment Configuration
|
13
15
|
#
|
14
16
|
Apartment.configure do |config|
|
15
|
-
|
16
17
|
# Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace.
|
17
18
|
# A typical example would be a Customer or Tenant model that stores each Tenant's information.
|
18
19
|
#
|
@@ -48,7 +49,7 @@ Apartment.configure do |config|
|
|
48
49
|
# end
|
49
50
|
# end
|
50
51
|
#
|
51
|
-
config.tenant_names =
|
52
|
+
config.tenant_names = -> { ToDo_Tenant_Or_User_Model.pluck :database }
|
52
53
|
|
53
54
|
# PostgreSQL:
|
54
55
|
# Specifies whether to use PostgreSQL schemas or create a new database per Tenant.
|
data/lib/tasks/apartment.rake
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'apartment/migrator'
|
2
4
|
require 'parallel'
|
3
5
|
|
4
6
|
apartment_namespace = namespace :apartment do
|
5
|
-
|
6
|
-
desc "Create all tenants"
|
7
|
+
desc 'Create all tenants'
|
7
8
|
task :create do
|
8
9
|
tenants.each do |tenant|
|
9
10
|
begin
|
@@ -15,19 +16,19 @@ apartment_namespace = namespace :apartment do
|
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
|
-
desc
|
19
|
+
desc 'Drop all tenants'
|
19
20
|
task :drop do
|
20
21
|
tenants.each do |tenant|
|
21
22
|
begin
|
22
23
|
puts("Dropping #{tenant} tenant")
|
23
24
|
Apartment::Tenant.drop(tenant)
|
24
|
-
rescue Apartment::TenantNotFound => e
|
25
|
+
rescue Apartment::TenantNotFound, ActiveRecord::NoDatabaseError => e
|
25
26
|
puts e.message
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
desc
|
31
|
+
desc 'Migrate all tenants'
|
31
32
|
task :migrate do
|
32
33
|
warn_if_tenants_empty
|
33
34
|
each_tenant do |tenant|
|
@@ -40,7 +41,7 @@ apartment_namespace = namespace :apartment do
|
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
43
|
-
desc
|
44
|
+
desc 'Seed all tenants'
|
44
45
|
task :seed do
|
45
46
|
warn_if_tenants_empty
|
46
47
|
|
@@ -56,7 +57,7 @@ apartment_namespace = namespace :apartment do
|
|
56
57
|
end
|
57
58
|
end
|
58
59
|
|
59
|
-
desc
|
60
|
+
desc 'Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants.'
|
60
61
|
task :rollback do
|
61
62
|
warn_if_tenants_empty
|
62
63
|
|
@@ -107,7 +108,7 @@ apartment_namespace = namespace :apartment do
|
|
107
108
|
end
|
108
109
|
end
|
109
110
|
|
110
|
-
desc
|
111
|
+
desc 'Rolls back the tenant one migration and re migrate up (options: STEP=x, VERSION=x).'
|
111
112
|
task :redo do
|
112
113
|
if ENV['VERSION']
|
113
114
|
apartment_namespace['migrate:down'].invoke
|
@@ -126,20 +127,20 @@ apartment_namespace = namespace :apartment do
|
|
126
127
|
end
|
127
128
|
|
128
129
|
def tenants
|
129
|
-
ENV['DB'] ? ENV['DB'].split(',').map
|
130
|
+
ENV['DB'] ? ENV['DB'].split(',').map(&:strip) : Apartment.tenant_names || []
|
130
131
|
end
|
131
132
|
|
132
133
|
def warn_if_tenants_empty
|
133
|
-
|
134
|
-
puts <<-WARNING
|
135
|
-
[WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
|
134
|
+
return unless tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != 'true'
|
136
135
|
|
137
|
-
|
138
|
-
|
139
|
-
* `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`
|
136
|
+
puts <<-WARNING
|
137
|
+
[WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
|
140
138
|
|
141
|
-
|
142
|
-
|
143
|
-
|
139
|
+
1. You may not have created any, in which case you can ignore this message
|
140
|
+
2. You've run `apartment:migrate` directly without loading the Rails environment
|
141
|
+
* `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`
|
142
|
+
|
143
|
+
Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
|
144
|
+
WARNING
|
144
145
|
end
|
145
146
|
end
|
@@ -1,19 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
if defined?(JRUBY_VERSION)
|
2
4
|
|
3
5
|
require 'spec_helper'
|
4
6
|
require 'apartment/adapters/jdbc_mysql_adapter'
|
5
7
|
|
6
8
|
describe Apartment::Adapters::JDBCMysqlAdapter, database: :mysql do
|
7
|
-
|
8
9
|
subject { Apartment::Tenant.jdbc_mysql_adapter config.symbolize_keys }
|
9
10
|
|
10
11
|
def tenant_names
|
11
|
-
ActiveRecord::Base.connection.execute(
|
12
|
+
ActiveRecord::Base.connection.execute('SELECT schema_name FROM information_schema.schemata').collect { |row| row['schema_name'] }
|
12
13
|
end
|
13
14
|
|
14
15
|
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
15
16
|
|
16
|
-
it_should_behave_like
|
17
|
-
it_should_behave_like
|
17
|
+
it_should_behave_like 'a generic apartment adapter'
|
18
|
+
it_should_behave_like 'a connection based apartment adapter'
|
18
19
|
end
|
19
20
|
end
|
@@ -1,41 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
if defined?(JRUBY_VERSION)
|
2
4
|
|
3
5
|
require 'spec_helper'
|
4
6
|
require 'apartment/adapters/jdbc_postgresql_adapter'
|
5
7
|
|
6
8
|
describe Apartment::Adapters::JDBCPostgresqlAdapter, database: :postgresql do
|
7
|
-
|
8
9
|
subject { Apartment::Tenant.jdbc_postgresql_adapter config.symbolize_keys }
|
9
10
|
|
10
|
-
context
|
11
|
-
|
11
|
+
context 'using schemas' do
|
12
12
|
before { Apartment.use_schemas = true }
|
13
13
|
|
14
14
|
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
|
15
15
|
def tenant_names
|
16
|
-
ActiveRecord::Base.connection.execute(
|
16
|
+
ActiveRecord::Base.connection.execute('SELECT nspname FROM pg_namespace;').collect { |row| row['nspname'] }
|
17
17
|
end
|
18
18
|
|
19
19
|
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } }
|
20
20
|
|
21
|
-
it_should_behave_like
|
22
|
-
it_should_behave_like
|
21
|
+
it_should_behave_like 'a generic apartment adapter'
|
22
|
+
it_should_behave_like 'a schema based apartment adapter'
|
23
23
|
end
|
24
24
|
|
25
|
-
context
|
26
|
-
|
25
|
+
context 'using databases' do
|
27
26
|
before { Apartment.use_schemas = false }
|
28
27
|
|
29
28
|
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
|
30
29
|
def tenant_names
|
31
|
-
connection.execute(
|
30
|
+
connection.execute('select datname from pg_database;').collect { |row| row['datname'] }
|
32
31
|
end
|
33
32
|
|
34
33
|
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
35
34
|
|
36
|
-
it_should_behave_like
|
37
|
-
it_should_behave_like
|
38
|
-
|
35
|
+
it_should_behave_like 'a generic apartment adapter'
|
36
|
+
it_should_behave_like 'a connection based apartment adapter'
|
39
37
|
end
|
40
38
|
end
|
41
39
|
end
|
@@ -1,34 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'apartment/adapters/mysql2_adapter'
|
3
5
|
|
4
6
|
describe Apartment::Adapters::Mysql2Adapter, database: :mysql do
|
5
7
|
unless defined?(JRUBY_VERSION)
|
6
8
|
|
7
|
-
subject(:adapter){ Apartment::Tenant.mysql2_adapter config }
|
9
|
+
subject(:adapter) { Apartment::Tenant.mysql2_adapter config }
|
8
10
|
|
9
11
|
def tenant_names
|
10
|
-
ActiveRecord::Base.connection.execute(
|
12
|
+
ActiveRecord::Base.connection.execute('SELECT schema_name FROM information_schema.schemata').collect { |row| row[0] }
|
11
13
|
end
|
12
14
|
|
13
15
|
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
14
16
|
|
15
|
-
context
|
17
|
+
context 'using - the equivalent of - schemas' do
|
16
18
|
before { Apartment.use_schemas = true }
|
17
19
|
|
18
|
-
it_should_behave_like
|
20
|
+
it_should_behave_like 'a generic apartment adapter'
|
19
21
|
|
20
|
-
describe
|
21
|
-
it
|
22
|
+
describe '#default_tenant' do
|
23
|
+
it 'is set to the original db from config' do
|
22
24
|
expect(subject.default_tenant).to eq(config[:database])
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
26
|
-
describe
|
28
|
+
describe '#init' do
|
27
29
|
include Apartment::Spec::AdapterRequirements
|
28
30
|
|
29
31
|
before do
|
30
32
|
Apartment.configure do |config|
|
31
|
-
config.excluded_models = [
|
33
|
+
config.excluded_models = ['Company']
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
@@ -40,7 +42,7 @@ describe Apartment::Adapters::Mysql2Adapter, database: :mysql do
|
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
43
|
-
it
|
45
|
+
it 'should process model exclusions' do
|
44
46
|
Apartment::Tenant.init
|
45
47
|
|
46
48
|
expect(Company.table_name).to eq("#{default_tenant}.companies")
|
@@ -48,12 +50,12 @@ describe Apartment::Adapters::Mysql2Adapter, database: :mysql do
|
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
51
|
-
context
|
53
|
+
context 'using connections' do
|
52
54
|
before { Apartment.use_schemas = false }
|
53
55
|
|
54
|
-
it_should_behave_like
|
55
|
-
it_should_behave_like
|
56
|
-
it_should_behave_like
|
56
|
+
it_should_behave_like 'a generic apartment adapter'
|
57
|
+
it_should_behave_like 'a generic apartment adapter able to handle custom configuration'
|
58
|
+
it_should_behave_like 'a connection based apartment adapter'
|
57
59
|
end
|
58
60
|
end
|
59
61
|
end
|