Skip to content

Conversation

mikz
Copy link

@mikz mikz commented Sep 25, 2025

Rails is using execute_batch when truncating tables and loading fixtures.

https://github.com/rails/rails/blob/bab99f8b51dfbff4d9e161b8f23c0dbf5d3b9314/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb#L232

https://github.com/rails/rails/blob/bab99f8b51dfbff4d9e161b8f23c0dbf5d3b9314/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb#L515

But since this gem is changing signature of raw_execute, then it does not work out of the box and needs to call execute instead.

https://github.com/rails/rails/blob/bab99f8b51dfbff4d9e161b8f23c0dbf5d3b9314/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb#L616-L620

PR upstream opened PNixx#216

Summary by CodeRabbit

  • New Features
    • Added support for executing multiple database statements in a single batch when using the ClickHouse adapter. This streamlines migrations and maintenance tasks by reducing repetitive calls, improving convenience and potential performance. Behavior remains consistent with executing the same statements individually, ensuring predictable outcomes and error handling. Users can now submit a list of statements for sequential execution without changing existing workflows or configuration.

@mikz mikz requested a review from gmitrev September 25, 2025 11:16
Copy link

coderabbitai bot commented Sep 25, 2025

Walkthrough

Introduces a new public method execute_batch(statements, name = nil, **kwargs) to iterate over an array of SQL statements and call execute for each, delegating to the existing execution path without altering error handling or other control flow.

Changes

Cohort / File(s) Summary
ClickHouse schema statements
lib/active_record/connection_adapters/clickhouse/schema_statements.rb
Added public method execute_batch(statements, name = nil, **kwargs) that loops through statements and invokes execute(statement, name, **kwargs) for each item. No other logic changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller
  participant SchemaStatements as Clickhouse::SchemaStatements
  participant Adapter as execute()

  Caller->>SchemaStatements: execute_batch(statements, name, **kwargs)
  loop for each statement in statements
    SchemaStatements->>Adapter: execute(statement, name, **kwargs)
    Adapter-->>SchemaStatements: result
  end
  SchemaStatements-->>Caller: nil or last result
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

I thump my paws on rolling logs,
Batch by batch through data bogs;
One hop per SQL, neat and terse,
I nibble rows, avoid the curse—
Execute, execute, onward I dash,
A rabbit of queries, batch by batch. 🐇📜

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly summarizes the primary change of adding support for the execute_batch method in the Clickhouse adapter, directly reflecting the key functionality introduced and aligning with the PR’s objectives.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch execute-batch-for-truncate-tables

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 56577ed and c6681ec.

📒 Files selected for processing (1)
  • lib/active_record/connection_adapters/clickhouse/schema_statements.rb (1 hunks)

Comment on lines +50 to +54
def execute_batch(statements, name = nil, **kwargs)
statements.each do |statement|
execute(statement, name, **kwargs)
end
end
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.

@mikz mikz merged commit 4efa83c into master Sep 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant