Skip to content

Instantly share code, notes, and snippets.

@terabyte
Last active December 12, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terabyte/4694419 to your computer and use it in GitHub Desktop.
Save terabyte/4694419 to your computer and use it in GitHub Desktop.
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Adms::Application.initialize!
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
if method_defined?(:connect)
alias_method :connect_no_sql_mode, :connect
end
def connect
connect_no_sql_mode
puts "Setting sql_mode..."
execute("SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO,STRICT_ALL_TABLES'")
end
end
ActiveRecord::Base.connection.reconnect!
end
end
(13:51:48):cmyers@cmyers-ubuntu:3 fix-bugs-in-event-tests¹] RAILS_ENV="test" bundle exec rails c
/home/cmyers/gitrepos/adms/config/environment.rb:15:in `connect': undefined local variable or method `connect_no_sql_mode' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x000000037e55b0> (NameError)
from /home/cmyers/.rvm/gems/ruby-1.9.2-p320@adms/gems/activerecord-3.2.9/lib/active_record/connection_adapters/mysql2_adapter.rb:78:in `reconnect!'
from /home/cmyers/gitrepos/adms/config/environment.rb:21:in `block in <top (required)>'
from /home/cmyers/gitrepos/adms/config/environment.rb:8:in `class_eval'
from /home/cmyers/gitrepos/adms/config/environment.rb:8:in `<top (required)>'
from /home/cmyers/.rvm/gems/ruby-1.9.2-p320@adms/gems/railties-3.2.9/lib/rails/application.rb:103:in `require'
from /home/cmyers/.rvm/gems/ruby-1.9.2-p320@adms/gems/railties-3.2.9/lib/rails/application.rb:103:in `require_environment!'
from /home/cmyers/.rvm/gems/ruby-1.9.2-p320@adms/gems/railties-3.2.9/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
RAILS_ENV="test" bundle exec rails c 5.12s user 0.64s system 99% cpu 5.780 total
@terabyte
Copy link
Author

terabyte commented Feb 1, 2013

Trying to override ActiveRecord::ConnectionAdapters::Mysql2Adapter.connect

But... it isn't always available. So sometimes, I get the above error. I presume I need to wrap it in an if responds_to? or something but I have no clue. Halp?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment