trilogy-libraries / activerecord-trilogy-adapter

Active Record adapter for the Trilogy database client for Rails v6.0 - v7.0.
https://github.com/trilogy-libraries/trilogy
MIT License
171 stars 17 forks source link

`sql_mode` specification is not applied in ActiveRecord v7.0.6 #54

Closed tmimura39 closed 1 year ago

tmimura39 commented 1 year ago

Steps to reproduce

It seems that various settings including sql_mode are not applied correctly because configure_connection is not executed.

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", "= 7.0.6"
  gem "activerecord-trilogy-adapter"
  gem "mysql2"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.logger = Logger.new(STDOUT)

class BugTest < Minitest::Test
  def test_mysql2
    ActiveRecord::Base.establish_connection(adapter: "mysql2", database: "test", username: "root", host: '127.0.0.1', port: 3306, variables: { sql_mode: "STRICT_ALL_TABLES" })
    assert_equal ActiveRecord::Base.connection.execute('SELECT @@SESSION.sql_mode;').to_a, [["STRICT_ALL_TABLES"]]
  end

  def test_trilogy
    ActiveRecord::Base.establish_connection(adapter: "trilogy", database: "test", username: "root", host: '127.0.0.1', port: 3306, variables: { sql_mode: "STRICT_ALL_TABLES" })
    assert_equal ActiveRecord::Base.connection.execute('SELECT @@SESSION.sql_mode;').to_a, [["STRICT_ALL_TABLES"]]
  end
end

Expected behavior

Both mysql2 and torilogy, sql_mode is correctly applied as specified in the DB connection settings variables.

Actual behavior

mysql2 correctly applies sql_mode. However, trilogy applies the default sql_mode.

Failure:
BugTest#test_trilogy [main.rb:29]:
--- expected
+++ actual
@@ -1 +1 @@
-[["ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"]]
+[["STRICT_ALL_TABLES"]]

It seems to be due to the fact that the Trilogy Adapter assumes https://github.com/rails/rails/pull/44591. Current release versions, including Rails v7.0.6, do not include this fix and will not work properly.

System configuration

Rails version: v7.0.6

Ruby version: v3.2.2

trilogy version: v2.4.1

activerecord-trilogy-adapter: v3.1.0

lorint commented 1 year ago

Hi, Tomohiko Mimura! Thank you for your research with this issue. Have put together PR #56 with hopes it might offer a comprehensive fix.

Many thanks for your contributions to ActiveRecord Trilogy Adapter!