In a migration, how to specify 'self.down' if self.up drops a table

Hi-

I no longer need a join table and want to drop it, but I want to be
able to recover if I’ve made a mistake. Is there an easy way to
capture the current state of the table and incorporate it into my
self.down?

Also, is there a way to flatten my migrations to one master? Like, to
say, “take the current state of my tables and commit them to one
migration?”

Thanks,
Dino

dino d. wrote:

Hi-

I no longer need a join table and want to drop it, but I want to be
able to recover if I’ve made a mistake. Is there an easy way to
capture the current state of the table and incorporate it into my
self.down?

Also, is there a way to flatten my migrations to one master? Like, to
say, “take the current state of my tables and commit them to one
migration?”

Thanks,
Dino

I use the annotate_models rake task (google it on the web) to keep my
model commented with the latest state of that table in the db. Your
“undo” on the down would be to simply create the table in its current
state.

Flattening your migration depends on whether you’ve gone live or not.
Cool in dev as long as you comunicate with your team, risky/inadvisable
in live.

You can certainly move the net of all your migrations into a single one,
but you need to be sure your schema version matches your last migration.

I do this periodically in dev, along the lines of:

I have 28 migrations, and the schema is settled (for now).

rake db:migrate VERSION=0

combine/edit my migrations into to the 1, 5, or 16 I want (you wanted
just one, which is equally possible. IIRC, Danimal does this as well.)

Once you’re set with your 1 migration:

rake db:migrate

Schema version 1 now has all your tables in one shot.

If version 1 goes live, and you have users data in your DB, that
migration becomes sacrosanct. You shouldn’t ever alter that migration,
but can add new migrations on top of it in dev, and tinker with as you
want. When it’s time to produce your uber migration for version 2, in
dev, rake to version 1, edit/combine migrations 2 through whatever into
just migration 2, then rake up again.

That can be safely deployed to live on top of your 001 migration.

I no longer need a join table and want to drop it, but I want to be
able to recover if I’ve made a mistake. Is there an easy way to
capture the current state of the table and incorporate it into my
self.down?

Look at db/schema.rb and find the table…

Also, is there a way to flatten my migrations to one master? Like, to
say, “take the current state of my tables and commit them to one
migration?”

again, db/schema.rb…

  • backup everything
  • be careful
  • wipe out all your migrations
  • make schema.rb your 001 migration
  • reset the schema_info.version field in the database.