Skip to content

Commit fefed66

Browse files
authored
Merge pull request #188 from rails-on-services/development
**Implemented enhancements:** - Increase errors visibility by showing more information on the underlying error rather than a generic error 'Apartment::TenantNotFound' (#176) - Resolved #177 - Added rails 7 support (#178) **Fixed bugs:** - Fixing tenant_presence_check config in the README (#180) - Resolved #161 and #81 - Fixed sequence name (#187) **Closed issues:** - Resolved #151 - removed reloader and console overwrite of reload method (#174)
2 parents cf60dd2 + 2d034f0 commit fefed66

27 files changed

+111
-243
lines changed

.circleci/config.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ workflows:
7070
matrix:
7171
parameters:
7272
ruby_version: ["ruby:2.6-buster", "ruby:2.7-buster", "ruby:3.0-buster"]
73-
gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile", "gemfiles/rails_6_1.gemfile"]
73+
gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile", "gemfiles/rails_6_1.gemfile", "gemfiles/rails_7_0.gemfile"]
7474
exclude:
7575
- ruby_version: "ruby:3.0-buster"
7676
gemfile: "gemfiles/rails_5_2.gemfile"
77+
- ruby_version: "ruby:2.6-buster"
78+
gemfile: "gemfiles/rails_7_0.gemfile"

.rubocop_todo.yml

-6
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ RSpec/DescribedClass:
7777
- 'spec/tenant_spec.rb'
7878
- 'spec/unit/elevators/host_hash_spec.rb'
7979
- 'spec/unit/migrator_spec.rb'
80-
- 'spec/unit/reloader_spec.rb'
8180

8281
# Offense count: 5
8382
# Cop supports --auto-correct.
@@ -128,7 +127,6 @@ RSpec/FilePath:
128127
- 'spec/unit/elevators/host_spec.rb'
129128
- 'spec/unit/elevators/subdomain_spec.rb'
130129
- 'spec/unit/migrator_spec.rb'
131-
- 'spec/unit/reloader_spec.rb'
132130

133131
# Offense count: 1
134132
# Cop supports --auto-correct.
@@ -159,7 +157,6 @@ RSpec/InstanceVariable:
159157
# Cop supports --auto-correct.
160158
RSpec/LeadingSubject:
161159
Exclude:
162-
- 'spec/unit/reloader_spec.rb'
163160

164161
# Offense count: 2
165162
RSpec/LeakyConstantDeclaration:
@@ -196,7 +193,6 @@ RSpec/NamedSubject:
196193
- 'spec/support/contexts.rb'
197194
- 'spec/support/requirements.rb'
198195
- 'spec/tenant_spec.rb'
199-
- 'spec/unit/reloader_spec.rb'
200196

201197
# Offense count: 24
202198
RSpec/NestedGroups:
@@ -217,7 +213,6 @@ RSpec/VerifiedDoubles:
217213
Exclude:
218214
- 'spec/integration/apartment_rake_integration_spec.rb'
219215
- 'spec/unit/elevators/first_subdomain_spec.rb'
220-
- 'spec/unit/reloader_spec.rb'
221216

222217
# Offense count: 17
223218
Style/Documentation:
@@ -233,7 +228,6 @@ Style/Documentation:
233228
- 'lib/apartment/migrator.rb'
234229
- 'lib/apartment/model.rb'
235230
- 'lib/apartment/railtie.rb'
236-
- 'lib/apartment/reloader.rb'
237231
- 'lib/apartment/tasks/enhancements.rb'
238232
- 'lib/apartment/tasks/task_helper.rb'
239233
- 'lib/generators/apartment/install/install_generator.rb'

.ruby-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.7.3
1+
2.7.5

Appraisals

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
# frozen_string_literal: true
22

3-
appraise 'rails-5-0' do
4-
gem 'rails', '~> 5.0.0'
5-
platforms :ruby do
6-
gem 'pg', '< 1.0.0'
7-
end
8-
platforms :jruby do
9-
gem 'activerecord-jdbc-adapter', '~> 50.0'
10-
gem 'activerecord-jdbcpostgresql-adapter', '~> 50.0'
11-
gem 'activerecord-jdbcmysql-adapter', '~> 50.0'
12-
end
13-
end
14-
15-
appraise 'rails-5-1' do
16-
gem 'rails', '~> 5.1.0'
17-
platforms :ruby do
18-
gem 'pg', '< 1.0.0'
19-
end
20-
platforms :jruby do
21-
gem 'activerecord-jdbc-adapter', '~> 51.0'
22-
gem 'activerecord-jdbcpostgresql-adapter', '~> 51.0'
23-
gem 'activerecord-jdbcmysql-adapter', '~> 51.0'
24-
end
25-
end
26-
273
appraise 'rails-5-2' do
284
gem 'rails', '~> 5.2.0'
295
platforms :jruby do
@@ -57,6 +33,18 @@ appraise 'rails-6-1' do
5733
end
5834
end
5935

36+
appraise 'rails-7-0' do
37+
gem 'rails', '~> 7.0.0'
38+
platforms :ruby do
39+
gem 'sqlite3', '~> 1.4'
40+
end
41+
platforms :jruby do
42+
gem 'activerecord-jdbc-adapter', '~> 61.0'
43+
gem 'activerecord-jdbcpostgresql-adapter', '~> 61.0'
44+
gem 'activerecord-jdbcmysql-adapter', '~> 61.0'
45+
end
46+
end
47+
6048
appraise 'rails-master' do
6149
gem 'rails', git: 'https://github.com/rails/rails.git'
6250
platforms :ruby do

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ Setting this configuration value to `false` will disable the schema presence che
344344

345345
```ruby
346346
Apartment.configure do |config|
347-
tenant_presence_check = false
347+
config.tenant_presence_check = false
348348
end
349349
```
350350

gemfiles/rails_4_2.gemfile

-25
This file was deleted.

gemfiles/rails_5_0.gemfile

-23
This file was deleted.

gemfiles/rails_5_1.gemfile

-23
This file was deleted.

gemfiles/rails_5_2.gemfile

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
source "http://rubygems.org"
44

55
gem "rails", "~> 5.2.0"
6-
gem "rubocop"
7-
8-
group :local do
9-
gem "guard-rspec", "~> 4.2"
10-
gem "pry"
11-
end
126

137
platforms :jruby do
148
gem "activerecord-jdbc-adapter", "~> 52.0"

gemfiles/rails_6_0.gemfile

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
source "http://rubygems.org"
44

55
gem "rails", "~> 6.0.0"
6-
gem "rubocop"
7-
8-
group :local do
9-
gem "guard-rspec", "~> 4.2"
10-
gem "pry"
11-
end
126

137
platforms :ruby do
148
gem "sqlite3", "~> 1.4"

gemfiles/rails_6_1.gemfile

-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
source "http://rubygems.org"
44

55
gem "rails", "~> 6.1.0"
6-
gem "rubocop"
7-
8-
group :local do
9-
gem "guard-rspec", "~> 4.2"
10-
gem "pry"
11-
end
126

137
platforms :ruby do
148
gem "sqlite3", "~> 1.4"

gemfiles/rails_7_0.gemfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file was generated by Appraisal
2+
3+
source "http://rubygems.org"
4+
5+
gem "rails", "~> 7.0.0"
6+
7+
platforms :ruby do
8+
gem "sqlite3", "~> 1.4"
9+
end
10+
11+
platforms :jruby do
12+
gem "activerecord-jdbc-adapter", "~> 61.0"
13+
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0"
14+
gem "activerecord-jdbcmysql-adapter", "~> 61.0"
15+
end
16+
17+
gemspec path: "../"

gemfiles/rails_master.gemfile

+3-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
source "http://rubygems.org"
44

55
gem "rails", git: "https://github.com/rails/rails.git"
6-
gem "rubocop"
7-
8-
group :local do
9-
gem "guard-rspec", "~> 4.2"
10-
gem "pry"
11-
end
126

137
platforms :ruby do
148
gem "sqlite3", "~> 1.4"
159
end
1610

1711
platforms :jruby do
18-
gem "activerecord-jdbc-adapter", "~> 52.0"
19-
gem "activerecord-jdbcpostgresql-adapter", "~> 52.0"
20-
gem "activerecord-jdbcmysql-adapter", "~> 52.0"
12+
gem "activerecord-jdbc-adapter", "~> 61.0"
13+
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0"
14+
gem "activerecord-jdbcmysql-adapter", "~> 61.0"
2115
end
2216

2317
gemspec path: "../"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# rubocop:disable Style/ClassAndModuleChildren
4+
5+
# NOTE: This patch is meant to remove any schema_prefix appart from the ones for
6+
# excluded models. The schema_prefix would be resolved by apartment's setting
7+
# of search path
8+
module Apartment::PostgreSqlAdapterPatch
9+
def default_sequence_name(table, _column)
10+
res = super
11+
schema_prefix = "#{Apartment::Tenant.current}."
12+
default_tenant_prefix = "#{Apartment::Tenant.default_tenant}."
13+
14+
# NOTE: Excluded models should always access the sequence from the default
15+
# tenant schema
16+
if excluded_model?(table)
17+
res.sub!(schema_prefix, default_tenant_prefix) if schema_prefix != default_tenant_prefix
18+
return res
19+
end
20+
21+
res.delete_prefix!(schema_prefix) if res&.starts_with?(schema_prefix)
22+
23+
res
24+
end
25+
26+
private
27+
28+
def excluded_model?(table)
29+
Apartment.excluded_models.any? { |m| m.constantize.table_name == table }
30+
end
31+
end
32+
33+
require 'active_record/connection_adapters/postgresql_adapter'
34+
35+
# NOTE: inject this into postgresql adapters
36+
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
37+
include Apartment::PostgreSqlAdapterPatch
38+
end
39+
# rubocop:enable Style/ClassAndModuleChildren

lib/apartment/adapters/postgresql_adapter.rb

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'apartment/adapters/abstract_adapter'
4+
require 'apartment/active_record/postgresql_adapter'
45

56
module Apartment
67
module Tenant
@@ -41,7 +42,6 @@ def default_tenant
4142
def reset
4243
@current = default_tenant
4344
Apartment.connection.schema_search_path = full_search_path
44-
reset_sequence_names
4545
end
4646

4747
def init
@@ -81,9 +81,8 @@ def connect_to_new(tenant = nil)
8181
# there is a issue for prepared statement with changing search_path.
8282
# https://www.postgresql.org/docs/9.3/static/sql-prepare.html
8383
Apartment.connection.clear_cache! if postgresql_version < 90_300
84-
reset_sequence_names
85-
rescue *rescuable_exceptions
86-
raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}"
84+
rescue *rescuable_exceptions => e
85+
raise_schema_connect_to_new(tenant, e)
8786
end
8887

8988
private
@@ -130,29 +129,18 @@ def postgresql_version
130129
Apartment.connection.send(:postgresql_version)
131130
end
132131

133-
def reset_sequence_names
134-
# sequence_name contains the schema, so it must be reset after switch
135-
# There is `reset_sequence_name`, but that method actually goes to the database
136-
# to find out the new name. Therefore, we do this hack to only unset the name,
137-
# and it will be dynamically found the next time it is needed
138-
descendants_to_unset = ActiveRecord::Base.descendants
139-
.select { |c| c.instance_variable_defined?(:@sequence_name) }
140-
.reject do |c|
141-
c.instance_variable_defined?(:@explicit_sequence_name) &&
142-
c.instance_variable_get(:@explicit_sequence_name)
143-
end
144-
descendants_to_unset.each do |c|
145-
# NOTE: due to this https://github.com/rails-on-services/apartment/issues/81
146-
# unreproduceable error we're checking before trying to remove it
147-
c.remove_instance_variable :@sequence_name if c.instance_variable_defined?(:@sequence_name)
148-
end
149-
end
150-
151132
def schema_exists?(schemas)
152133
return true unless Apartment.tenant_presence_check
153134

154135
Array(schemas).all? { |schema| Apartment.connection.schema_exists?(schema.to_s) }
155136
end
137+
138+
def raise_schema_connect_to_new(tenant, exception)
139+
raise TenantNotFound, <<~EXCEPTION_MESSAGE
140+
Could not set search path to schemas, they may be invalid: "#{tenant}" #{full_search_path}.
141+
Original error: #{exception.class}: #{exception}
142+
EXCEPTION_MESSAGE
143+
end
156144
end
157145

158146
# Another Adapter for Postgresql when using schemas and SQL

0 commit comments

Comments
 (0)