has_many :questions

January 17, 2010

Let your SQL Growl in Rails 3

Filed under: Uncategorized — gavinstark @ 3:40 pm

While participating (mostly working on getting some existing apps running under 3.0) in the Rails BugMash this weekend I noted there was recently a subscription system for logging added by José Valim.

I usually enable some hack to watch SQL while I’m in script/console so I thought I’d see if I could use these new subscription models to see the passing sql in growl. I came up with this (sorry for the lack of formatting, I’ll fix this up later) which you can put in development.rb or whatever method you prefer for development-mode-only code.


Gist: http://gist.github.com/280099

(Sorry for the image paste and the gist link, but my wordpress account doesn’t let me embed javascript needed for gist, time to upgrade/move the blog)

You enable the growling of SQL via a call to SQLGrowler::enable in the script/console session. You will also need to require the ‘g’ gem in development mode as well.

I have a couple of problems with this:

  1. There is no way to turn it off once it is on.
  2. It seems there is only one subscriber per namespace so I inherit and ‘super’ — maybe not the best way, but it doesn’t clobber the existing logging facility. Thanks to lenary in the #railsbridge IRC for that suggestion.
  3. Growling all your sql calls may be more annoying than helpful
  4. This is probably a horrible use of Rails::Subscriber for which I will be publicly admonished for using.

I haven’t asked the core team what the plan for Rails::Subscriber is yet. This may be a nice way to observe/log events coming from the core library (or perhaps even your own code)

Happy Growling.

15 Comments »

  1. […] This post was mentioned on Twitter by Joel Moss, Gavin Stark. Gavin Stark said: Growl all sql in Rails 3: http://bit.ly/8VUZfO That plus a list of my app changes (post soon) for Rails 3 my only contribs to Rails BugMash […]

    Pingback by Tweets that mention Let your SQL Growl in Rails 3 « has_many :questions -- Topsy.com — January 19, 2010 @ 4:01 pm | Reply

  2. […] Let your SQL Growl in Rails 3 […]

    Pingback by Caffeine Driven Development » Blog Archive » L33t Links #70 — January 20, 2010 @ 2:49 am | Reply

  3. […] Let your SQL Growl in Rails 3 (01/17/10) — A neat trick and an example on how to use Rails 3 subscription models. […]

    Pingback by Rails 3 Reading Material | Medium eXposure — January 21, 2010 @ 5:29 am | Reply

  4. […] Up To Speed With Rails 3 – Notes from the field upgrading to Rails 3 – Let your SQL growl in Rails 3 – Active Record Query Interface 3.0: good write up from Pratik Naik – Webbynode Guides: Rapid […]

    Pingback by crispycode » CC Links 11 — January 29, 2010 @ 4:38 pm | Reply

  5. […] Let Your SQL Growl in Rails 3.0 – Gavin Stark demonstrates how he tweaked Rails 3.0 to put out Growl notifications for all SQL queries performed by ActiveRecord. Cute trick. […]

    Pingback by Rails 3.0 Beta: 36 Links and Resources To Get You Going — February 5, 2010 @ 6:28 pm | Reply

  6. […] Configure seu Growl para mostrar SQL no Rails 3.0 – Gavin Stark mostra como ele alterou o Rails 3.0 para mostrar notificações via Growl para todas as queries SQL executadas pelo ActiveRecord. Belo truque. […]

    Pingback by Rails 3.0 Beta - 36 Links e artigos para você começar — February 6, 2010 @ 12:52 pm | Reply

  7. […] Lassen Sie Ihre SQL Growl in Rails 3,0 – Gavin Stark zeigt, wie er gezwickt Rails 3,0 to put out Growl-Benachrichtigungen für alle SQL-Abfragen durch ActiveRecord durchgeführt. Cute Trick. […]

    Pingback by Rails 3.0 Beta: 36 Links und Ressourcen To Get You Going « hemju – Linz Webdesign — February 7, 2010 @ 7:58 am | Reply

  8. […] Lassen Sie Ihre SQL Growl in Rails 3,0 – Gavin Stark zeigt, wie er gezwickt Rails 3,0 to put out Growl-Benachrichtigungen für alle SQL-Abfragen durch ActiveRecord durchgeführt. Cute Trick. […]

    Pingback by Rails 3.0 Beta: 36 Links und Ressourcen To Get You Going « hemju – homepage erstellen und webdesign — February 7, 2010 @ 7:59 am | Reply

  9. Hey mate,

    Nice post and thanks for going deep into subscriber and notifications stuff. The proper API to do that though, is in AS::Notifications. You could try something like this:

    ActiveSupport::Notifications.subscribe “active_record.sql” do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    g(‘%s (%.1fms) %s’ % [event.payload[:name], event.duration, event.payload[:sql].squeeze(‘ ‘)])
    end

    And that is it! Now you have your own subscriber without messing with Rails’ one!

    Rails::Subscriber is just another subscriber like the one we just created, with the only purpose of logging (maybe we could rename it to Rails::LoggingSubscriber?).

    Regards!

    Comment by José Valim — February 11, 2010 @ 9:15 am | Reply

    • José, thanks for the update, that code has a much better feel for it. Is there a list of all the notifications that can be subscribed to? If not, I might try to take a swing at finding and documenting them. Also, I think Rails::LoggingSubscriber would have been a better name.

      Comment by Gavin Stark — February 11, 2010 @ 9:38 am | Reply

  10. […] Let Your SQL Growl in Rails 3.0 – Gavin Stark demonstrates how he tweaked Rails 3.0 to put out Growl notifications for all SQL queries performed by ActiveRecord. Cute trick. […]

    Pingback by Rails 3.0 Beta: 36 Links and Resources To Get You Going « Особое программирование — February 11, 2010 @ 4:52 pm | Reply

  11. […] Gavin Stark writes about using instrumentation to log SQL calls […]

    Pingback by Rails 3 Resources | Web Development News — March 12, 2010 @ 9:40 am | Reply

  12. I built in a method disable to turn it off after enabling it – http://gist.github.com/338650

    Comment by Leonhardt Wille — March 20, 2010 @ 8:05 am | Reply

  13. Has anyone gotten this to work with current versions of Rails?

    Comment by David W. Wormuth — November 19, 2010 @ 7:40 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: