Showing application version data in your Rails App

In my top secret (but hopefully soon-to-be-ready-for-public-beta) Ruby on Rails application, I had a strong desire to be able to see what revision of code I was running on my server. I use Subversion for all my version control needs, and must say I cant live without it. For this app I do most of the development in the wee hours of the morning (aka ‘free time’ ), what I like to call BBD, Before Bed Development. I code on my laptop (and do basic testing) but then I deploy, using Capistrano, to my server and really use the application and really test it.

Often, after making small changes, I might forget to check something in or deploy, and when using the live version on the web, I can’t tell which version I’m running, or if the FastCGI processes have been restarted, etc. I thought adding some simple versioning info within the main view might be really helpful.

If you’re using Capistrano to deploy (and if you aren’t why not?) this is actually really easy.

$ ./script/generate model Version

This will generate the model and empty migration files. Now to fill in the migration:

class CreateVersions < ActiveRecord::Migration
  def self.up
    create_table :versions do |t|
      t.column "revision", :integer
      t.column "created_at", :datetime
    end
  end

  def self.down
    drop_table :versions
  end
end

Then apply the migration:

$ rake migrate

Now that we have the database all set up, lets set up our deployment recipe so that the info will be filled in on deploy (in config/delpoy.rb):


task :after_update_code do
  run "cd #{release_path}/ && ./script/runner -e production 'Version.create(:revision => #{revision})'"
end

All this does is tells Capistrano that after it updates the code, it should get into the directory of the app we just deployed and use the ./script/runner tool to create a new version in our production database with the revision number we just checked out. Also because we have a ‘created_at’ row in our ‘versions’ db table, we’ll get the full time-stamp of the deployment.

You can test it out and see if it works by running rake deploy and checking the database on your production server.

But this is no fun – the whole point was that we wanted to show the information, not just store it. So in the Version model, lets add a method to fetch us the most recent version info (in app/models/version.rb):

class Version < ActiveRecord::Base

  def self.latest
    find(:first,:order => "created_at DESC")
  end
  
end

All this little guy does is allow us to call ‘Version.latest’ in our controller and get the info about the most recent update. Lets do that. (in app/controllers/application.rb)

  def load_version
    session[:version] = Version.latest unless session[:version]
    @version = session[:version]
  end

Since we want to be able to get the versioning info throughout our app, we put this little diddy in the application controller. If you want to use it just in one specific section, just put it in that one controller. Using session keeps it from making the same DB query every-time a page loads. Now we can also add a filter so we dont have to call this method in every action (in app/controllers/application.rb)

  before_filter :load_version

Just one thing left – the display. Wherever you want to put it (I put it in my default layout):

QTodo: rev. <%= @version.revision %><br />
            Code Last Updated<br /> 
            <%= @version.created_at.strftime("%m/%d/%Y %I:%M%p") %>

And you can get something like this:

versioning-qtodo.png

I think there are some great uses for this, especially if you work in a team. Wouldn’t it be a lot easier to just see what version you’re running, instead of having to poke around, or FTP to the server and poke around?

Resources:

The Capistrano ‘Book’
Capistrano on Dreamhost

Comments are closed.

About

QuirkeyBlog is Aaron Quint's perspective on the ongoing adventure of Code, Life, Work and the Web.

twitter/@aq.

instagram/@quirkey.

github/quirkey.

QuirkeyBlog is proudly powered by WordPress

Categories