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