Magical Rails migrations
Rails migration is one of the best tools provided in Rails that helps you to handle database very efficiently. Here I am gonna add some internal stuff that Rails do under the hood.
Rails migration I feel is a great tool to manage your db schema, It always work like charm and make my life very comfortable to deal with db.
Am I going to explain how the migrations work?
noooo, Its already there in Rails guide go check it here:- Rails Migration
But the thing that excites me is How Rails manage schema, e.g:- I want to add user_name column in my users table.
rails g migration add_user_name_to_users nameand it creates file with some content similar to :-
class AddUserNameToUsers < ActiveRecord::Migration def change add_column :users, :user_name, :string end end
You ran some command in terminal, it generated a file and on running
rake db:migrateit create all those columns in your database. It feels like
The question that came to my mind was, I already had 10 extra files in my
db/migratefolder, how does rails exactly know its just this file that need to run.
I have seen other developers working on other framworks and its a pain in the ass to handle db changes, they used to take db dumps, share it within team and in Rails it works without any effort this thing increased the curiosity to many folds.
What Rails do is, when you try to create some migration file it take the UTC timestamp and put that in filename e.g:- if this is filename
I also found there is another table named
schema_migrationand it is having timestamp of all the files those are already migrated.
schema_migrationtable looks something like this, Its basically timestamp of all the migration files already migrated.
version ---------------- 20150714124350 20150714124402 20150714145508 20150714201233 20150714204601
Eureka moment ;)
And that eureka moment arrived when I find out that basically what Rails does is it keeps the file timestamp in this
schema_migrationand whenever we run
rake db:migrateit checks if there is any other file whose time stamp is not there in the table it pass that file to parser.
Then Migration class parses the content and run sql queries accordingly for those files only.
It also parses the
20150714204601out of file name i.e.
20150714204601_add_username_to_usersand add it to
schema_migration, this is the reason once file is migrated its not migrated again.
This is one of those things that I felt Rails handles greatly, well I came across this when Migration stopped working for me, on production as my db was very big for deployment script to migrate and I had to run all these queries directly. ;)
Happy Coding :)Written on November 14, 2015
Share in your circles :)