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.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/.rubocop_todo.yml +1 -1
  4. data/.story_branch.yml +1 -0
  5. data/.travis.yml +13 -30
  6. data/Appraisals +4 -17
  7. data/Gemfile +2 -2
  8. data/Guardfile +3 -1
  9. data/HISTORY.md +90 -0
  10. data/README.md +31 -16
  11. data/Rakefile +34 -22
  12. data/TODO.md +0 -1
  13. data/gemfiles/rails_5_0.gemfile +10 -11
  14. data/gemfiles/rails_5_1.gemfile +10 -11
  15. data/gemfiles/rails_5_2.gemfile +9 -10
  16. data/gemfiles/rails_6_0.gemfile +10 -11
  17. data/gemfiles/rails_master.gemfile +10 -11
  18. data/lib/apartment.rb +31 -13
  19. data/lib/apartment/active_record/connection_handling.rb +17 -0
  20. data/lib/apartment/active_record/internal_metadata.rb +11 -0
  21. data/lib/apartment/active_record/log_subscriber.rb +41 -0
  22. data/lib/apartment/active_record/schema_migration.rb +13 -0
  23. data/lib/apartment/adapters/abstract_adapter.rb +17 -13
  24. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +9 -4
  25. data/lib/apartment/adapters/mysql2_adapter.rb +2 -0
  26. data/lib/apartment/adapters/postgresql_adapter.rb +33 -4
  27. data/lib/apartment/adapters/sqlite3_adapter.rb +2 -0
  28. data/lib/apartment/console.rb +7 -0
  29. data/lib/apartment/custom_console.rb +23 -7
  30. data/lib/apartment/model.rb +27 -0
  31. data/lib/apartment/railtie.rb +19 -15
  32. data/lib/apartment/tasks/task_helper.rb +35 -0
  33. data/lib/apartment/tenant.rb +16 -5
  34. data/lib/apartment/version.rb +1 -1
  35. data/lib/generators/apartment/install/templates/apartment.rb +5 -0
  36. data/lib/tasks/apartment.rake +16 -37
  37. data/{apartment.gemspec → ros-apartment.gemspec} +12 -6
  38. metadata +16 -213
  39. data/.github/workflows/.rubocop-linter.yml +0 -22
  40. data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -20
  41. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -39
  42. data/spec/adapters/mysql2_adapter_spec.rb +0 -61
  43. data/spec/adapters/postgresql_adapter_spec.rb +0 -63
  44. data/spec/adapters/sqlite3_adapter_spec.rb +0 -101
  45. data/spec/apartment_spec.rb +0 -13
  46. data/spec/config/database.yml.sample +0 -49
  47. data/spec/dummy/Rakefile +0 -7
  48. data/spec/dummy/app/controllers/application_controller.rb +0 -7
  49. data/spec/dummy/app/helpers/application_helper.rb +0 -4
  50. data/spec/dummy/app/models/application_record.rb +0 -6
  51. data/spec/dummy/app/models/company.rb +0 -5
  52. data/spec/dummy/app/models/user.rb +0 -5
  53. data/spec/dummy/app/views/application/index.html.erb +0 -1
  54. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  55. data/spec/dummy/config.ru +0 -6
  56. data/spec/dummy/config/application.rb +0 -51
  57. data/spec/dummy/config/boot.rb +0 -13
  58. data/spec/dummy/config/database.yml.sample +0 -44
  59. data/spec/dummy/config/environment.rb +0 -7
  60. data/spec/dummy/config/environments/development.rb +0 -29
  61. data/spec/dummy/config/environments/production.rb +0 -53
  62. data/spec/dummy/config/environments/test.rb +0 -36
  63. data/spec/dummy/config/initializers/apartment.rb +0 -6
  64. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -8
  65. data/spec/dummy/config/initializers/inflections.rb +0 -11
  66. data/spec/dummy/config/initializers/mime_types.rb +0 -6
  67. data/spec/dummy/config/initializers/secret_token.rb +0 -9
  68. data/spec/dummy/config/initializers/session_store.rb +0 -10
  69. data/spec/dummy/config/locales/en.yml +0 -5
  70. data/spec/dummy/config/routes.rb +0 -5
  71. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
  72. data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
  73. data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
  74. data/spec/dummy/db/schema.rb +0 -55
  75. data/spec/dummy/db/seeds.rb +0 -5
  76. data/spec/dummy/db/seeds/import.rb +0 -7
  77. data/spec/dummy/public/404.html +0 -26
  78. data/spec/dummy/public/422.html +0 -26
  79. data/spec/dummy/public/500.html +0 -26
  80. data/spec/dummy/public/favicon.ico +0 -0
  81. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  82. data/spec/dummy/script/rails +0 -8
  83. data/spec/dummy_engine/.gitignore +0 -8
  84. data/spec/dummy_engine/Gemfile +0 -15
  85. data/spec/dummy_engine/Rakefile +0 -34
  86. data/spec/dummy_engine/bin/rails +0 -12
  87. data/spec/dummy_engine/config/initializers/apartment.rb +0 -52
  88. data/spec/dummy_engine/dummy_engine.gemspec +0 -24
  89. data/spec/dummy_engine/lib/dummy_engine.rb +0 -6
  90. data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -6
  91. data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -5
  92. data/spec/dummy_engine/test/dummy/Rakefile +0 -6
  93. data/spec/dummy_engine/test/dummy/config.ru +0 -6
  94. data/spec/dummy_engine/test/dummy/config/application.rb +0 -24
  95. data/spec/dummy_engine/test/dummy/config/boot.rb +0 -7
  96. data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
  97. data/spec/dummy_engine/test/dummy/config/environment.rb +0 -7
  98. data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -39
  99. data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -80
  100. data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -41
  101. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -10
  102. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -8
  103. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -5
  104. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -6
  105. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -17
  106. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -5
  107. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -5
  108. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -16
  109. data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
  110. data/spec/dummy_engine/test/dummy/config/routes.rb +0 -58
  111. data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
  112. data/spec/examples/connection_adapter_examples.rb +0 -44
  113. data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -93
  114. data/spec/examples/generic_adapter_examples.rb +0 -164
  115. data/spec/examples/schema_adapter_examples.rb +0 -239
  116. data/spec/integration/apartment_rake_integration_spec.rb +0 -107
  117. data/spec/integration/query_caching_spec.rb +0 -83
  118. data/spec/integration/use_within_an_engine_spec.rb +0 -28
  119. data/spec/schemas/v1.rb +0 -15
  120. data/spec/schemas/v2.rb +0 -41
  121. data/spec/schemas/v3.rb +0 -47
  122. data/spec/spec_helper.rb +0 -63
  123. data/spec/support/apartment_helpers.rb +0 -47
  124. data/spec/support/capybara_sessions.rb +0 -15
  125. data/spec/support/config.rb +0 -13
  126. data/spec/support/contexts.rb +0 -54
  127. data/spec/support/requirements.rb +0 -48
  128. data/spec/support/setup.rb +0 -46
  129. data/spec/tasks/apartment_rake_spec.rb +0 -124
  130. data/spec/tenant_spec.rb +0 -194
  131. data/spec/unit/config_spec.rb +0 -111
  132. data/spec/unit/elevators/domain_spec.rb +0 -33
  133. data/spec/unit/elevators/first_subdomain_spec.rb +0 -26
  134. data/spec/unit/elevators/generic_spec.rb +0 -55
  135. data/spec/unit/elevators/host_hash_spec.rb +0 -33
  136. data/spec/unit/elevators/host_spec.rb +0 -89
  137. data/spec/unit/elevators/subdomain_spec.rb +0 -77
  138. data/spec/unit/migrator_spec.rb +0 -78
  139. data/spec/unit/reloader_spec.rb +0 -24
@@ -1,111 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Apartment do
6
- describe '#config' do
7
- let(:excluded_models) { ['Company'] }
8
- let(:seed_data_file_path) { Rails.root.join('db', 'seeds', 'import.rb') }
9
-
10
- def tenant_names_from_array(names)
11
- names.each_with_object({}) do |tenant, hash|
12
- hash[tenant] = Apartment.connection_config
13
- end.with_indifferent_access
14
- end
15
-
16
- it 'should yield the Apartment object' do
17
- Apartment.configure do |config|
18
- config.excluded_models = []
19
- expect(config).to eq(Apartment)
20
- end
21
- end
22
-
23
- it 'should set excluded models' do
24
- Apartment.configure do |config|
25
- config.excluded_models = excluded_models
26
- end
27
- expect(Apartment.excluded_models).to eq(excluded_models)
28
- end
29
-
30
- it 'should set use_schemas' do
31
- Apartment.configure do |config|
32
- config.excluded_models = []
33
- config.use_schemas = false
34
- end
35
- expect(Apartment.use_schemas).to be false
36
- end
37
-
38
- it 'should set seed_data_file' do
39
- Apartment.configure do |config|
40
- config.seed_data_file = seed_data_file_path
41
- end
42
- expect(Apartment.seed_data_file).to eq(seed_data_file_path)
43
- end
44
-
45
- it 'should set seed_after_create' do
46
- Apartment.configure do |config|
47
- config.excluded_models = []
48
- config.seed_after_create = true
49
- end
50
- expect(Apartment.seed_after_create).to be true
51
- end
52
-
53
- context 'databases' do
54
- let(:users_conf_hash) { { port: 5444 } }
55
-
56
- before do
57
- Apartment.configure do |config|
58
- config.tenant_names = tenant_names
59
- end
60
- end
61
-
62
- context 'tenant_names as string array' do
63
- let(:tenant_names) { %w[users companies] }
64
-
65
- it 'should return object if it doesnt respond_to call' do
66
- expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names).keys)
67
- end
68
-
69
- it 'should set tenants_with_config' do
70
- expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names))
71
- end
72
- end
73
-
74
- context 'tenant_names as proc returning an array' do
75
- let(:tenant_names) { -> { %w[users companies] } }
76
-
77
- it 'should return object if it doesnt respond_to call' do
78
- expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names.call).keys)
79
- end
80
-
81
- it 'should set tenants_with_config' do
82
- expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names.call))
83
- end
84
- end
85
-
86
- context 'tenant_names as Hash' do
87
- let(:tenant_names) { { users: users_conf_hash }.with_indifferent_access }
88
-
89
- it 'should return object if it doesnt respond_to call' do
90
- expect(Apartment.tenant_names).to eq(tenant_names.keys)
91
- end
92
-
93
- it 'should set tenants_with_config' do
94
- expect(Apartment.tenants_with_config).to eq(tenant_names)
95
- end
96
- end
97
-
98
- context 'tenant_names as proc returning a Hash' do
99
- let(:tenant_names) { -> { { users: users_conf_hash }.with_indifferent_access } }
100
-
101
- it 'should return object if it doesnt respond_to call' do
102
- expect(Apartment.tenant_names).to eq(tenant_names.call.keys)
103
- end
104
-
105
- it 'should set tenants_with_config' do
106
- expect(Apartment.tenants_with_config).to eq(tenant_names.call)
107
- end
108
- end
109
- end
110
- end
111
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'apartment/elevators/domain'
5
-
6
- describe Apartment::Elevators::Domain do
7
- subject(:elevator) { described_class.new(proc {}) }
8
-
9
- describe '#parse_tenant_name' do
10
- it 'parses the host for a domain name' do
11
- request = ActionDispatch::Request.new('HTTP_HOST' => 'example.com')
12
- expect(elevator.parse_tenant_name(request)).to eq('example')
13
- end
14
-
15
- it 'ignores a www prefix and domain suffix' do
16
- request = ActionDispatch::Request.new('HTTP_HOST' => 'www.example.bc.ca')
17
- expect(elevator.parse_tenant_name(request)).to eq('example')
18
- end
19
-
20
- it 'returns nil if there is no host' do
21
- request = ActionDispatch::Request.new('HTTP_HOST' => '')
22
- expect(elevator.parse_tenant_name(request)).to be_nil
23
- end
24
- end
25
-
26
- describe '#call' do
27
- it 'switches to the proper tenant' do
28
- expect(Apartment::Tenant).to receive(:switch).with('example')
29
-
30
- elevator.call('HTTP_HOST' => 'www.example.com')
31
- end
32
- end
33
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'apartment/elevators/first_subdomain'
5
-
6
- describe Apartment::Elevators::FirstSubdomain do
7
- describe 'subdomain' do
8
- subject { described_class.new('test').parse_tenant_name(request) }
9
- let(:request) { double(:request, host: "#{subdomain}.example.com") }
10
-
11
- context 'one subdomain' do
12
- let(:subdomain) { 'test' }
13
- it { is_expected.to eq('test') }
14
- end
15
-
16
- context 'nested subdomains' do
17
- let(:subdomain) { 'test1.test2' }
18
- it { is_expected.to eq('test1') }
19
- end
20
-
21
- context 'no subdomain' do
22
- let(:subdomain) { nil }
23
- it { is_expected.to eq(nil) }
24
- end
25
- end
26
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'apartment/elevators/generic'
5
-
6
- describe Apartment::Elevators::Generic do
7
- class MyElevator < described_class
8
- def parse_tenant_name(*)
9
- 'tenant2'
10
- end
11
- end
12
-
13
- subject(:elevator) { described_class.new(proc {}) }
14
-
15
- describe '#call' do
16
- it 'calls the processor if given' do
17
- elevator = described_class.new(proc {}, proc { 'tenant1' })
18
-
19
- expect(Apartment::Tenant).to receive(:switch).with('tenant1')
20
-
21
- elevator.call('HTTP_HOST' => 'foo.bar.com')
22
- end
23
-
24
- it 'raises if parse_tenant_name not implemented' do
25
- expect do
26
- elevator.call('HTTP_HOST' => 'foo.bar.com')
27
- end.to raise_error(RuntimeError)
28
- end
29
-
30
- it 'switches to the parsed db_name' do
31
- elevator = MyElevator.new(proc {})
32
-
33
- expect(Apartment::Tenant).to receive(:switch).with('tenant2')
34
-
35
- elevator.call('HTTP_HOST' => 'foo.bar.com')
36
- end
37
-
38
- it 'calls the block implementation of `switch`' do
39
- elevator = MyElevator.new(proc {}, proc { 'tenant2' })
40
-
41
- expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield
42
- elevator.call('HTTP_HOST' => 'foo.bar.com')
43
- end
44
-
45
- it 'does not call `switch` if no database given' do
46
- app = proc {}
47
- elevator = MyElevator.new(app, proc {})
48
-
49
- expect(Apartment::Tenant).not_to receive(:switch)
50
- expect(app).to receive :call
51
-
52
- elevator.call('HTTP_HOST' => 'foo.bar.com')
53
- end
54
- end
55
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'apartment/elevators/host_hash'
5
-
6
- describe Apartment::Elevators::HostHash do
7
- subject(:elevator) { Apartment::Elevators::HostHash.new(proc {}, 'example.com' => 'example_tenant') }
8
-
9
- describe '#parse_tenant_name' do
10
- it 'parses the host for a domain name' do
11
- request = ActionDispatch::Request.new('HTTP_HOST' => 'example.com')
12
- expect(elevator.parse_tenant_name(request)).to eq('example_tenant')
13
- end
14
-
15
- it 'raises TenantNotFound exception if there is no host' do
16
- request = ActionDispatch::Request.new('HTTP_HOST' => '')
17
- expect { elevator.parse_tenant_name(request) }.to raise_error(Apartment::TenantNotFound)
18
- end
19
-
20
- it 'raises TenantNotFound exception if there is no database associated to current host' do
21
- request = ActionDispatch::Request.new('HTTP_HOST' => 'example2.com')
22
- expect { elevator.parse_tenant_name(request) }.to raise_error(Apartment::TenantNotFound)
23
- end
24
- end
25
-
26
- describe '#call' do
27
- it 'switches to the proper tenant' do
28
- expect(Apartment::Tenant).to receive(:switch).with('example_tenant')
29
-
30
- elevator.call('HTTP_HOST' => 'example.com')
31
- end
32
- end
33
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'apartment/elevators/host'
5
-
6
- describe Apartment::Elevators::Host do
7
- subject(:elevator) { described_class.new(proc {}) }
8
-
9
- describe '#parse_tenant_name' do
10
- it 'should return nil when no host' do
11
- request = ActionDispatch::Request.new('HTTP_HOST' => '')
12
- expect(elevator.parse_tenant_name(request)).to be_nil
13
- end
14
-
15
- context 'assuming no ignored_first_subdomains' do
16
- before { allow(described_class).to receive(:ignored_first_subdomains).and_return([]) }
17
-
18
- context 'with 3 parts' do
19
- it 'should return the whole host' do
20
- request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com')
21
- expect(elevator.parse_tenant_name(request)).to eq('foo.bar.com')
22
- end
23
- end
24
-
25
- context 'with 6 parts' do
26
- it 'should return the whole host' do
27
- request = ActionDispatch::Request.new('HTTP_HOST' => 'one.two.three.foo.bar.com')
28
- expect(elevator.parse_tenant_name(request)).to eq('one.two.three.foo.bar.com')
29
- end
30
- end
31
- end
32
-
33
- context 'assuming ignored_first_subdomains is set' do
34
- before { allow(described_class).to receive(:ignored_first_subdomains).and_return(%w[www foo]) }
35
-
36
- context 'with 3 parts' do
37
- it 'should return host without www' do
38
- request = ActionDispatch::Request.new('HTTP_HOST' => 'www.bar.com')
39
- expect(elevator.parse_tenant_name(request)).to eq('bar.com')
40
- end
41
-
42
- it 'should return host without foo' do
43
- request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com')
44
- expect(elevator.parse_tenant_name(request)).to eq('bar.com')
45
- end
46
- end
47
-
48
- context 'with 6 parts' do
49
- it 'should return host without www' do
50
- request = ActionDispatch::Request.new('HTTP_HOST' => 'www.one.two.three.foo.bar.com')
51
- expect(elevator.parse_tenant_name(request)).to eq('one.two.three.foo.bar.com')
52
- end
53
-
54
- it 'should return host without www' do
55
- request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.one.two.three.bar.com')
56
- expect(elevator.parse_tenant_name(request)).to eq('one.two.three.bar.com')
57
- end
58
- end
59
- end
60
-
61
- context 'assuming localhost' do
62
- it 'should return localhost' do
63
- request = ActionDispatch::Request.new('HTTP_HOST' => 'localhost')
64
- expect(elevator.parse_tenant_name(request)).to eq('localhost')
65
- end
66
- end
67
-
68
- context 'assuming ip address' do
69
- it 'should return the ip address' do
70
- request = ActionDispatch::Request.new('HTTP_HOST' => '127.0.0.1')
71
- expect(elevator.parse_tenant_name(request)).to eq('127.0.0.1')
72
- end
73
- end
74
- end
75
-
76
- describe '#call' do
77
- it 'switches to the proper tenant' do
78
- allow(described_class).to receive(:ignored_first_subdomains).and_return([])
79
- expect(Apartment::Tenant).to receive(:switch).with('foo.bar.com')
80
- elevator.call('HTTP_HOST' => 'foo.bar.com')
81
- end
82
-
83
- it 'ignores ignored_first_subdomains' do
84
- allow(described_class).to receive(:ignored_first_subdomains).and_return(%w[foo])
85
- expect(Apartment::Tenant).to receive(:switch).with('bar.com')
86
- elevator.call('HTTP_HOST' => 'foo.bar.com')
87
- end
88
- end
89
- end
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'apartment/elevators/subdomain'
5
-
6
- describe Apartment::Elevators::Subdomain do
7
- subject(:elevator) { described_class.new(proc {}) }
8
-
9
- describe '#parse_tenant_name' do
10
- context 'assuming one tld' do
11
- it 'should parse subdomain' do
12
- request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com')
13
- expect(elevator.parse_tenant_name(request)).to eq('foo')
14
- end
15
-
16
- it 'should return nil when no subdomain' do
17
- request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.com')
18
- expect(elevator.parse_tenant_name(request)).to be_nil
19
- end
20
- end
21
-
22
- context 'assuming two tlds' do
23
- it 'should parse subdomain in the third level domain' do
24
- request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.co.uk')
25
- expect(elevator.parse_tenant_name(request)).to eq('foo')
26
- end
27
-
28
- it 'should return nil when no subdomain in the third level domain' do
29
- request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.co.uk')
30
- expect(elevator.parse_tenant_name(request)).to be_nil
31
- end
32
- end
33
-
34
- context 'assuming two subdomains' do
35
- it 'should parse two subdomains in the two level domain' do
36
- request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.xyz.bar.com')
37
- expect(elevator.parse_tenant_name(request)).to eq('foo')
38
- end
39
-
40
- it 'should parse two subdomains in the third level domain' do
41
- request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.xyz.bar.co.uk')
42
- expect(elevator.parse_tenant_name(request)).to eq('foo')
43
- end
44
- end
45
-
46
- context 'assuming localhost' do
47
- it 'should return nil for localhost' do
48
- request = ActionDispatch::Request.new('HTTP_HOST' => 'localhost')
49
- expect(elevator.parse_tenant_name(request)).to be_nil
50
- end
51
- end
52
-
53
- context 'assuming ip address' do
54
- it 'should return nil for an ip address' do
55
- request = ActionDispatch::Request.new('HTTP_HOST' => '127.0.0.1')
56
- expect(elevator.parse_tenant_name(request)).to be_nil
57
- end
58
- end
59
- end
60
-
61
- describe '#call' do
62
- it 'switches to the proper tenant' do
63
- expect(Apartment::Tenant).to receive(:switch).with('tenant1')
64
- elevator.call('HTTP_HOST' => 'tenant1.example.com')
65
- end
66
-
67
- it 'ignores excluded subdomains' do
68
- described_class.excluded_subdomains = %w[foo]
69
-
70
- expect(Apartment::Tenant).not_to receive(:switch)
71
-
72
- elevator.call('HTTP_HOST' => 'foo.bar.com')
73
-
74
- described_class.excluded_subdomains = nil
75
- end
76
- end
77
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'apartment/migrator'
5
-
6
- describe Apartment::Migrator do
7
- let(:tenant) { Apartment::Test.next_db }
8
-
9
- # Don't need a real switch here, just testing behaviour
10
- before { allow(Apartment::Tenant.adapter).to receive(:connect_to_new) }
11
-
12
- context 'with ActiveRecord below 5.2.0', skip: ActiveRecord.version >= Gem::Version.new('5.2.0') do
13
- before do
14
- allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w[spec/dummy/db/migrate] }
15
- allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true }
16
- end
17
-
18
- describe '::migrate' do
19
- it 'switches and migrates' do
20
- expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
21
- expect(ActiveRecord::Migrator).to receive(:migrate)
22
-
23
- Apartment::Migrator.migrate(tenant)
24
- end
25
- end
26
-
27
- describe '::run' do
28
- it 'switches and runs' do
29
- expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
30
- expect(ActiveRecord::Migrator).to receive(:run).with(:up, anything, 1234)
31
-
32
- Apartment::Migrator.run(:up, tenant, 1234)
33
- end
34
- end
35
-
36
- describe '::rollback' do
37
- it 'switches and rolls back' do
38
- expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
39
- expect(ActiveRecord::Migrator).to receive(:rollback).with(anything, 2)
40
-
41
- Apartment::Migrator.rollback(tenant, 2)
42
- end
43
- end
44
- end
45
-
46
- context 'with ActiveRecord above or equal to 5.2.0', skip: ActiveRecord.version < Gem::Version.new('5.2.0') do
47
- before do
48
- allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
49
- end
50
-
51
- describe '::migrate' do
52
- it 'switches and migrates' do
53
- expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
54
- expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:migrate)
55
-
56
- Apartment::Migrator.migrate(tenant)
57
- end
58
- end
59
-
60
- describe '::run' do
61
- it 'switches and runs' do
62
- expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
63
- expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:run).with(:up, 1234)
64
-
65
- Apartment::Migrator.run(:up, tenant, 1234)
66
- end
67
- end
68
-
69
- describe '::rollback' do
70
- it 'switches and rolls back' do
71
- expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
72
- expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:rollback).with(2)
73
-
74
- Apartment::Migrator.rollback(tenant, 2)
75
- end
76
- end
77
- end
78
- end