Guanlun
Guanlun

Reputation: 1608

Rails: Why using database migration?

In Rails we can create database migrations to modify the database columns, like add_column. But we can also directly edit the database file in db/migrate. So do we bother writing the migrations each time instead of changing the database file?

Thanks in advance.

Upvotes: 0

Views: 110

Answers (3)

Jeremy
Jeremy

Reputation: 1208

Rebuilding the database from either schema.rb or the initial create database migration will destroy all of your data. Migrations are needed for deploying incremental change to production systems. During your dev cycle I wouldn't use them. Use mocks, fixtures or factories for test/dev data.

Upvotes: 1

pjammer
pjammer

Reputation: 9577

In theory look at it like you have a production database with millions of rows. Each time you want to change or add in a database, you want to amend another file and apply those changes as to not lose that data.

If you just edit a db/migrate file, say from last week , first it wouldn't be picked up with bundle exec rake db:migrate but you'd have to rollback those changes, which destroys the data.

So, to answer the question in one line, keep adding migrations, even for simple changes.

Upvotes: 1

Ionut Hulub
Ionut Hulub

Reputation: 6326

the files in db/migrate are the files generated by the generate migration command

you can use the command to speed things up, cause rails favors convention over configuration. this is the rails way.

but if you want to do something by hand or have something more complicated to do you can manually edit the files in db/migrate, and even add new files. After that you have to run the rake db:migrate command to commit the changes to the db.

Upvotes: 1

Related Questions