Skip to content

Commit

Permalink
Merge pull request #188 from rails-on-services/development
Browse files Browse the repository at this point in the history
**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)
  • Loading branch information
rpbaltazar authored Feb 7, 2022
2 parents cf60dd2 + 2d034f0 commit fefed66
Show file tree
Hide file tree
Showing 27 changed files with 111 additions and 243 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ workflows:
matrix:
parameters:
ruby_version: ["ruby:2.6-buster", "ruby:2.7-buster", "ruby:3.0-buster"]
gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile", "gemfiles/rails_6_1.gemfile"]
gemfile: ["gemfiles/rails_5_2.gemfile", "gemfiles/rails_6_0.gemfile", "gemfiles/rails_6_1.gemfile", "gemfiles/rails_7_0.gemfile"]
exclude:
- ruby_version: "ruby:3.0-buster"
gemfile: "gemfiles/rails_5_2.gemfile"
- ruby_version: "ruby:2.6-buster"
gemfile: "gemfiles/rails_7_0.gemfile"
6 changes: 0 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ RSpec/DescribedClass:
- 'spec/tenant_spec.rb'
- 'spec/unit/elevators/host_hash_spec.rb'
- 'spec/unit/migrator_spec.rb'
- 'spec/unit/reloader_spec.rb'

# Offense count: 5
# Cop supports --auto-correct.
Expand Down Expand Up @@ -128,7 +127,6 @@ RSpec/FilePath:
- 'spec/unit/elevators/host_spec.rb'
- 'spec/unit/elevators/subdomain_spec.rb'
- 'spec/unit/migrator_spec.rb'
- 'spec/unit/reloader_spec.rb'

# Offense count: 1
# Cop supports --auto-correct.
Expand Down Expand Up @@ -159,7 +157,6 @@ RSpec/InstanceVariable:
# Cop supports --auto-correct.
RSpec/LeadingSubject:
Exclude:
- 'spec/unit/reloader_spec.rb'

# Offense count: 2
RSpec/LeakyConstantDeclaration:
Expand Down Expand Up @@ -196,7 +193,6 @@ RSpec/NamedSubject:
- 'spec/support/contexts.rb'
- 'spec/support/requirements.rb'
- 'spec/tenant_spec.rb'
- 'spec/unit/reloader_spec.rb'

# Offense count: 24
RSpec/NestedGroups:
Expand All @@ -217,7 +213,6 @@ RSpec/VerifiedDoubles:
Exclude:
- 'spec/integration/apartment_rake_integration_spec.rb'
- 'spec/unit/elevators/first_subdomain_spec.rb'
- 'spec/unit/reloader_spec.rb'

# Offense count: 17
Style/Documentation:
Expand All @@ -233,7 +228,6 @@ Style/Documentation:
- 'lib/apartment/migrator.rb'
- 'lib/apartment/model.rb'
- 'lib/apartment/railtie.rb'
- 'lib/apartment/reloader.rb'
- 'lib/apartment/tasks/enhancements.rb'
- 'lib/apartment/tasks/task_helper.rb'
- 'lib/generators/apartment/install/install_generator.rb'
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.3
2.7.5
36 changes: 12 additions & 24 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
# frozen_string_literal: true

appraise 'rails-5-0' do
gem 'rails', '~> 5.0.0'
platforms :ruby do
gem 'pg', '< 1.0.0'
end
platforms :jruby do
gem 'activerecord-jdbc-adapter', '~> 50.0'
gem 'activerecord-jdbcpostgresql-adapter', '~> 50.0'
gem 'activerecord-jdbcmysql-adapter', '~> 50.0'
end
end

appraise 'rails-5-1' do
gem 'rails', '~> 5.1.0'
platforms :ruby do
gem 'pg', '< 1.0.0'
end
platforms :jruby do
gem 'activerecord-jdbc-adapter', '~> 51.0'
gem 'activerecord-jdbcpostgresql-adapter', '~> 51.0'
gem 'activerecord-jdbcmysql-adapter', '~> 51.0'
end
end

appraise 'rails-5-2' do
gem 'rails', '~> 5.2.0'
platforms :jruby do
Expand Down Expand Up @@ -57,6 +33,18 @@ appraise 'rails-6-1' do
end
end

appraise 'rails-7-0' do
gem 'rails', '~> 7.0.0'
platforms :ruby do
gem 'sqlite3', '~> 1.4'
end
platforms :jruby do
gem 'activerecord-jdbc-adapter', '~> 61.0'
gem 'activerecord-jdbcpostgresql-adapter', '~> 61.0'
gem 'activerecord-jdbcmysql-adapter', '~> 61.0'
end
end

appraise 'rails-master' do
gem 'rails', git: 'https://github.com/rails/rails.git'
platforms :ruby do
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Setting this configuration value to `false` will disable the schema presence che

```ruby
Apartment.configure do |config|
tenant_presence_check = false
config.tenant_presence_check = false
end
```

Expand Down
25 changes: 0 additions & 25 deletions gemfiles/rails_4_2.gemfile

This file was deleted.

23 changes: 0 additions & 23 deletions gemfiles/rails_5_0.gemfile

This file was deleted.

23 changes: 0 additions & 23 deletions gemfiles/rails_5_1.gemfile

This file was deleted.

6 changes: 0 additions & 6 deletions gemfiles/rails_5_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
source "http://rubygems.org"

gem "rails", "~> 5.2.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
gem "pry"
end

platforms :jruby do
gem "activerecord-jdbc-adapter", "~> 52.0"
Expand Down
6 changes: 0 additions & 6 deletions gemfiles/rails_6_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
source "http://rubygems.org"

gem "rails", "~> 6.0.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
gem "pry"
end

platforms :ruby do
gem "sqlite3", "~> 1.4"
Expand Down
6 changes: 0 additions & 6 deletions gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
source "http://rubygems.org"

gem "rails", "~> 6.1.0"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
gem "pry"
end

platforms :ruby do
gem "sqlite3", "~> 1.4"
Expand Down
17 changes: 17 additions & 0 deletions gemfiles/rails_7_0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "rails", "~> 7.0.0"

platforms :ruby do
gem "sqlite3", "~> 1.4"
end

platforms :jruby do
gem "activerecord-jdbc-adapter", "~> 61.0"
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0"
gem "activerecord-jdbcmysql-adapter", "~> 61.0"
end

gemspec path: "../"
12 changes: 3 additions & 9 deletions gemfiles/rails_master.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@
source "http://rubygems.org"

gem "rails", git: "https://github.com/rails/rails.git"
gem "rubocop"

group :local do
gem "guard-rspec", "~> 4.2"
gem "pry"
end

platforms :ruby do
gem "sqlite3", "~> 1.4"
end

platforms :jruby do
gem "activerecord-jdbc-adapter", "~> 52.0"
gem "activerecord-jdbcpostgresql-adapter", "~> 52.0"
gem "activerecord-jdbcmysql-adapter", "~> 52.0"
gem "activerecord-jdbc-adapter", "~> 61.0"
gem "activerecord-jdbcpostgresql-adapter", "~> 61.0"
gem "activerecord-jdbcmysql-adapter", "~> 61.0"
end

gemspec path: "../"
39 changes: 39 additions & 0 deletions lib/apartment/active_record/postgresql_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

# rubocop:disable Style/ClassAndModuleChildren

# NOTE: This patch is meant to remove any schema_prefix appart from the ones for
# excluded models. The schema_prefix would be resolved by apartment's setting
# of search path
module Apartment::PostgreSqlAdapterPatch
def default_sequence_name(table, _column)
res = super
schema_prefix = "#{Apartment::Tenant.current}."
default_tenant_prefix = "#{Apartment::Tenant.default_tenant}."

# NOTE: Excluded models should always access the sequence from the default
# tenant schema
if excluded_model?(table)
res.sub!(schema_prefix, default_tenant_prefix) if schema_prefix != default_tenant_prefix
return res
end

res.delete_prefix!(schema_prefix) if res&.starts_with?(schema_prefix)

res
end

private

def excluded_model?(table)
Apartment.excluded_models.any? { |m| m.constantize.table_name == table }
end
end

require 'active_record/connection_adapters/postgresql_adapter'

# NOTE: inject this into postgresql adapters
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
include Apartment::PostgreSqlAdapterPatch
end
# rubocop:enable Style/ClassAndModuleChildren
32 changes: 10 additions & 22 deletions lib/apartment/adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'apartment/adapters/abstract_adapter'
require 'apartment/active_record/postgresql_adapter'

module Apartment
module Tenant
Expand Down Expand Up @@ -41,7 +42,6 @@ def default_tenant
def reset
@current = default_tenant
Apartment.connection.schema_search_path = full_search_path
reset_sequence_names
end

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

private
Expand Down Expand Up @@ -130,29 +129,18 @@ def postgresql_version
Apartment.connection.send(:postgresql_version)
end

def reset_sequence_names
# sequence_name contains the schema, so it must be reset after switch
# There is `reset_sequence_name`, but that method actually goes to the database
# to find out the new name. Therefore, we do this hack to only unset the name,
# and it will be dynamically found the next time it is needed
descendants_to_unset = ActiveRecord::Base.descendants
.select { |c| c.instance_variable_defined?(:@sequence_name) }
.reject do |c|
c.instance_variable_defined?(:@explicit_sequence_name) &&
c.instance_variable_get(:@explicit_sequence_name)
end
descendants_to_unset.each do |c|
# NOTE: due to this https://github.com/rails-on-services/apartment/issues/81
# unreproduceable error we're checking before trying to remove it
c.remove_instance_variable :@sequence_name if c.instance_variable_defined?(:@sequence_name)
end
end

def schema_exists?(schemas)
return true unless Apartment.tenant_presence_check

Array(schemas).all? { |schema| Apartment.connection.schema_exists?(schema.to_s) }
end

def raise_schema_connect_to_new(tenant, exception)
raise TenantNotFound, <<~EXCEPTION_MESSAGE
Could not set search path to schemas, they may be invalid: "#{tenant}" #{full_search_path}.
Original error: #{exception.class}: #{exception}
EXCEPTION_MESSAGE
end
end

# Another Adapter for Postgresql when using schemas and SQL
Expand Down
Loading

0 comments on commit fefed66

Please sign in to comment.