1

I am trying to collapse all of my migrations into one (just to try it out), using this blog advice: I personally prefer to collapse all the existing migrations into a single migration at the end of every release, by just copying the schema.rb into "001_collapsed_schema.rb". This way, you won't have these older migrations which need to be "maintained".

So I delete all my migrations, create a new migration file 001_collapsed_schema.rb and copy schema.rb into it. Then I try to run

rake db:migrate

and get this NameError: uninitialized constant CollapsedSchema error. What did I do wrong?

Evgenia Karunus
  • 10,715
  • 5
  • 56
  • 70

1 Answers1

0

I don't think you are supposed to copy your schema.rb into your migration file but rather merge all different migration files into one.

This has the benefit of less migration files and quicker preparation of fresh DBs in the future (e.g. instead of having to go through 1000 migration files you go only through 100).

Maybe the author of the blog you mentioned is doing that as a way to keep different versions of her schema.rb, in which case you cannot execute them as migrations but rather rollback to them, if needed, by replacing your schema.rb with them and issuing:

rake db:schema:load # deletes all DB data!

See also the discussion in this relevant SO question: rake db:schema:load vs. migrations

Community
  • 1
  • 1
Kostas Rousis
  • 5,918
  • 1
  • 33
  • 38
  • so you can just delete all the migrations and run rake `db:schema:load` and that will delete all the db data but leave you with all the proper tables? – Evgenia Karunus May 18 '14 at 10:53
  • 1
    Yes that's right. Works well for bringing new servers up to date fast. Maybe for dev and test envs too, depending on your needs. But when you have production data you need migrations to, well, migrate your data one step at a time. And reverse these in case something goes wrong! Don't put too much effort in getting rid of all migrations, they serve their purpose! – Kostas Rousis May 18 '14 at 11:12
  • This is not good advice. It's better to collapse them all, it's the database operations that take a long time, not the number of files. Plus, if you have any references to classes in your migrations (a bad practice, but sometimes necessary), they can start failing if you rename/delete/refactor those classes. Better to collapse to a schema, safer and faster. – thewoolleyman Feb 20 '17 at 23:28