Row Version Migrations
Listen to this article
The more I use Rails, the more plugins I seem to create. Each time I start a new project, almost the first thing I find I want to do is to copy a chunk of code from an existing project. The moment that happens, I think: Plugin!
So here it is. A plugin that automatically generates the following row version columns for every table:
:created_at, :datetime, :null => false :updated_at, :datetime, :null => false :lock_version, :integer, :null => false, :default => 0
Not so difficult. In fact it seems almost trivial (as usual) but it means all my tables now get date/time stamps and optimistic locking for free.
Enjoy. As always, feedback, suggestions, patches, criticism, all welcome.
Comments
Bravo! Sadly all of these great migration enhancements are coming out long after I've created my database and moved on to developing the product :-) At least I'll have them for next time.
Posted by: Ted | June 3, 2006 03:10 AM
I tried to alter your "Row Version Migrations" plugin like this:
:created_at, :datetime, :null => false, :default => "now()"
:updated_at, :datetime, :null => false, :default => "now()"
:lock_version, :integer, :null => false, :default => 0
It does not work however since the now() somehow gets evaluated before it is sent to the database.
Do you have any idea as to why? I'm using Postgres.
Very nice plugin by the way :)
Posted by: Anders Janmyr | June 19, 2006 11:42 PM