Adding new column with default value to high volume database table

Almost all Ruby on Rails developers might come across scenario where they need to add a new column with a default value to one of the database tables. Most of us (including me) would write following migration statement - add_column :table_name, :column_name, :boolean, default: false This is a good practice but would cause downtime if the table has large number of records. It took 3 secs when I ran the migration for a table having 50k records. -- add_column(:table_name, :column_name, :boolean, {:default=>false}) -> 3.3695s 3 secs is a long

Things which Rails could do but I didn't know.

Run Helper methods in the console $rails c => helper.number_to_currency(100) => "$100.00" => helper.time_ago_in_words(3.days.ago) => "3 days" Shorthand Migrations $ rails g resource person first_name:string last_name:string email:string token:string You can write above command as- $ rails g resource person first_name last_name email token Both will generate same migration: class CreatePeople Add Indexes to migrations $rails g resource person first_name:index last_name email:uniq token class CreatePeople true end end Add Associations to migrations $ rails g resource company person:references name description:text We can

Migrating (export and import) PostgreSQL database from Heroku to SoftLayer (or dedicated server)

Recently we had requirement in one of the project to migrate Heroku PostgreSQL database to SoftLayer (dedicated server) psql database. One major issue we faced during migration i.e. import Heroku pg database to SoftLayer database using pg_restore is the version conflict between the dump file that Heroku generates (for psql backups) and the old version 8.4. of PostGreSQL that SoftLayer instance has. We upgraded PostgreSQL to version 9.1 on SoftLayer. Remember to upgrade version above Heroku dump file or use same version but not below versions as pg_restore doesn't handle backward compatibility properly for dump