1

We maintain a large project with many migrations dating back about 4 years.

Ideally we would like to delete all the old migrations and copy the schema contents into an initial_migration.rb file.

My question then, consists of three things:

  • Will this wipe out our clients databases?
  • If so, how do we persist the data?
  • Generally speaking what is the best way to go about this

Thanks in advance.

Matthew
  • 95
  • 6
  • 1
    I think you're overcomplicating this. You don't need to keep migrations around after they've been applied, everything you need is in `db/schema.rb` already: https://stackoverflow.com/a/47640821/479863, https://stackoverflow.com/a/6824773/479863, ... – mu is too short Jun 01 '19 at 03:54

2 Answers2

1

Here's how I would approach this:

  1. Back up the database in at least three places.
  2. Make a copy of a small slice of your database.
  3. Convert the copy into seed data (if this is confusing, let me know, and I will provide an example).
  4. Create a new rails app (rails new <appname>).
  5. Create one migration in your new app per table in your old database, with all the fields and formatting you want.
  6. Migrate your new database.
  7. Add at least one ! onto the create method for each table type in your seeds, i.e. Student.create!(name:'Jane', email: 'jane@edu.edu'), Teacher.create!('...). This will cause ActiveRecord to give you a more verbose error if the data is not persisted to the table.
  8. Run rails db:seed.
  9. When you have that working, repeat the test with a much larger chunk of the database, or skip to trying the whole thing.

Swapping out the databases depends a lot on how you are deployed and I'm not an expert in that area. You should consult with the persons responsible in your org, or you could try reading up here Application Migration Best Practices.

Abe
  • 4,500
  • 2
  • 11
  • 25
0

I think first of all you should backup your databases.

For your current environments, I think there's no need to run your new migration, so, it should have timestamp less than your current schema version.

If you want to run that new migration file on other environments, I think you can. From my opinion, you will have another database with the same structure. Or, you can just make copy of current one for those environments.

quyetdc
  • 1,485
  • 1
  • 14
  • 24