Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete old migrations files in a Rails app

Tags:

Is it permissible to delete (or archive) old migration files in a Rails app if the schema is stable?

My migrations are numerous and I suspect there may be some problem in there somewhere, because I occasionally have problems migrating the database on Heroku.

like image 397
niftygrifty Avatar asked Nov 21 '13 11:11

niftygrifty


People also ask

Can you delete migration files in Rails?

After working on a Rails project for months, it's not unusual to have hundreds of migration files in the db/migrate folder. Turns out, you can safely delete the ones that already ran in production, keeping your codebase small.

Where does rails store migration data?

Rails stores the most recent database schema in the file db/schema. rb . This file is the Ruby representation of all the migrations run on your database over the life of the application.


2 Answers

You don't need to keep around your old migration files in a Rails app, because your database schema should be captured either in schema.rb or an equivalent SQL file that can be used to regenerate your schema.

Migrations are not the authoritative source for your database schema. That role falls to either db/schema.rb or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.

There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema, which is in schema.rb or the SQL file.
This file should be versioned and kept in source control.

To set up automatic schema.rb generation, modify config/application.rb by the config.active_record.schema_format setting, which may be :ruby or :sql. If :ruby is selected then the schema is stored in db/schema.rb. If :sql is selected, the schema is dumped out in native SQL format of your database.

like image 155
Zack Xu Avatar answered Sep 22 '22 07:09

Zack Xu


You can delete your old migrations. After you have done this, when you are setting up your app you will need to run:

rake db:schema:load

Instead of:

rake db:migrate
like image 26
Jeremy Lynch Avatar answered Sep 18 '22 07:09

Jeremy Lynch