I've several dozens Rails DB migrations that have been written more than a year. It is possible to method to aggregate these to one migration to ensure that I'll just visit a full DDL statement for that database because it is available now? I simply require the current snaphot without all of the good reputation for the way we reached it.

You can just load the present schema in to the DB.

rake db:schema:load RAILS_ENV=[production, test, etc.]

This can go ahead and take schema.rb file's version from the schema, and load it in to the DB without running individual migrations.

NOTE: for those who have migrations that put data in to the DB (e.g. default values, for instance), that data won't be put into the DB.

If you want to load default values to your DB, that could be better done using a custom rake task, separate from migrations.

It's possible, but most likely not recommended to aggregate the migrations!

Maybe request:

  • Why would you like to do that?
  • How frequently do you want emigrate completely to VERSION= after which back again?
  • Is one thing really damaged? (otherwise, then don't repair it)

I have had exactly the same problem once.. I wound up just re-ordering my migrations, because alterations in the schema triggered it not to properly migrate up/lower any longer. I'd be reluctant to achieve that again.

For those who have migrations which just add fields or indexes, maybe you are able to mix all of them with the primary migration for that model -- but beware that you simply can't reproduce old situations any longer, e.g. older DB-dumps might not be suitable for what migration number they must be suitable for -- that's most likely the greatest argument against aggregating...

Technically, you are able to dump the schema after which load it directly - that's one of the ways:

rake db:schema:dump

then produce a single new migration using the items in the schema dump file db/schema.rb

Here are a few similar questions:


P.S.: I discovered it helpful to stay using the old migration numbering plan, in which the migrations don't use timestamps - for me personally this works more effectively (is simpler to determine by which order they're).

e.g. inside your config/application.rb file:

config.active_record.timestamped_migrations = false

You shouldn't be utilising all of the migrations to obtain a database ready to go. The present schema.rb is definitely exactly what the DB appears like 'presently'.

It is good practice to periodically just truncate your migrations for those who have a lot of them inside. We finally did that and among our bigger programs, getting rid of a great 50 migrations in the folder since the only factor that means something is schema.rb. Migrations are simply that, a method to migrate making changes for an existing condition from the database. They ought to only ever need to be run once.