Skip to content

Commit

Permalink
Support Rails7.1
Browse files Browse the repository at this point in the history
The `table_name` method was defined in `Arel::Nodes::Table` and `Arel::Nodes::TableAlias` to get the table name, but `Arel::Nodes::Table#table_name` was removed  rails/rails#46864.
Therefore, it is no longer possible to simply call `#table_name` to get `table_name`, so a `TableNode.table_name` has been added to get table_name from node.
  • Loading branch information
alpaca-tc committed Sep 13, 2023
1 parent 927d642 commit e8ae83e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/activerecord-multi-tenant/multi_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def self.multi_tenant_model_for_arel(arel)
return nil unless arel.respond_to?(:ast)

if arel.ast.relation.is_a? Arel::Nodes::JoinSource
MultiTenant.multi_tenant_model_for_table(arel.ast.relation.left.table_name)
MultiTenant.multi_tenant_model_for_table(TableNode.table_name(arel.ast.relation.left))
else
MultiTenant.multi_tenant_model_for_table(arel.ast.relation.table_name)
MultiTenant.multi_tenant_model_for_table(TableNode.table_name(arel.ast.relation))
end
end

Expand Down
20 changes: 11 additions & 9 deletions lib/activerecord-multi-tenant/query_rewriter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def visit_Arel_Attributes_Attribute(*args)

def visit_Arel_Nodes_Equality(obj, *args)
if obj.left.is_a?(Arel::Attributes::Attribute)
table_name = obj.left.relation.table_name
table_name = MultiTenant::TableNode.table_name(obj.left.relation)
model = MultiTenant.multi_tenant_model_for_table(table_name)
if model.present? && obj.left.name.to_s == model.partition_key.to_s
@current_context.visited_handled_relation(obj.left.relation)
Expand All @@ -101,7 +101,7 @@ def visit_MultiTenant_TenantJoinEnforcementClause(obj, *)
end

def visit_Arel_Table(obj, _collector = nil)
@current_context.visited_relation(obj) if tenant_relation?(obj.table_name)
@current_context.visited_relation(obj) if tenant_relation?(MultiTenant::TableNode.table_name(obj))
end

alias visit_Arel_Nodes_TableAlias visit_Arel_Table
Expand Down Expand Up @@ -179,7 +179,9 @@ class BaseTenantEnforcementClause < Arel::Nodes::Node
def initialize(tenant_attribute)
super()
@tenant_attribute = tenant_attribute
@tenant_model = MultiTenant.multi_tenant_model_for_table(tenant_attribute.relation.table_name)
@tenant_model = MultiTenant.multi_tenant_model_for_table(
MultiTenant::TableNode.table_name(tenant_attribute.relation)
)
end

def to_s
Expand Down Expand Up @@ -215,7 +217,7 @@ class TenantJoinEnforcementClause < BaseTenantEnforcementClause
def initialize(tenant_attribute, table_left)
super(tenant_attribute)
@table_left = table_left
@model_left = MultiTenant.multi_tenant_model_for_table(table_left.table_name)
@model_left = MultiTenant.multi_tenant_model_for_table(MultiTenant::TableNode.table_name(table_left))
end

private
Expand All @@ -241,7 +243,7 @@ def visit_MultiTenant_TenantJoinEnforcementClause(obj, collector)
module DatabaseStatements
def join_to_update(update, *args)
update = super(update, *args)
model = MultiTenant.multi_tenant_model_for_table(update.ast.relation.table_name)
model = MultiTenant.multi_tenant_model_for_table(MultiTenant::TableNode.table_name(update.ast.relation))
if model.present? && !MultiTenant.with_write_only_mode_enabled? && MultiTenant.current_tenant_id.present?
update.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
end
Expand All @@ -250,7 +252,7 @@ def join_to_update(update, *args)

def join_to_delete(delete, *args)
delete = super(delete, *args)
model = MultiTenant.multi_tenant_model_for_table(delete.ast.left.table_name)
model = MultiTenant.multi_tenant_model_for_table(MultiTenant::TableNode.table_name(delete.ast.left))
if model.present? && !MultiTenant.with_write_only_mode_enabled? && MultiTenant.current_tenant_id.present?
delete.where(MultiTenant::TenantEnforcementClause.new(model.arel_table[model.partition_key]))
end
Expand Down Expand Up @@ -295,7 +297,7 @@ def build_arel(*args)
node = context.arel_node

context.unhandled_relations.each do |relation|
model = MultiTenant.multi_tenant_model_for_table(relation.arel_table.table_name)
model = MultiTenant.multi_tenant_model_for_table(MultiTenant::TableNode.table_name(relation.arel_table))

if MultiTenant.current_tenant_id
enforcement_clause = MultiTenant::TenantEnforcementClause.new(relation.arel_table[model.partition_key])
Expand Down Expand Up @@ -330,8 +332,8 @@ def build_arel(*args)

next unless relation_right && relation_left

model_right = MultiTenant.multi_tenant_model_for_table(relation_left.table_name)
model_left = MultiTenant.multi_tenant_model_for_table(relation_right.table_name)
model_right = MultiTenant.multi_tenant_model_for_table(MultiTenant::TableNode.table_name(relation_left))
model_left = MultiTenant.multi_tenant_model_for_table(MultiTenant::TableNode.table_name(relation_right))
next unless model_right && model_left

join_enforcement_clause = MultiTenant::TenantJoinEnforcementClause.new(
Expand Down
13 changes: 13 additions & 0 deletions lib/activerecord-multi-tenant/table_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module MultiTenant
module TableNode
# Return table name
def self.table_name(node)
# NOTE: Arel::Nodes::Table#table_name is removed in Rails 7.1
if node.is_a?(Arel::Nodes::TableAlias)
node.table_name
else
node.name
end
end
end
end
1 change: 1 addition & 0 deletions lib/activerecord_multi_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
require_relative 'activerecord-multi-tenant/multi_tenant'
require_relative 'activerecord-multi-tenant/query_rewriter'
require_relative 'activerecord-multi-tenant/query_monitor'
require_relative 'activerecord-multi-tenant/table_node'
require_relative 'activerecord-multi-tenant/version'
require_relative 'activerecord-multi-tenant/habtm'

0 comments on commit e8ae83e

Please sign in to comment.