Tip #11 - Transact Your Migrations!
Thu Apr 17 10:23:10 -0700 2008
If you are using Rails, you are most likely using migrations. Have you ever had a migration fail half way through? Have you ever then had to figure out how to find each change and revert it in the database? Would you like to never have to do that again? Here is how…
USE TRANSACTIONS
I sometimes wonder why this just isn’t the default. When I am doing a migration, I either want the thing to work completely all the way through, or I want nothing done to the database.
Luckily, you can do this with transactions.
A transaction (for those non SQL people out there) is an idea of a series of events being atomic. That is, either, they all are executed without error, or if any one of them fails or faults, then no change is made to the database at all.
Luckily, adding transactions to a migration is very easy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class ComplexMigrationThatCanNotFail < ActiveRecord::Migration def self.up transaction do add_column :table, :name, :type add_column :table, :name, :type # Complex task #1 remove_column :table, :name, :type remove_column :table, :name, :type # Complex task #2 end end def self.down # ... Down script end end |
Now, say your migration fails at the second remove column, no fear, everything is rolled back to the start point, you can read the rake error messages, fix the problem, and try again.
Of course, you are testing this on the development server, so no major harm done if it blows up a couple of times, but I don’t know about you, but I have lots more things to do than try to restore two gigabytes of staging or development database :)
blogLater
Mikel
Leave a Reply
Latest posts
- rake RSpec & Cucumber uninitialized constant Rails::Boot::Bundler
- This Relationship is Worth Nothing
- Thank YOU...
- Inline Attachments for ActionMailer
- Upgrading RailsPlugins.org to Rails 3 - Part 1
- Stripping dollar signs and commas from a string
- Getting Rails 3 Edge with jQuery, RSpec and Cucumber using RVM
- Action Mailer, go Proc thyself
- The Real News Donation Drive
- ActionMailer ScreenCast and Article
- Installing RSpec for Rails 3
- I am speaking at RailsConf 2010
- Rails 3 Session Secret and Session Store
- If you're lazy and you know it write your specs!
- Bundler - uninitialized constant ActionController
- Bundle Me Some Sanity
- How to use Mail / ActionMailer 3 with GMail SMTP
- Put your mailer where the action is!
- Why Force a Choice?
- How to make an RSS feed in Rails
- Rails 3 Routing with Rack
- Bundle me some Rails
- Helping out in Haiti
- Watch your self
- Is Rails 3.0 a Game Changer?
- Where did the scripts go?
- validates :rails_3, :awesome => true
- New Rails Version 3.0 Guides Online
- New ActionMailer API in Rails 3.0
- Mail gem version 2 released
- How to rename a Rails 3 Application
- Rails 3.0 Examples
- DECCA Driving Day
- Mail now merged into ActionMailer
- Tip #29 - Stop a Mongrel (or any) Service in Windows
- Ruby on Rails Tips Page
- Monitoring a DAHDI or Zaptel Channel
- Mail gets some compliments!
- Rails Unit Tests: uninitialized constant error
- New Mail gem released
- Mail and Bounced Emails
- Mail, TMail, The Future of Ruby Email Handling
- Custom Music on Hold for Asterisk
- Always getting an invalid authenticity token error
- Windows ipconfig does not show anything
- FreeBSD rc scripts
- How to monitor a logged in professional
- TMail Moves to GIT
- Funny...
- How to reset a sequence with PostgreSQL
Latest comments
- ferrisoxide
Hey Mikel Thanks for the hea...
- B
@im - try this (from RSoC): htt...
- ferrisoxide
Hey Mikel Thanks for the hea...
- Ian Alexander Wood
Just a quick update on this exc...
- Daze
The command is "rails new app_n...
Categories
Tag Cloud
AJAX ARGH! ActiveRecord Ajax Apache Apple Asterisk Australia Copy Database Development Feedburner Gem server Google Human Rights Javascript L. Ron Hubbard MS SQL Server MacOSX Mail Mephisto Not Programming OpenBSD Opensource Performance Personal Integrity PostgreSQL Programming Prototype Puzzle RDoc REST RESTful Rails RSPec RSpec Rails Rails Tips Rspec Ruby Ruby on Rails Ruby on Rails Tips Ruby on rails Tips SQL SQLServer SVN Scientologist Scientology Site Stats Soekris Soekris net5501 TMail Textmate Tips Windows World about mikel anti drug apache contributing daemon documentation drugs illustrator javascript lambda mail mephisto newspapers nitro open source opensource photoshop productivity programming railscasts rspec ruby ruby on rails rubyforge scientology seo sitemap sqlserver tips tmail tom cruise unix tricks vector graphicsArchives
- November 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (0)
- July 2009 (1)
- June 2009 (0)
- May 2009 (1)
- April 2009 (0)
- March 2009 (0)
- February 2009 (0)
- January 2009 (2)
- December 2008 (0)
- November 2008 (5)
- October 2008 (0)
- September 2008 (1)
- August 2008 (0)
- July 2008 (2)
- June 2008 (13)
- May 2008 (7)
- April 2008 (18)
- March 2008 (8)
- February 2008 (5)
- January 2008 (7)
- December 2007 (20)
- November 2007 (22)


Sun May 11 14:28:11 -0700 2008
You can automate this by using the transaction_migrations plugin:
http://www.redhillonrails.org/transactional_migrations.html
Sun May 11 20:17:19 -0700 2008
This only works if the DB supports transactional DDL (like PostrgreSQL). It will fail on other DBs, like MySQL or Oracle. On those bases, the schema modifing statements are un-rollback-able. That’s why it’s not a default :)
(You can still, of course, rollback all other statements like INSERTs or UPDATEs.)
Sun May 11 20:21:26 -0700 2008
Szerf: Yeah… but who wouldn’t use PostgreSQL? :D
Good point though… I have gotten so used to the advanced features of Postgres that I sort of forget that the other DBs don’t support everything it does.
Mikel
Sat May 31 23:36:08 -0700 2008
Firebird is another great RDBMS with full transactioning support (but ok, Firebird connectors and ActiveRecord adapters are not… as good as one can expect)
Mon Jul 07 19:41:37 -0700 2008
Well, it’s not accurate that MySQL do not support transactions, it does when the used storage engine is InnoDB and not the default MyISAM.
http://railsforum.com/viewtopic.php?id=3632&p=2