This blog is part of our Rails 5 series.
Database schemas change rapidly as project progresses. And
it can be difficult to track purpose of each table
each column in a large project with multiple team members.
The solution for this problem is to document data models right from Rails migrations.
Solution in Rails 4
You can add comments in Rails 4.x migrations using gems like
Solution in Rails 5
allows to specify comments
for tables, column
indexes in migrations.
These comments are stored in database itself.
Currently only MySQL and PostgreSQL supports adding comments.
We can add comments in migration as shown below.
When we run above migration output will look as shown below.
The comments are also dumped in
db/schema.rb file for PostgreSQL and MySQL.
db/schema.rb of application will have following content after running
products table migration .
We can view these comments with Database Administration Tools such as MySQL Workbench or PgAdmin III.
PgAdmin III will show database structure with comments as shown below.
If we update comments through migrations, corresponding comments will be updated in