diff --git a/.github/workflows/active-record-multi-tenant-tests.yml b/.github/workflows/active-record-multi-tenant-tests.yml index 4912398..f0b6de4 100644 --- a/.github/workflows/active-record-multi-tenant-tests.yml +++ b/.github/workflows/active-record-multi-tenant-tests.yml @@ -47,15 +47,18 @@ jobs: - '3.0' - '3.1' - '3.2' + - '3.3' appraisal: - rails-6.0 - rails-6.1 - rails-7.0 - rails-7.1 + - rails-7.2 - active-record-6.0 - active-record-6.1 - active-record-7.0 - active-record-7.1 + - active-record-7.2 citus_version: - '10' - '11' diff --git a/Appraisals b/Appraisals index 7b1f908..b331bd3 100644 --- a/Appraisals +++ b/Appraisals @@ -16,6 +16,10 @@ appraise 'rails-7.1' do gem 'rails', '~> 7.1.0' end +appraise 'rails-7.2' do + gem 'rails', '~> 7.2.0' +end + appraise 'active-record-6.0' do gem 'activerecord', '~> 6.0.3' end @@ -31,3 +35,7 @@ end appraise 'active-record-7.1' do gem 'activerecord', '~> 7.1.0' end + +appraise 'active-record-7.2' do + gem 'activerecord', '~> 7.2.0' +end diff --git a/lib/activerecord-multi-tenant/habtm.rb b/lib/activerecord-multi-tenant/habtm.rb index 3161845..7af9f0d 100644 --- a/lib/activerecord-multi-tenant/habtm.rb +++ b/lib/activerecord-multi-tenant/habtm.rb @@ -12,7 +12,7 @@ def has_and_belongs_to_many_with_tenant(name, scope = nil, **options, &extension # rubocop:enable Naming/PredicateName has_and_belongs_to_many_without_tenant(name, scope, **options, &extension) - middle_reflection = _reflections[name.to_s].through_reflection + middle_reflection = _reflect_on_association(name.to_s).through_reflection join_model = middle_reflection.klass # get tenant_enabled from options and if it is not set, set it to false diff --git a/lib/activerecord-multi-tenant/model_extensions.rb b/lib/activerecord-multi-tenant/model_extensions.rb index f0acaa2..44fce9e 100644 --- a/lib/activerecord-multi-tenant/model_extensions.rb +++ b/lib/activerecord-multi-tenant/model_extensions.rb @@ -42,20 +42,15 @@ def partition_key .try(:instance_variable_get, :@partition_key) end - # Avoid primary_key errors when using composite primary keys (e.g. id, tenant_id) - def primary_key - if defined?(PRIMARY_KEY_NOT_SET) ? !PRIMARY_KEY_NOT_SET.equal?(@primary_key) : @primary_key - return @primary_key - end - + def reset_primary_key primary_object_keys = Array.wrap(connection.schema_cache.primary_keys(table_name)) - [partition_key] - @primary_key = if primary_object_keys.size == 1 - primary_object_keys.first - elsif table_name && - connection.schema_cache.columns_hash(table_name).include?(DEFAULT_ID_FIELD) - DEFAULT_ID_FIELD - end + self.primary_key = if primary_object_keys.size == 1 + primary_object_keys.first + elsif table_name && + connection.schema_cache.columns_hash(table_name).include?(DEFAULT_ID_FIELD) + DEFAULT_ID_FIELD + end end def inherited(subclass) diff --git a/lib/activerecord-multi-tenant/query_rewriter.rb b/lib/activerecord-multi-tenant/query_rewriter.rb index aa37570..aa8aa86 100644 --- a/lib/activerecord-multi-tenant/query_rewriter.rb +++ b/lib/activerecord-multi-tenant/query_rewriter.rb @@ -375,11 +375,18 @@ def relations_from_node_join(node_join) ActiveRecord::QueryMethods.prepend(MultiTenant::QueryMethodsExtensions) module MultiTenantFindBy - def cached_find_by_statement(key, &block) - return super unless respond_to?(:scoped_by_tenant?) && scoped_by_tenant? + if ActiveRecord::VERSION::MAJOR >= 7 && ActiveRecord::VERSION::MINOR >= 2 + def cached_find_by_statement(connection, key, &block) + return super unless respond_to?(:scoped_by_tenant?) && scoped_by_tenant? - key = Array.wrap(key) + [MultiTenant.current_tenant_id.to_s] - super + super(connection, Array.wrap(key) + [MultiTenant.current_tenant_id.to_s], &block) + end + else + def cached_find_by_statement(key, &block) + return super unless respond_to?(:scoped_by_tenant?) && scoped_by_tenant? + + super(Array.wrap(key) + [MultiTenant.current_tenant_id.to_s], &block) + end end end diff --git a/lib/activerecord-multi-tenant/relation_extension.rb b/lib/activerecord-multi-tenant/relation_extension.rb index c1d8900..15e7149 100644 --- a/lib/activerecord-multi-tenant/relation_extension.rb +++ b/lib/activerecord-multi-tenant/relation_extension.rb @@ -37,7 +37,14 @@ def generate_in_condition_subquery tenant_id = MultiTenant.current_tenant_id # Build an Arel query - arel = eager_loading? ? apply_join_dependency.arel : build_arel + arel = if eager_loading? + apply_join_dependency.arel + elsif ActiveRecord::VERSION::MAJOR >= 7 && ActiveRecord::VERSION::MINOR >= 2 + build_arel(klass.connection) + else + build_arel + end + arel.source.left = table # If the tenant ID is present and the tenant key is a column in the model, diff --git a/spec/activerecord-multi-tenant/model_extensions_spec.rb b/spec/activerecord-multi-tenant/model_extensions_spec.rb index a49af98..6b9edb4 100644 --- a/spec/activerecord-multi-tenant/model_extensions_spec.rb +++ b/spec/activerecord-multi-tenant/model_extensions_spec.rb @@ -132,6 +132,17 @@ def self.name end describe 'inspect method filters senstive column values' do + if ActiveRecord::VERSION::MAJOR >= 7 && ActiveRecord::VERSION::MINOR >= 2 + # related: https://github.com/rails/rails/pull/49765 + around do |example| + prev = Account.attributes_for_inspect + Account.attributes_for_inspect = :all + example.run + ensure + Account.attributes_for_inspect = prev + end + end + it 'filters senstive value' do account = Account.new(name: 'foo', password: 'baz') expect(account.inspect).to eq '#'