ros-apartment 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +8 -30
- data/Appraisals +4 -17
- data/Gemfile +1 -1
- data/Guardfile +3 -1
- data/README.md +15 -14
- data/Rakefile +34 -22
- data/apartment.gemspec +11 -3
- 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/active_record/connection_handling.rb +17 -0
- data/lib/apartment/active_record/internal_metadata.rb +11 -0
- data/lib/apartment/active_record/schema_migration.rb +13 -0
- data/lib/apartment/adapters/abstract_adapter.rb +3 -2
- data/lib/apartment/model.rb +27 -0
- data/lib/apartment/tenant.rb +3 -1
- data/lib/apartment/version.rb +1 -1
- data/lib/apartment.rb +22 -3
- metadata +11 -209
- 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/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/config.ru +0 -6
- 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/import.rb +0 -7
- data/spec/dummy/db/seeds.rb +0 -5
- data/spec/dummy/db/test.sqlite3 +0 -0
- 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/engine.rb +0 -6
- data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -5
- data/spec/dummy_engine/lib/dummy_engine.rb +0 -6
- data/spec/dummy_engine/test/dummy/Rakefile +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/dummy_engine/test/dummy/config.ru +0 -6
- 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
@@ -1,44 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
shared_examples_for 'a connection based apartment adapter' do
|
6
|
-
include Apartment::Spec::AdapterRequirements
|
7
|
-
|
8
|
-
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
9
|
-
|
10
|
-
describe '#init' do
|
11
|
-
after do
|
12
|
-
# Apartment::Tenant.init creates per model connection.
|
13
|
-
# Remove the connection after testing not to unintentionally keep the connection across tests.
|
14
|
-
Apartment.excluded_models.each do |excluded_model|
|
15
|
-
excluded_model.constantize.remove_connection
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should process model exclusions' do
|
20
|
-
Apartment.configure do |config|
|
21
|
-
config.excluded_models = ['Company']
|
22
|
-
end
|
23
|
-
Apartment::Tenant.init
|
24
|
-
|
25
|
-
expect(Company.connection.object_id).not_to eq(ActiveRecord::Base.connection.object_id)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
describe '#drop' do
|
30
|
-
it 'should raise an error for unknown database' do
|
31
|
-
expect do
|
32
|
-
subject.drop 'unknown_database'
|
33
|
-
end.to raise_error(Apartment::TenantNotFound)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe '#switch!' do
|
38
|
-
it 'should raise an error if database is invalid' do
|
39
|
-
expect do
|
40
|
-
subject.switch! 'unknown_database'
|
41
|
-
end.to raise_error(Apartment::TenantNotFound)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,93 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
shared_examples_for 'a generic apartment adapter able to handle custom configuration' do
|
6
|
-
let(:custom_tenant_name) { 'test_tenantwwww' }
|
7
|
-
let(:db) { |example| example.metadata[:database] }
|
8
|
-
let(:custom_tenant_names) do
|
9
|
-
{
|
10
|
-
custom_tenant_name => custom_db_conf
|
11
|
-
}
|
12
|
-
end
|
13
|
-
|
14
|
-
before do
|
15
|
-
Apartment.tenant_names = custom_tenant_names
|
16
|
-
Apartment.with_multi_server_setup = true
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
Apartment.with_multi_server_setup = false
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'database key taken from specific config' do
|
24
|
-
let(:expected_args) { custom_db_conf }
|
25
|
-
|
26
|
-
describe '#create' do
|
27
|
-
it 'should establish_connection with the separate connection with expected args' do
|
28
|
-
expect(Apartment::Adapters::AbstractAdapter::SeparateDbConnectionHandler).to receive(:establish_connection).with(expected_args).and_call_original
|
29
|
-
|
30
|
-
# because we dont have another server to connect to it errors
|
31
|
-
# what matters is establish_connection receives proper args
|
32
|
-
expect { subject.create(custom_tenant_name) }.to raise_error(Apartment::TenantExists)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
describe '#drop' do
|
37
|
-
it 'should establish_connection with the separate connection with expected args' do
|
38
|
-
expect(Apartment::Adapters::AbstractAdapter::SeparateDbConnectionHandler).to receive(:establish_connection).with(expected_args).and_call_original
|
39
|
-
|
40
|
-
# because we dont have another server to connect to it errors
|
41
|
-
# what matters is establish_connection receives proper args
|
42
|
-
expect { subject.drop(custom_tenant_name) }.to raise_error(Apartment::TenantNotFound)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context 'database key from tenant name' do
|
48
|
-
let(:expected_args) do
|
49
|
-
custom_db_conf.tap { |args| args.delete(:database) }
|
50
|
-
end
|
51
|
-
|
52
|
-
describe '#switch!' do
|
53
|
-
it 'should connect to new db' do
|
54
|
-
expect(Apartment).to receive(:establish_connection) do |args|
|
55
|
-
db_name = args.delete(:database)
|
56
|
-
|
57
|
-
expect(args).to eq expected_args
|
58
|
-
expect(db_name).to match custom_tenant_name
|
59
|
-
|
60
|
-
# we only need to check args, then we short circuit
|
61
|
-
# in order to avoid the mess due to the `establish_connection` override
|
62
|
-
raise ActiveRecord::ActiveRecordError
|
63
|
-
end
|
64
|
-
|
65
|
-
expect { subject.switch!(custom_tenant_name) }.to raise_error(Apartment::TenantNotFound)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def specific_connection
|
71
|
-
{
|
72
|
-
postgresql: {
|
73
|
-
adapter: 'postgresql',
|
74
|
-
database: 'override_database',
|
75
|
-
password: 'override_password',
|
76
|
-
username: 'overridepostgres'
|
77
|
-
},
|
78
|
-
mysql: {
|
79
|
-
adapter: 'mysql2',
|
80
|
-
database: 'override_database',
|
81
|
-
username: 'root'
|
82
|
-
},
|
83
|
-
sqlite: {
|
84
|
-
adapter: 'sqlite3',
|
85
|
-
database: 'override_database'
|
86
|
-
}
|
87
|
-
}
|
88
|
-
end
|
89
|
-
|
90
|
-
def custom_db_conf
|
91
|
-
specific_connection[db.to_sym].with_indifferent_access
|
92
|
-
end
|
93
|
-
end
|
@@ -1,164 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
shared_examples_for 'a generic apartment adapter' do
|
6
|
-
include Apartment::Spec::AdapterRequirements
|
7
|
-
|
8
|
-
before do
|
9
|
-
Apartment.prepend_environment = false
|
10
|
-
Apartment.append_environment = false
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#init' do
|
14
|
-
it 'should not retain a connection after railtie' do
|
15
|
-
# this test should work on rails >= 4, the connection pool code is
|
16
|
-
# completely different for 3.2 so we'd have to have a messy conditional..
|
17
|
-
unless Rails::VERSION::MAJOR < 4
|
18
|
-
ActiveRecord::Base.connection_pool.disconnect!
|
19
|
-
|
20
|
-
Apartment::Railtie.config.to_prepare_blocks.map(&:call)
|
21
|
-
|
22
|
-
num_available_connections = Apartment.connection_class.connection_pool
|
23
|
-
.instance_variable_get(:@available)
|
24
|
-
.instance_variable_get(:@queue)
|
25
|
-
.size
|
26
|
-
|
27
|
-
expect(num_available_connections).to eq(1)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
#
|
33
|
-
# Creates happen already in our before_filter
|
34
|
-
#
|
35
|
-
describe '#create' do
|
36
|
-
it 'should create the new databases' do
|
37
|
-
expect(tenant_names).to include(db1)
|
38
|
-
expect(tenant_names).to include(db2)
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should load schema.rb to new schema' do
|
42
|
-
subject.switch(db1) do
|
43
|
-
expect(connection.tables).to include('companies')
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should yield to block if passed and reset' do
|
48
|
-
subject.drop(db2) # so we don't get errors on creation
|
49
|
-
|
50
|
-
@count = 0 # set our variable so its visible in and outside of blocks
|
51
|
-
|
52
|
-
subject.create(db2) do
|
53
|
-
@count = User.count
|
54
|
-
expect(subject.current).to eq(db2)
|
55
|
-
User.create
|
56
|
-
end
|
57
|
-
|
58
|
-
expect(subject.current).not_to eq(db2)
|
59
|
-
|
60
|
-
subject.switch(db2) { expect(User.count).to eq(@count + 1) }
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'should raise error when the schema.rb is missing unless Apartment.use_sql is set to true' do
|
64
|
-
next if Apartment.use_sql
|
65
|
-
|
66
|
-
subject.drop(db1)
|
67
|
-
begin
|
68
|
-
Dir.mktmpdir do |tmpdir|
|
69
|
-
Apartment.database_schema_file = "#{tmpdir}/schema.rb"
|
70
|
-
expect do
|
71
|
-
subject.create(db1)
|
72
|
-
end.to raise_error(Apartment::FileNotFound)
|
73
|
-
end
|
74
|
-
ensure
|
75
|
-
Apartment.remove_instance_variable(:@database_schema_file)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe '#drop' do
|
81
|
-
it 'should remove the db' do
|
82
|
-
subject.drop db1
|
83
|
-
expect(tenant_names).not_to include(db1)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
describe '#switch!' do
|
88
|
-
it 'should connect to new db' do
|
89
|
-
subject.switch!(db1)
|
90
|
-
expect(subject.current).to eq(db1)
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'should reset connection if database is nil' do
|
94
|
-
subject.switch!
|
95
|
-
expect(subject.current).to eq(default_tenant)
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'should raise an error if database is invalid' do
|
99
|
-
expect do
|
100
|
-
subject.switch! 'unknown_database'
|
101
|
-
end.to raise_error(Apartment::ApartmentError)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe '#switch' do
|
106
|
-
it 'connects and resets the tenant' do
|
107
|
-
subject.switch(db1) do
|
108
|
-
expect(subject.current).to eq(db1)
|
109
|
-
end
|
110
|
-
expect(subject.current).to eq(default_tenant)
|
111
|
-
end
|
112
|
-
|
113
|
-
# We're often finding when using Apartment in tests, the `current` (ie the previously connect to db)
|
114
|
-
# gets dropped, but switch will try to return to that db in a test. We should just reset if it doesn't exist
|
115
|
-
it 'should not throw exception if current is no longer accessible' do
|
116
|
-
subject.switch!(db2)
|
117
|
-
|
118
|
-
expect do
|
119
|
-
subject.switch(db1) { subject.drop(db2) }
|
120
|
-
end.to_not raise_error
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
describe '#reset' do
|
125
|
-
it 'should reset connection' do
|
126
|
-
subject.switch!(db1)
|
127
|
-
subject.reset
|
128
|
-
expect(subject.current).to eq(default_tenant)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe '#current' do
|
133
|
-
it 'should return the current db name' do
|
134
|
-
subject.switch!(db1)
|
135
|
-
expect(subject.current).to eq(db1)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
describe '#each' do
|
140
|
-
it 'iterates over each tenant by default' do
|
141
|
-
result = []
|
142
|
-
Apartment.tenant_names = [db2, db1]
|
143
|
-
|
144
|
-
subject.each do |tenant|
|
145
|
-
result << tenant
|
146
|
-
expect(subject.current).to eq(tenant)
|
147
|
-
end
|
148
|
-
|
149
|
-
expect(result).to eq([db2, db1])
|
150
|
-
end
|
151
|
-
|
152
|
-
it 'iterates over the given tenants' do
|
153
|
-
result = []
|
154
|
-
Apartment.tenant_names = [db2]
|
155
|
-
|
156
|
-
subject.each([db2]) do |tenant|
|
157
|
-
result << tenant
|
158
|
-
expect(subject.current).to eq(tenant)
|
159
|
-
end
|
160
|
-
|
161
|
-
expect(result).to eq([db2])
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
@@ -1,239 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
# rubocop:disable Metrics/BlockLength
|
6
|
-
shared_examples_for 'a schema based apartment adapter' do
|
7
|
-
include Apartment::Spec::AdapterRequirements
|
8
|
-
|
9
|
-
let(:schema1) { db1 }
|
10
|
-
let(:schema2) { db2 }
|
11
|
-
let(:public_schema) { default_tenant }
|
12
|
-
|
13
|
-
describe '#init' do
|
14
|
-
before do
|
15
|
-
Apartment.configure do |config|
|
16
|
-
config.excluded_models = ['Company']
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
after do
|
21
|
-
# Apartment::Tenant.init creates per model connection.
|
22
|
-
# Remove the connection after testing not to unintentionally keep the connection across tests.
|
23
|
-
Apartment.excluded_models.each do |excluded_model|
|
24
|
-
excluded_model.constantize.remove_connection
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'should process model exclusions' do
|
29
|
-
Apartment::Tenant.init
|
30
|
-
|
31
|
-
expect(Company.table_name).to eq('public.companies')
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'with a default_schema', default_schema: true do
|
35
|
-
it 'should set the proper table_name on excluded_models' do
|
36
|
-
Apartment::Tenant.init
|
37
|
-
|
38
|
-
expect(Company.table_name).to eq("#{default_schema}.companies")
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'sets the search_path correctly' do
|
42
|
-
Apartment::Tenant.init
|
43
|
-
|
44
|
-
expect(User.connection.schema_search_path).to match(/|#{default_schema}|/)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context 'persistent_schemas', persistent_schemas: true do
|
49
|
-
it 'sets the persistent schemas in the schema_search_path' do
|
50
|
-
Apartment::Tenant.init
|
51
|
-
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %("#{schema}") }.join(', ')
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
#
|
57
|
-
# Creates happen already in our before_filter
|
58
|
-
#
|
59
|
-
describe '#create' do
|
60
|
-
it 'should load schema.rb to new schema' do
|
61
|
-
connection.schema_search_path = schema1
|
62
|
-
expect(connection.tables).to include('companies')
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should yield to block if passed and reset' do
|
66
|
-
subject.drop(schema2) # so we don't get errors on creation
|
67
|
-
|
68
|
-
@count = 0 # set our variable so its visible in and outside of blocks
|
69
|
-
|
70
|
-
subject.create(schema2) do
|
71
|
-
@count = User.count
|
72
|
-
expect(connection.schema_search_path).to start_with %("#{schema2}")
|
73
|
-
User.create
|
74
|
-
end
|
75
|
-
|
76
|
-
expect(connection.schema_search_path).not_to start_with %("#{schema2}")
|
77
|
-
|
78
|
-
subject.switch(schema2) { expect(User.count).to eq(@count + 1) }
|
79
|
-
end
|
80
|
-
|
81
|
-
context 'numeric database names' do
|
82
|
-
let(:db) { 1234 }
|
83
|
-
it 'should allow them' do
|
84
|
-
expect do
|
85
|
-
subject.create(db)
|
86
|
-
end.to_not raise_error
|
87
|
-
expect(tenant_names).to include(db.to_s)
|
88
|
-
end
|
89
|
-
|
90
|
-
after { subject.drop(db) }
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
describe '#drop' do
|
95
|
-
it 'should raise an error for unknown database' do
|
96
|
-
expect do
|
97
|
-
subject.drop 'unknown_database'
|
98
|
-
end.to raise_error(Apartment::TenantNotFound)
|
99
|
-
end
|
100
|
-
|
101
|
-
context 'numeric database names' do
|
102
|
-
let(:db) { 1234 }
|
103
|
-
|
104
|
-
it 'should be able to drop them' do
|
105
|
-
subject.create(db)
|
106
|
-
expect do
|
107
|
-
subject.drop(db)
|
108
|
-
end.to_not raise_error
|
109
|
-
expect(tenant_names).not_to include(db.to_s)
|
110
|
-
end
|
111
|
-
|
112
|
-
after do
|
113
|
-
begin
|
114
|
-
subject.drop(db)
|
115
|
-
rescue StandardError => _e
|
116
|
-
nil
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
describe '#switch' do
|
123
|
-
it 'connects and resets' do
|
124
|
-
subject.switch(schema1) do
|
125
|
-
expect(connection.schema_search_path).to start_with %("#{schema1}")
|
126
|
-
end
|
127
|
-
|
128
|
-
expect(connection.schema_search_path).to start_with %("#{public_schema}")
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe '#reset' do
|
133
|
-
it 'should reset connection' do
|
134
|
-
subject.switch!(schema1)
|
135
|
-
subject.reset
|
136
|
-
expect(connection.schema_search_path).to start_with %("#{public_schema}")
|
137
|
-
end
|
138
|
-
|
139
|
-
context 'with default_schema', default_schema: true do
|
140
|
-
it 'should reset to the default schema' do
|
141
|
-
subject.switch!(schema1)
|
142
|
-
subject.reset
|
143
|
-
expect(connection.schema_search_path).to start_with %("#{default_schema}")
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
context 'persistent_schemas', persistent_schemas: true do
|
148
|
-
before do
|
149
|
-
subject.switch!(schema1)
|
150
|
-
subject.reset
|
151
|
-
end
|
152
|
-
|
153
|
-
it 'maintains the persistent schemas in the schema_search_path' do
|
154
|
-
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %("#{schema}") }.join(', ')
|
155
|
-
end
|
156
|
-
|
157
|
-
context 'with default_schema', default_schema: true do
|
158
|
-
it 'prioritizes the switched schema to front of schema_search_path' do
|
159
|
-
subject.reset # need to re-call this as the default_schema wasn't set at the time that the above reset ran
|
160
|
-
expect(connection.schema_search_path).to start_with %("#{default_schema}")
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
describe '#switch!' do
|
167
|
-
it 'should connect to new schema' do
|
168
|
-
subject.switch!(schema1)
|
169
|
-
expect(connection.schema_search_path).to start_with %("#{schema1}")
|
170
|
-
end
|
171
|
-
|
172
|
-
it 'should reset connection if database is nil' do
|
173
|
-
subject.switch!
|
174
|
-
expect(connection.schema_search_path).to eq(%("#{public_schema}"))
|
175
|
-
end
|
176
|
-
|
177
|
-
it 'should raise an error if schema is invalid' do
|
178
|
-
expect do
|
179
|
-
subject.switch! 'unknown_schema'
|
180
|
-
end.to raise_error(Apartment::TenantNotFound)
|
181
|
-
end
|
182
|
-
|
183
|
-
context 'numeric databases' do
|
184
|
-
let(:db) { 1234 }
|
185
|
-
|
186
|
-
it 'should connect to them' do
|
187
|
-
subject.create(db)
|
188
|
-
expect do
|
189
|
-
subject.switch!(db)
|
190
|
-
end.to_not raise_error
|
191
|
-
|
192
|
-
expect(connection.schema_search_path).to start_with %("#{db}")
|
193
|
-
end
|
194
|
-
|
195
|
-
after { subject.drop(db) }
|
196
|
-
end
|
197
|
-
|
198
|
-
describe 'with default_schema specified', default_schema: true do
|
199
|
-
before do
|
200
|
-
subject.switch!(schema1)
|
201
|
-
end
|
202
|
-
|
203
|
-
it 'should switch out the default schema rather than public' do
|
204
|
-
expect(connection.schema_search_path).not_to include default_schema
|
205
|
-
end
|
206
|
-
|
207
|
-
it 'should still switch to the switched schema' do
|
208
|
-
expect(connection.schema_search_path).to start_with %("#{schema1}")
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
context 'persistent_schemas', persistent_schemas: true do
|
213
|
-
before { subject.switch!(schema1) }
|
214
|
-
|
215
|
-
it 'maintains the persistent schemas in the schema_search_path' do
|
216
|
-
expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %("#{schema}") }.join(', ')
|
217
|
-
end
|
218
|
-
|
219
|
-
it 'prioritizes the switched schema to front of schema_search_path' do
|
220
|
-
expect(connection.schema_search_path).to start_with %("#{schema1}")
|
221
|
-
end
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
describe '#current' do
|
226
|
-
it 'should return the current schema name' do
|
227
|
-
subject.switch!(schema1)
|
228
|
-
expect(subject.current).to eq(schema1)
|
229
|
-
end
|
230
|
-
|
231
|
-
context 'persistent_schemas', persistent_schemas: true do
|
232
|
-
it 'should exlude persistent_schemas' do
|
233
|
-
subject.switch!(schema1)
|
234
|
-
expect(subject.current).to eq(schema1)
|
235
|
-
end
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
# rubocop:enable Metrics/BlockLength
|
@@ -1,107 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
require 'rake'
|
5
|
-
|
6
|
-
describe 'apartment rake tasks', database: :postgresql do
|
7
|
-
before do
|
8
|
-
@rake = Rake::Application.new
|
9
|
-
Rake.application = @rake
|
10
|
-
Dummy::Application.load_tasks
|
11
|
-
|
12
|
-
# rails tasks running F up the schema...
|
13
|
-
Rake::Task.define_task('db:migrate')
|
14
|
-
Rake::Task.define_task('db:seed')
|
15
|
-
Rake::Task.define_task('db:rollback')
|
16
|
-
Rake::Task.define_task('db:migrate:up')
|
17
|
-
Rake::Task.define_task('db:migrate:down')
|
18
|
-
Rake::Task.define_task('db:migrate:redo')
|
19
|
-
|
20
|
-
Apartment.configure do |config|
|
21
|
-
config.use_schemas = true
|
22
|
-
config.excluded_models = ['Company']
|
23
|
-
config.tenant_names = -> { Company.pluck(:database) }
|
24
|
-
end
|
25
|
-
Apartment::Tenant.reload!(config)
|
26
|
-
|
27
|
-
# fix up table name of shared/excluded models
|
28
|
-
Company.table_name = 'public.companies'
|
29
|
-
end
|
30
|
-
|
31
|
-
after { Rake.application = nil }
|
32
|
-
|
33
|
-
context 'with x number of databases' do
|
34
|
-
let(:x) { rand(1..5) } # random number of dbs to create
|
35
|
-
let(:db_names) { x.times.map { Apartment::Test.next_db } }
|
36
|
-
let!(:company_count) { db_names.length }
|
37
|
-
|
38
|
-
before do
|
39
|
-
db_names.collect do |db_name|
|
40
|
-
Apartment::Tenant.create(db_name)
|
41
|
-
Company.create database: db_name
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
after do
|
46
|
-
db_names.each { |db| Apartment::Tenant.drop(db) }
|
47
|
-
Company.delete_all
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with ActiveRecord below 5.2.0' do
|
51
|
-
before do
|
52
|
-
allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w[spec/dummy/db/migrate] }
|
53
|
-
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true }
|
54
|
-
end
|
55
|
-
|
56
|
-
describe '#migrate' do
|
57
|
-
it 'should migrate all databases' do
|
58
|
-
expect(ActiveRecord::Migrator).to receive(:migrate).exactly(company_count).times
|
59
|
-
|
60
|
-
@rake['apartment:migrate'].invoke
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe '#rollback' do
|
65
|
-
it 'should rollback all dbs' do
|
66
|
-
expect(ActiveRecord::Migrator).to receive(:rollback).exactly(company_count).times
|
67
|
-
|
68
|
-
@rake['apartment:rollback'].invoke
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
context 'with ActiveRecord above or equal to 5.2.0' do
|
74
|
-
let(:migration_context_double) { double(:migration_context) }
|
75
|
-
|
76
|
-
before do
|
77
|
-
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
|
78
|
-
end
|
79
|
-
|
80
|
-
describe '#migrate' do
|
81
|
-
it 'should migrate all databases' do
|
82
|
-
allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double }
|
83
|
-
expect(migration_context_double).to receive(:migrate).exactly(company_count).times
|
84
|
-
|
85
|
-
@rake['apartment:migrate'].invoke
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
describe '#rollback' do
|
90
|
-
it 'should rollback all dbs' do
|
91
|
-
allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double }
|
92
|
-
expect(migration_context_double).to receive(:rollback).exactly(company_count).times
|
93
|
-
|
94
|
-
@rake['apartment:rollback'].invoke
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
describe 'apartment:seed' do
|
100
|
-
it 'should seed all databases' do
|
101
|
-
expect(Apartment::Tenant).to receive(:seed).exactly(company_count).times
|
102
|
-
|
103
|
-
@rake['apartment:seed'].invoke
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|