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.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/.rubocop-linter.yml +22 -0
  3. data/.pryrc +5 -3
  4. data/.rubocop.yml +17 -0
  5. data/.rubocop_todo.yml +29 -0
  6. data/.story_branch.yml +4 -0
  7. data/.travis.yml +12 -11
  8. data/Appraisals +18 -18
  9. data/Gemfile +4 -1
  10. data/README.md +33 -5
  11. data/Rakefile +2 -0
  12. data/apartment.gemspec +6 -6
  13. data/gemfiles/rails_4_2.gemfile +12 -10
  14. data/gemfiles/rails_5_0.gemfile +11 -9
  15. data/gemfiles/rails_5_1.gemfile +11 -9
  16. data/gemfiles/rails_5_2.gemfile +10 -8
  17. data/gemfiles/rails_6_0.gemfile +11 -9
  18. data/gemfiles/rails_master.gemfile +11 -9
  19. data/lib/apartment/adapters/abstract_adapter.rb +34 -34
  20. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +4 -3
  21. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
  22. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +13 -11
  23. data/lib/apartment/adapters/mysql2_adapter.rb +10 -9
  24. data/lib/apartment/adapters/postgis_adapter.rb +3 -2
  25. data/lib/apartment/adapters/postgresql_adapter.rb +28 -25
  26. data/lib/apartment/adapters/sqlite3_adapter.rb +16 -8
  27. data/lib/apartment/console.rb +28 -3
  28. data/lib/apartment/custom_console.rb +26 -0
  29. data/lib/apartment/deprecation.rb +2 -1
  30. data/lib/apartment/elevators/domain.rb +4 -3
  31. data/lib/apartment/elevators/first_subdomain.rb +3 -2
  32. data/lib/apartment/elevators/generic.rb +4 -3
  33. data/lib/apartment/elevators/host.rb +6 -1
  34. data/lib/apartment/elevators/host_hash.rb +6 -2
  35. data/lib/apartment/elevators/subdomain.rb +9 -5
  36. data/lib/apartment/migrator.rb +4 -3
  37. data/lib/apartment/railtie.rb +13 -5
  38. data/lib/apartment/reloader.rb +2 -1
  39. data/lib/apartment/tasks/enhancements.rb +4 -6
  40. data/lib/apartment/tenant.rb +3 -4
  41. data/lib/apartment/version.rb +3 -1
  42. data/lib/apartment.rb +15 -9
  43. data/lib/generators/apartment/install/install_generator.rb +4 -3
  44. data/lib/generators/apartment/install/templates/apartment.rb +3 -2
  45. data/lib/tasks/apartment.rake +19 -18
  46. data/spec/adapters/jdbc_mysql_adapter_spec.rb +5 -4
  47. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +10 -12
  48. data/spec/adapters/mysql2_adapter_spec.rb +15 -13
  49. data/spec/adapters/postgresql_adapter_spec.rb +22 -20
  50. data/spec/adapters/sqlite3_adapter_spec.rb +41 -23
  51. data/spec/apartment_spec.rb +4 -2
  52. data/spec/dummy/app/controllers/application_controller.rb +4 -3
  53. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  54. data/spec/dummy/app/models/application_record.rb +6 -0
  55. data/spec/dummy/app/models/company.rb +4 -2
  56. data/spec/dummy/app/models/user.rb +4 -2
  57. data/spec/dummy/config/application.rb +11 -9
  58. data/spec/dummy/config/boot.rb +4 -2
  59. data/spec/dummy/config/environment.rb +3 -1
  60. data/spec/dummy/config/environments/development.rb +2 -1
  61. data/spec/dummy/config/environments/production.rb +3 -1
  62. data/spec/dummy/config/environments/test.rb +3 -1
  63. data/spec/dummy/config/initializers/apartment.rb +4 -2
  64. data/spec/dummy/config/initializers/backtrace_silencers.rb +1 -0
  65. data/spec/dummy/config/initializers/inflections.rb +1 -0
  66. data/spec/dummy/config/initializers/mime_types.rb +1 -0
  67. data/spec/dummy/config/initializers/secret_token.rb +2 -0
  68. data/spec/dummy/config/initializers/session_store.rb +3 -1
  69. data/spec/dummy/config/routes.rb +3 -1
  70. data/spec/dummy/config.ru +3 -1
  71. data/spec/dummy/db/seeds/import.rb +3 -1
  72. data/spec/dummy/script/rails +4 -2
  73. data/spec/dummy_engine/config/initializers/apartment.rb +4 -3
  74. data/spec/dummy_engine/lib/dummy_engine/engine.rb +2 -0
  75. data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -1
  76. data/spec/dummy_engine/lib/dummy_engine.rb +3 -1
  77. data/spec/dummy_engine/test/dummy/config/application.rb +4 -2
  78. data/spec/dummy_engine/test/dummy/config/boot.rb +4 -2
  79. data/spec/dummy_engine/test/dummy/config/environment.rb +3 -1
  80. data/spec/dummy_engine/test/dummy/config/environments/development.rb +2 -0
  81. data/spec/dummy_engine/test/dummy/config/environments/production.rb +2 -0
  82. data/spec/dummy_engine/test/dummy/config/environments/test.rb +2 -0
  83. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +2 -0
  84. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +1 -0
  85. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -1
  86. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  87. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +1 -0
  88. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +1 -0
  89. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +2 -0
  90. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +2 -0
  91. data/spec/dummy_engine/test/dummy/config/routes.rb +2 -0
  92. data/spec/dummy_engine/test/dummy/config.ru +3 -1
  93. data/spec/examples/connection_adapter_examples.rb +15 -13
  94. data/spec/examples/generic_adapter_custom_configuration_example.rb +21 -23
  95. data/spec/examples/generic_adapter_examples.rb +40 -39
  96. data/spec/examples/schema_adapter_examples.rb +93 -88
  97. data/spec/integration/apartment_rake_integration_spec.rb +24 -24
  98. data/spec/integration/query_caching_spec.rb +10 -8
  99. data/spec/integration/use_within_an_engine_spec.rb +5 -5
  100. data/spec/schemas/v1.rb +2 -3
  101. data/spec/schemas/v2.rb +22 -24
  102. data/spec/schemas/v3.rb +26 -28
  103. data/spec/spec_helper.rb +8 -6
  104. data/spec/support/apartment_helpers.rb +9 -5
  105. data/spec/support/capybara_sessions.rb +5 -5
  106. data/spec/support/config.rb +5 -2
  107. data/spec/support/contexts.rb +10 -8
  108. data/spec/support/requirements.rb +19 -6
  109. data/spec/support/setup.rb +5 -5
  110. data/spec/tasks/apartment_rake_spec.rb +30 -35
  111. data/spec/tenant_spec.rb +48 -44
  112. data/spec/unit/config_spec.rb +26 -27
  113. data/spec/unit/elevators/domain_spec.rb +9 -8
  114. data/spec/unit/elevators/first_subdomain_spec.rb +14 -12
  115. data/spec/unit/elevators/generic_spec.rb +16 -15
  116. data/spec/unit/elevators/host_hash_spec.rb +9 -8
  117. data/spec/unit/elevators/host_spec.rb +26 -26
  118. data/spec/unit/elevators/subdomain_spec.rb +24 -23
  119. data/spec/unit/migrator_spec.rb +18 -17
  120. data/spec/unit/reloader_spec.rb +8 -8
  121. metadata +40 -22
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is used by Rack-based servers to start the application.
2
4
 
3
- require ::File.expand_path('../config/environment', __FILE__)
5
+ require ::File.expand_path('../config/environment', __FILE__)
4
6
  run Rails.application
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- shared_examples_for "a connection based apartment adapter" do
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 "#init" do
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 "should process model exclusions" do
19
+ it 'should process model exclusions' do
18
20
  Apartment.configure do |config|
19
- config.excluded_models = ["Company"]
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 "#drop" do
28
- it "should raise an error for unknown database" do
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
- }.to raise_error(Apartment::TenantNotFound)
33
+ end.to raise_error(Apartment::TenantNotFound)
32
34
  end
33
35
  end
34
36
 
35
- describe "#switch!" do
36
- it "should raise an error if database is invalid" do
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
- }.to raise_error(Apartment::TenantNotFound)
41
+ end.to raise_error(Apartment::TenantNotFound)
40
42
  end
41
43
  end
42
44
  end
@@ -1,12 +1,13 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- shared_examples_for "a generic apartment adapter able to handle custom configuration" do
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 => get_custom_db_conf
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 "database key taken from specific config" do
23
+ context 'database key taken from specific config' do
24
+ let(:expected_args) { custom_db_conf }
23
25
 
24
- let(:expected_args) { get_custom_db_conf }
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 "#drop" do
37
- it "should establish_connection with the separate connection with expected args" do
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 "database key from tenant name" do
48
-
49
- let(:expected_args) {
50
- get_custom_db_conf.tap {|args| args.delete(:database) }
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
- it "should connect to new db" do
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: 'postgresql',
73
+ adapter: 'postgresql',
76
74
  database: 'override_database',
77
75
  password: 'override_password',
78
76
  username: 'overridepostgres'
79
77
  },
80
78
  mysql: {
81
- adapter: 'mysql2',
79
+ adapter: 'mysql2',
82
80
  database: 'override_database',
83
81
  username: 'root'
84
82
  },
85
83
  sqlite: {
86
- adapter: 'sqlite3',
84
+ adapter: 'sqlite3',
87
85
  database: 'override_database'
88
86
  }
89
87
  }
90
88
  end
91
89
 
92
- def get_custom_db_conf
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 "a generic apartment adapter" do
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 "#init" do
12
- it "should not retain a connection after railtie" do
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
- .instance_variable_get(:@available)
22
- .instance_variable_get(:@queue)
23
- .size
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 "#create" do
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 "should load schema.rb to new schema" do
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 "should yield to block if passed and reset" do
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 # set our variable so its visible in and outside of blocks
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 "should raise error when the schema.rb is missing unless Apartment.use_sql is set to true" do
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
- }.to raise_error(Apartment::FileNotFound)
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 "#drop" do
80
- it "should remove the db" do
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 "#switch!" do
87
- it "should connect to new db" do
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 "should reset connection if database is nil" do
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 "should raise an error if database is invalid" do
98
- expect {
98
+ it 'should raise an error if database is invalid' do
99
+ expect do
99
100
  subject.switch! 'unknown_database'
100
- }.to raise_error(Apartment::ApartmentError)
101
+ end.to raise_error(Apartment::ApartmentError)
101
102
  end
102
103
  end
103
104
 
104
- describe "#switch" do
105
- it "connects and resets the tenant" do
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 "should not throw exception if current is no longer accessible" do
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
- }.to_not raise_error
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 "#reset" do
124
- it "should reset connection" do
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 "#current" do
132
- it "should return the current db name" do
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 "#each" do
139
- it "iterates over each tenant by default" do
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 "iterates over the given tenants" do
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
- shared_examples_for "a schema based apartment adapter" do
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 = ["Company"]
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 "should process model exclusions" do
28
+ it 'should process model exclusions' do
27
29
  Apartment::Tenant.init
28
30
 
29
- expect(Company.table_name).to eq("public.companies")
31
+ expect(Company.table_name).to eq('public.companies')
30
32
  end
31
33
 
32
- context "with a default_schema", :default_schema => true do
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(%r|#{default_schema}|)
44
+ expect(User.connection.schema_search_path).to match(/|#{default_schema}|/)
44
45
  end
45
46
  end
46
47
 
47
- context "persistent_schemas", :persistent_schemas => true do
48
- it "sets the persistent schemas in the schema_search_path" do
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| %{"#{schema}"} }.join(', ')
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 "#create" do
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 "should yield to block if passed and reset" do
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 # set our variable so its visible in and outside of blocks
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 %{"#{schema2}"}
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 %{"#{schema2}"}
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 "numeric database names" do
82
- let(:db){ 1234 }
83
- it "should allow them" do
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
- }.to_not raise_error
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 "#drop" do
96
- it "should raise an error for unknown database" do
97
- expect {
98
- subject.drop "unknown_database"
99
- }.to raise_error(Apartment::TenantNotFound)
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 "numeric database names" do
103
- let(:db){ 1234 }
101
+ context 'numeric database names' do
102
+ let(:db) { 1234 }
104
103
 
105
- it "should be able to drop them" do
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
- }.to_not raise_error
108
+ end.to_not raise_error
110
109
  expect(tenant_names).not_to include(db.to_s)
111
110
  end
112
111
 
113
- after { subject.drop(db) rescue nil }
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 "#switch" do
118
- it "connects and resets" do
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 %{"#{schema1}"}
125
+ expect(connection.schema_search_path).to start_with %("#{schema1}")
121
126
  end
122
127
 
123
- expect(connection.schema_search_path).to start_with %{"#{public_schema}"}
128
+ expect(connection.schema_search_path).to start_with %("#{public_schema}")
124
129
  end
125
130
  end
126
131
 
127
- describe "#reset" do
128
- it "should reset connection" do
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 %{"#{public_schema}"}
136
+ expect(connection.schema_search_path).to start_with %("#{public_schema}")
132
137
  end
133
138
 
134
- context "with default_schema", :default_schema => true do
135
- it "should reset to the default schema" do
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 %{"#{default_schema}"}
143
+ expect(connection.schema_search_path).to start_with %("#{default_schema}")
139
144
  end
140
145
  end
141
146
 
142
- context "persistent_schemas", :persistent_schemas => true do
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 "maintains the persistent schemas in the schema_search_path" do
149
- expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
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 "with default_schema", :default_schema => true do
153
- it "prioritizes the switched schema to front of schema_search_path" do
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 %{"#{default_schema}"}
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 "#switch!" do
162
- it "should connect to new schema" do
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 %{"#{schema1}"}
169
+ expect(connection.schema_search_path).to start_with %("#{schema1}")
165
170
  end
166
171
 
167
- it "should reset connection if database is nil" do
172
+ it 'should reset connection if database is nil' do
168
173
  subject.switch!
169
- expect(connection.schema_search_path).to eq(%{"#{public_schema}"})
174
+ expect(connection.schema_search_path).to eq(%("#{public_schema}"))
170
175
  end
171
176
 
172
- it "should raise an error if schema is invalid" do
173
- expect {
177
+ it 'should raise an error if schema is invalid' do
178
+ expect do
174
179
  subject.switch! 'unknown_schema'
175
- }.to raise_error(Apartment::TenantNotFound)
180
+ end.to raise_error(Apartment::TenantNotFound)
176
181
  end
177
182
 
178
- context "numeric databases" do
179
- let(:db){ 1234 }
183
+ context 'numeric databases' do
184
+ let(:db) { 1234 }
180
185
 
181
- it "should connect to them" do
186
+ it 'should connect to them' do
182
187
  subject.create(db)
183
- expect {
188
+ expect do
184
189
  subject.switch!(db)
185
- }.to_not raise_error
190
+ end.to_not raise_error
186
191
 
187
- expect(connection.schema_search_path).to start_with %{"#{db.to_s}"}
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 "with default_schema specified", :default_schema => true do
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 "should switch out the default schema rather than public" do
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 "should still switch to the switched schema" do
203
- expect(connection.schema_search_path).to start_with %{"#{schema1}"}
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 "persistent_schemas", :persistent_schemas => true do
208
-
209
- before{ subject.switch!(schema1) }
212
+ context 'persistent_schemas', persistent_schemas: true do
213
+ before { subject.switch!(schema1) }
210
214
 
211
- it "maintains the persistent schemas in the schema_search_path" do
212
- expect(connection.schema_search_path).to end_with persistent_schemas.map { |schema| %{"#{schema}"} }.join(', ')
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 "prioritizes the switched schema to front of schema_search_path" do
216
- expect(connection.schema_search_path).to start_with %{"#{schema1}"}
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 "#current" do
222
- it "should return the current schema name" do
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 "persistent_schemas", :persistent_schemas => true do
228
- it "should exlude persistent_schemas" do
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