Randuin posted a comment on my previous [_Lighting fast, zero-downtime deployments with git, capistrano, nginx and Unicorn_][1] post asking how I handle database migrations. This is a good question.
Database migrations, especially with large datasets, take a long time to run. They also lock your database tables which may cause all kinds of trouble.
There's a quite an easy solution for this, offered to us by Capistrano. Unfortunately it will cause downtime for your site while the migration is running.
The first thing you should do it update your Nginx configuration in `/etc/nginx/sites-available/default` and add the following snippet, just before the `location /`:
As [iGEL][3] and [Anlek Consulting][4] pointed out in the comments, it's good practice to send a _503 Service Temporarily Unavailable_ HTTP code back to the client. A normal user won't notice the difference, but spiders do. Sending the 503 code will tell search engines, like Google, that your site is not available and that they should not re-index your maintenance message as the new site content. Instead, they'll come back later when your site returns a _HTTP 200_ code again.
You can try this fairly easily by putting your site in maintenance mode (per the instructions that follow) and do a HEAD request:
This task will upload a generic placeholder and place it in `public/system/maintenance.html`. If nginx sees this file exists, it will render it and abort further processing. So, as long as the maintenance.html file is present, your app is not accessible.
Of course you want to customize the maintenance page, because, frankly, it's kind of ugly by default. This does require you write your own `deploy:web:disable` task:
Now you can create a maintenance page for your app at `app/views/layouts/maintentance.html.erb`. See [the original maintenance template][2] for inspiration.
Note that you can use the environment variables `REASON` and `UNTIL` to give some info about why the app is down.
_Keep in mind that when you put up the `maintenance.html` none of your files is accessible. This includes stylesheets and js files. Make sure to host any images and other assets on a remote server like Amazon S3._