Now When Did I Add That? 1
A colleague noticed today that on of our Rails models was raising a validation error when attempting to update a previously-entered row. A validates_uniqueness_of constraint was being violated.
He wondered how the data could have been saved in the first place? As it turns out, the validates_uniqueness_of had been added to the model fairly recently. Here’s how we found out exactly when and by whom:
$ svn blame app/models/widget.rb | grep validates_uniqueness_of
494 showaltb validates_uniqueness_of :nameThe Subversion blame command annotates each line of a file with the revision number in which that line was last changed and the user that committed that revision. I used grep to just find any validates_uniqeness_of lines in the file.
From the output, you can see that the line was added (or changed) in revision 494 by user showaltb (that’s me).
Hmm, was the line added or changed? Let’s ask Subversion:
$ svn diff -c494 app/models/widget.rb | grep validates_uniqueness_of
+ validates_uniqueness_of :name-c494 tells Subversion to show me the changes that were made by revision 494 (i.e. between 493 and 494). Yep, it was an add (the + indicates an added line).
Now, when did I add that line? We need to know when changeset 494 was committed:
$ svn info -r494 app/models/widget.rb
Path: app/models/widget.rb
...(snip)...
Last Changed Date: 2007-10-08 09:51:03 -0400 (Mon, 08 Oct 2007)You can pass a revision number to svn info. Cool. OK, so that line was added on October 8.
Now, generally, if you add or change a validation rule, you need to consider what to do about your existing data, because some of it may not be valid any more. One option would be to create a migration that would fix any validation problems.
For example, with my example here, I could write a migration to find any rows with duplicate names and add some kind of “tie-breaker” to the names to avoid the duplication problem.
i love svn blame but it always blames me, so i stopped using it :)