Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def execute(sql, name = nil, format: @response_format, settings: {})
end
end

def execute_batch(statements, name = nil, **kwargs)
statements.each do |statement|
execute(statement, name, **kwargs)
end
end
Comment on lines +50 to +54
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Restore fallback for single-statement batches

The upstream ActiveRecord::ConnectionAdapters::DatabaseStatements#execute_batch accepts either an enumerable of statements or a single statement and only iterates when the argument responds to to_ary. By overriding it with statements.each, calling execute_batch("TRUNCATE ...") will now raise NoMethodError (Strings and many other statement objects don’t implement each). Please keep parity with the abstract adapter and fall back to a single execute call when statements isn’t array-like.

 def execute_batch(statements, name = nil, **kwargs)
-  statements.each do |statement|
-    execute(statement, name, **kwargs)
-  end
+  if statements.respond_to?(:to_ary)
+    statements.to_ary.each { |statement| execute(statement, name, **kwargs) }
+  else
+    execute(statements, name, **kwargs)
+  end
 end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def execute_batch(statements, name = nil, **kwargs)
statements.each do |statement|
execute(statement, name, **kwargs)
end
end
def execute_batch(statements, name = nil, **kwargs)
if statements.respond_to?(:to_ary)
statements.to_ary.each { |statement| execute(statement, name, **kwargs) }
else
execute(statements, name, **kwargs)
end
end
🤖 Prompt for AI Agents
In lib/active_record/connection_adapters/clickhouse/schema_statements.rb around
lines 50 to 54, the current implementation always calls statements.each which
breaks when a single statement string is passed; change it to detect array-like
input (e.g. if statements.respond_to?(:to_ary) or similar) and iterate only in
that case, otherwise call execute(statements, name, **kwargs) once so behavior
matches ActiveRecord's fallback for single-statement batches.


def exec_insert(sql, name = nil, _binds = [], _pk = nil, _sequence_name = nil, returning: nil)
new_sql = sql.sub(/ (DEFAULT )?VALUES/, " VALUES")
with_response_format(nil) { execute(new_sql, name) }
Expand Down