makandra/query_diet — pull request #9 by dtao (2013-07-30 16:32:47+00:00)
Title: Improve support for PostgreSQL adapter

QueryDiet was working great for me at first, using SQLite in development. Eventually I switched to Postgres (to mirror what my app was using in production—on Heroku), and suddently QueryDiet was repeatedly reporting 0 queries.

Digging into the issue, I noticed two things. First, the Postgres adapter for ActiveRecord seems to insert some leading space at the beginning of the query, which caused `QueryDiet::Logger#log_query?` to return `false` for actual queries. Second, there must be a `break` somewhere in the bowels of the adapter's logging code, because I determined (from super not-hacy `puts` debugging!) that the code was getting to the `Benchmark.realtime` call but not past it. (If I'm not mistaken, this means that a `break` somewhere in the block executed by `yield` was causing the method to return early.)

So this commit updates the pattern matched by `log_query?` and puts the query storage in an `ensure` to force it to work.

I realize this is a bit crap without any tests, but I was feeling lazy and hoping you'd write them for me. If you want me to add them, let me know and I'll do it eventually :)