Lost my schema.rb! Can it be regenerated?

Due to some deployment issues I stopped tracking schema.rb in git. Somehow I have stuffed this up and somewhere along the way my schema.rb file has disappeared.

Is there a way of regenerating schema.rb from the database or from the migrations? I would prefer not to lose the existing data.


Solution 1:

If you run a rake -T it will list all possible rake tasks for your Rails project. One of them is db:schema:dump which will recreate the schema.rb for the Rails app from the database.

bundle exec rake db:schema:dump

Solution 2:

Careful,

rake db:schema:dump

will dump the current DB schema FROM the DB. This means that if you made any changes to your migrations, they will NOT be reflected in schema.rb file which is not what you want IMO.

If you want to re-create the schema from the migrations, do the following:

rake db:drop  # ERASES THE DATABASE !!!! 
rake db:create
rake db:migrate