Why do I want to return the migration?

Rails uses the default method to migrate migration down. In which scenario would I ever want to return to migration, though?

Some thoughts:

In the development or production process, I always have a snapshot of my database to return before I even start the migration. Especially for migrations that perform data conversion, in most cases I find that snapshot recovery is even faster than the migration return. (So ​​I would never do it in a hurry!)

If the migration failed, it will either:

  • failure during an exception in a non-transactional database and thus leaving the database broken or
  • transaction exception and rollback fail, and therefore there is no need to return otherwise.

If the changes made (or at the end of development), and later become an error, I would correct my error with a new migration. I would not give up the old. In development, I will simply remove the migration.

I also found that the down method introduces additional code that I repeat in, and therefore may introduce new errors. This is contrary to the DRY principle.

So, I'm curious what professionals are, because I can’t come up with.

+5
source share
4 answers

In development, it is easy and quick to gradually “improve” migrations using the down method automatically. For example,

  • Create a migration and go to it
  • , .
  • ver , db: migrate with version
  • /

. , , "" . db,

:

, . , . , .

- , db .

+4

"", DB, , . . , :

  • "resubmitted", .
  • , , int

, 10 15 , , , dev /, . , , , .

:

  • 1 Dev DB. .
  • 1
  • 2 Dev DB. .
  • 2
  • 2 , , , " ", () , , db: migrate, .
+2

. , rake db:migrate VERSION=0, . , , , , .

Because of this paranoia, I add the following to all my methods down.

  def self.down
    if Rails.env.production?
      raise ActiveRecord::IrreversibleMigration
    else
      drop_table :foo_bars
    end 
  end 

Thus, he is still working in development, but I cannot accidentally destroy my production database from orbit, while half asleep at 2:00 in the morning.

+2
source

If the migration did not have the expected result, it is better to drop it and rewrite it than to save the failed migration in the code.

0
source

All Articles