Skip to content

Fix a raw SQL for MySQL2 - #3529

Merged
pglombardo merged 1 commit into
masterfrom
fix-mysql2-drop-legacy
Jul 19, 2025
Merged

Fix a raw SQL for MySQL2#3529
pglombardo merged 1 commit into
masterfrom
fix-mysql2-drop-legacy

Conversation

@ozovalihasan

@ozovalihasan ozovalihasan commented Jul 14, 2025

Copy link
Copy Markdown
Collaborator

Description

ActiveRecord::Base.connection.execute(<<-SQL).first
   SELECT (
     (SELECT COUNT(*) FROM passwords) +
     (SELECT COUNT(*) FROM urls) +
     (SELECT COUNT(*) FROM file_pushes)
   ) as total_count
SQL

is returning [0] for MySQL2 and {"total_count" => 0} for SQLite3 and PostgreSQL. So, it is causing an error.

It is fixed to return 0;

ActiveRecord::Base.connection.select_value(<<-SQL)
     SELECT (
       (SELECT COUNT(*) FROM passwords) +
       (SELECT COUNT(*) FROM urls) +
       (SELECT COUNT(*) FROM file_pushes)
     )
SQL

What is the problem?

SQLite2 returns result of high-level queries in a hash. So, it doesn't cause any issue when working with ActiveRecord. But, the result of lower-level #execute method is returned in an array.

Related Issue

#3458

Type of Change

  • 馃摎 Examples / docs / tutorials / dependencies update
  • 馃敡 Bug fix (non-breaking change which fixes an issue)
  • 馃 Improvement (non-breaking change which improves an existing feature)
  • 馃殌 New feature (non-breaking change which adds functionality)
  • 馃挜 Breaking change (fix or feature that would cause existing functionality to change)
  • 馃攼 Security fix

Checklist

  • I've written tests (if applicable) for all new methods and classes that I created. (rake test)
    -- Tested manually
  • I've added documentation as necessary so users can easily use and understand this feature/fix.
    -- No need to document

@ozovalihasan
ozovalihasan requested a review from Copilot July 14, 2025 13:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the raw SQL check in the DropLegacyModels migration so it works correctly with MySQL2 by using select_value instead of execute(...).first.

  • Replace execute(...).first["total_count"] with select_value(...)
  • Remove the unused SQL alias total_count in the query

Comment thread db/migrate/20250606094103_drop_legacy_models.rb
@ozovalihasan
ozovalihasan requested a review from pglombardo July 14, 2025 13:58
@ozovalihasan
ozovalihasan marked this pull request as ready for review July 14, 2025 13:58
@ozovalihasan ozovalihasan changed the title Fix the result of a raw SQL in MySQL2 Fix a raw SQL for MySQL2 Jul 14, 2025
@pglombardo
pglombardo merged commit c3bb8d0 into master Jul 19, 2025
6 checks passed
@pglombardo
pglombardo deleted the fix-mysql2-drop-legacy branch July 19, 2025 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants