Skip to content

Commit

Permalink
Merge pull request rails#51969 from npezza93/insert-all-fix
Browse files Browse the repository at this point in the history
Parameterize table_name when constructing insert alias to avoid syntax error when table_name contains the database name for mysql
  • Loading branch information
yahonda authored Jun 4, 2024
2 parents 8159498 + 7f4b396 commit a6a840f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def build_insert_sql(insert) # :nodoc:
# MySQL 8.0.19 replaces `VALUES(<expression>)` clauses with row and column alias names, see https://dev.mysql.com/worklog/task/?id=6312 .
# then MySQL 8.0.20 deprecates the `VALUES(<expression>)` see https://dev.mysql.com/worklog/task/?id=13325 .
if supports_insert_raw_alias_syntax?
values_alias = quote_table_name("#{insert.model.table_name}_values")
values_alias = quote_table_name("#{insert.model.table_name.parameterize}_values")
sql = +"INSERT #{insert.into} #{insert.values_list} AS #{values_alias}"

if insert.skip_duplicates?
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/insert_all_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,18 @@ def test_upsert_all_with_unique_by_fails_cleanly_for_adapters_not_supporting_ins
assert_match "#{ActiveRecord::Base.lease_connection.class} does not support :unique_by", error.message
end

if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
def test_insert_all_when_table_name_contains_database
database_name = Book.connection_db_config.database
Book.table_name = "#{database_name}.books"

assert_nothing_raised do
Book.insert_all! [{ name: "Rework", author_id: 1 }]
end
Book.table_name = "books"
end
end

private
def capture_log_output
output = StringIO.new
Expand Down

0 comments on commit a6a840f

Please sign in to comment.