shopify / activerecord-pedantmysql2-adapter Goto Github PK
View Code? Open in Web Editor NEWActive Record adapter for MySQL that reports warnings.
License: MIT License
Active Record adapter for MySQL that reports warnings.
License: MIT License
While setting up cod-app
for cloud, I realized that rails dbconsole
would fail with unknown command-line client for #{database_name}
.
I dug through the dbconsole source and attached a few puts
to the dbconsole in cluster and observed the following output:
The regex match on the case predicate in dbconsole.rb
will always fail since when /^(jdbc)?mysql/
begins with pedant
, not mysql
.
Not sure if this is worthy of a fix since I don't know how often rails dbconsole
gets used, but a simple solution seems to be simply renaming the adapter to mysql_pedant
.
Currently EXPLAIN
queries fail with this adapter installed because on recent versions of MySQL, the server generates a warning with level = "Note" and code = 1003
because the EXPLAIN
query is optimized away. This makes sense because an EXPLAIN query doesn't actually execute the query it is explaining.
Example output when calling EXPLAIN SELECT 1
:
[1] pry(main)> ActiveRecord::Base.connection.exec_query("EXPLAIN SELECT 1;")
SQL (1.1ms) EXPLAIN SELECT 1;
{:severity=>:warn, :messages=>["/* select#1 */ select 1 AS `1`"], :query=>"EXPLAIN SELECT 1;"}
MysqlWarning: /* select#1 */ select 1 AS `1`
from /Users/ryanseys/repo/pedant_mysql2.rb:14:in `block in <main>'
[2] pry(main)> ActiveRecord::Base.connection.exec_query("SHOW WARNINGS")
SQL (15.9ms) SHOW WARNINGS
#<ActiveRecord::Result:0x000000010d8becb0 @columns=["Level", "Code", "Message"], @rows=[["Note", 1003, "/* select#1 */ select 1 AS `1`"]], @hash_rows=nil, @column_types={}>
There's currently no great way to ignore these queries because PedantMySQL2.ignore
can only ignore based on warning messages, and does not ignore based on warning code or warning level.
The current workaround I found was to exit the on_warning lambda early if the warning meets the conditions of an EXPLAIN
query.
PedantMysql2.on_warning = lambda do |warning|
return if warning.level == "Note" && warning.code == 1003 && warning.query.start_with?("EXPLAIN")
raise warning
end
There's a few different ways I think we could solve this:
EXPLAIN
queries into the default behavior of this gemSimilar to how drop_table
queries are already ignored, we could ignore these warnings by default. Also not backward compatible per-se because perhaps someone wants to catch these warnings?
That said, these warnings are safe to ignore because they are just a warning that the MySQL query optimizer has optimized the query away and did not execute it, which is to be expected for EXPLAIN
queries. It also follows a similar behavior to the drop_table behavior where a table cannot be found -- the query optimizer is doing the same thing in this case by not executing the query because the table does not exist.
I've implemented this in #24
PedantMySQL.ignore_if(&block)
Introduce a way to ignore based on the contents of a block. This would be backward compatible as it is a new method, but it would require anyone who wants this functionality to manually implement it, which is annoying and probably means that people are more likely to not implement this correctly, and adds additional overhead to all developers using this gem.
PedantMySQL2.ignore_if do |warning|
warning.code == 1003 && warning.level == "Note" && warning.query.start_with?("EXPLAIN")
end
Re-use the existing ignore
but allow it to take a block. Not backward compatible as it reuses the same method that already exists. Probably the worst of the three options.
PedantMySQL2.ignore do |warning|
warning.code == 1003 && warning.level == "Note" && warning.query.start_with?("EXPLAIN")
end
I added the gem, bundled and changed the adapter in my datbase.yml. Then, I get this when starting a console:
/gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in require': Please install the pedantmysql2 adapter:
gem install activerecord-pedantmysql2-adapter` (cannot load such file -- active_record/connection_adapters/pedantmysql2_adapter) (LoadError)
Seems to work fine in a rails 4.1 app though.... is rails 3.x supported?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.