Patch ActiveRecord with PR 54658

This commit is contained in:
cryptogopher 2025-03-22 14:14:42 +01:00
parent 8401424efa
commit 4c867daabb
8 changed files with 87 additions and 0 deletions

View File

@ -11,4 +11,15 @@ end
ActiveSupport.on_load :active_record do
ActiveModel::Validations::NumericalityValidator
.prepend CoreExt::ActiveModel::Validations::NumericalityValidatesPrecisionAndScale
# Temporary patch for https://github.com/rails/rails/pull/54658
Arel::TreeManager::StatementMethods
.prepend CoreExt::Arel::TreeManager::StatementMethodsCteUpdateAndDelete
Arel::Nodes::DeleteStatement
.prepend CoreExt::Arel::Nodes::DeleteStatementCteUpdateAndDelete
Arel::Nodes::UpdateStatement
.prepend CoreExt::Arel::Nodes::UpdateStatementCteUpdateAndDelete
Arel::Visitors::ToSql.prepend CoreExt::Arel::Visitors::ToSqlCteUpdateAndDelete
Arel::Crud.prepend CoreExt::Arel::CrudCteUpdateAndDelete
Arel::SelectManager.prepend CoreExt::Arel::SelectManagerCteUpdateAndDelete
end

View File

@ -0,0 +1,3 @@
module CoreExt::ActiveRecord::Relation::UpdateAndDeleteCteSupport
INVALID_METHODS_FOR_UPDATE_AND_DELETE_ALL = [:distinct]
end

View File

@ -0,0 +1,11 @@
module CoreExt::Arel::CrudCteUpdateAndDelete
def compile_update(...)
um = super
um.with = subqueries
end
def compile_delete(...)
dm = super
dm.with = subqueries
end
end

View File

@ -0,0 +1,16 @@
module CoreExt::Arel::Nodes::DeleteStatementCteUpdateAndDelete
attr_accessor :with
def initialize(...)
super
@with = nil
end
def hash
[self.class, @relation, @wheres, @orders, @limit, @offset, @key, @with].hash
end
def eql?(other)
eql?(other) && self.with == other.with
end
end

View File

@ -0,0 +1,16 @@
module CoreExt::Arel::Nodes::UpdateStatementCteUpdateAndDelete
attr_accessor :with
def initialize(...)
super
@with = nil
end
def hash
[self.class, @relation, @wheres, @orders, @limit, @offset, @key, @with].hash
end
def eql?(other)
eql?(other) && self.with == other.with
end
end

View File

@ -0,0 +1,5 @@
module CoreExt::Arel::SelectManagerCteUpdateAndDelete
def subqueries
@ast.with
end
end

View File

@ -0,0 +1,6 @@
module CoreExt::Arel::TreeManager::StatementMethodsCteUpdateAndDelete
def with=(expr)
@ast.with = expr
self
end
end

View File

@ -0,0 +1,19 @@
module CoreExt::Arel::Visitors::ToSqlCteUpdateAndDelete
def visit_Arel_Nodes_DeleteStatement(o, collector)
if o.with
collector = visit o.with, collector
collector << " "
end
super
end
def visit_Arel_Nodes_UpdateStatement(o, collector)
if o.with
collector = visit o.with, collector
collector << " "
end
super
end
end