1

My migration files for my Rails Heroku-hosted project had gotten very messy, so I wanted to consolidate them. I figured I could:

  1. do a Heroku database backup with heroku pg:backups:capture --app appname,

  2. add a new migration file dropping all my tables and run it on development and production,

  3. delete all my migration files and add new, neater migration files with all the information in my old schema.rb, and run them on development and production, and then

  4. restore my Heroku database with heroku pg:backups:restore b001 DATABASE_URL --app appname.

This is exactly what I did. I assumed a database restore would simply populate my current database with the records in the backed up database. However, what actually happens is that it replaces the entire current database with the old one. This is a problem because my existing migration files are now all viewed as new, and they can't be run because the tables they are meant to create already exist. This means I can't add any new migrations, because the existing ones can't be run. I could just delete all my existing migration files, but that seems like extremely bad practice.

Is the situation salvagable? Ideally I'd just run my migrations and populate the database with the records from my backed up database - is that possible?

GMB
  • 216,147
  • 25
  • 84
  • 135
Joe Morano
  • 1,715
  • 10
  • 50
  • 114
  • Migration files don't really matter, maybe just delete them all. Once you delete all the files then you can create new ones and it wont try to run old stuff. Reference this: https://stackoverflow.com/questions/20119391/delete-old-migrations-files-in-a-rails-app – ricks Nov 22 '19 at 22:41
  • I see no benefit on "cleaning" the migrations, migrations shows you the history of your database, you are loosing information when you refactor the migrations. For a clean database schema you already have db/schema.rb, you can run `rails db:schema:load` to load the schema without running migrations. I wouldn't refactor the migrations. – arieljuod Nov 23 '19 at 17:44

0 Answers0