2

I am using ruby on rails for an application. I am developing it on my local server as of now.

Everytime I need to make a change to the database, I need to create a migration. Why can't I directly make changes in schema.rb itself?

I am allowed to reset the database and reset all values in the tables. I came across a problem where I needed to change date format from "dateTime" to "timestamp". Now there are just too many fields to change. Why can't I just change them in schema.rb?

Abhinav
  • 722
  • 2
  • 11
  • 27

3 Answers3

3

schema.rb is an automatically generated file and will be dumped from the current state of the database after you ran a migration. Although I strongly discourage it, it is in fact possible to change it manually, then run rake db:schema:load to apply it to the database. However, you will loose all the benefits you get from migrations, and you'd be ignoring the convention.

So, what are the benefits, you ask? Just to name a few:

  • They can be rolled back when you made a mistake
  • They make it easier to handle multiple developers on a single project
  • They provide a place to clean up and move around data before/after applying the change
  • They give you a history of changes to the database schema
  • They reduce some of the boilerplate by mapping rails concepts such as polymorphic relationships to simple DSL commands so you don't need to think as much how the columns should be named and typed
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
1

First, migrations are easily revertable. Second, with migrations you have history and, more important, order of changing things. Third, you can add additional code to migrations (for example, calculate some value for just added column).

And I'm sure there are more benefits.

hedgesky
  • 3,271
  • 1
  • 21
  • 36
0

Migrations are key to ensuring that your dev, test and prod environments are all identical. If you start mucking around in the database manually dev will very quickly look nothing like prod... In fact, it becomes extremely likely that you will begin doing your development in prod which is a very bad idea!

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67