views:

345

answers:

4

Is there a plugin available to have Rails run through a db:migrate on startup? I'm looking for a solution that doesn't involve calling out to the Rake task via the shell; so, no "system('rake db:migrate')".

I can readily write my own plugin to do this, but figured it would be better to use/improve an existing migrate-on-init plugin if one exists.

A: 

Why would you want one? Having your migrations run automatically, possibly when you don't want them to run, seems like a bit of a risky business to me. Just integrate running the migrations into your deployment automation (you do ''have'' deployment automation, right?) and everything will be much more reliable.

womble
Rails tracks the schema version, so the system won't re-run the same migration, and I'm likely to deploy this app on customer boxes, and don't want them to have to do extra work to handle database setup and upgrades.
Don Werve
A: 

With womble's answer in mind- perhaps you should write/use a plugin that detects pending migrations and warns you or emails you. Sometimes migrations require other (manual) operations.

Bill
They only require manual operations if they're written that way. The entire code stack goes through some very comprehensive testing, and I'll be adding in a DB backup before running the upgrade, so I feel safe enough in automating things.
Don Werve
I suspect you will have to roll your own.
Bill
A: 

In my experience, db:migrate-like routine should only be called when new migrations are present, because it takes quite a bit of time. You shouldn't really have so many migrations that you can't track when they are present.

That said, the task of running new migration in production environment is best handled by deployment tools like Capistrano. when deploying to production box, capistrano runs migrations if you tell it too.

Running db:migrate-like routine upon every-init is resource and time wastful.

ulver
+1  A: 

Put the following inside Rails::Initializer block in environment.rb ...

  config.after_initialize do
    ActiveRecord::Migrator.migrate (RAILS_ROOT + "/db/migrate" )
  end