How to delete migration files in Rails 3

前端 未结 11 1048
盖世英雄少女心
盖世英雄少女心 2020-12-07 07:54

I would like to remove/delete a migration file. How would I go about doing that? I know there are similar questions on here but as an update, is there a better way than doin

相关标签:
11条回答
  • 2020-12-07 08:49

    Another way to delete the migration:

    $ rails d migration SameMigrationNameAsUsedToGenerate
    

    Use it before rake db:migrate is executed because changes in database will stay forever :) - or remove changes in Database manually

    0 讨论(0)
  • 2020-12-07 08:49

    Run below commands from app's home directory:

    1. rake db:migrate:down VERSION="20140311142212" (here version is the timestamp prepended by rails when migration was created. This action will revert DB changes due to this migration)

    2. Run "rails destroy migration migration_name" (migration_name is the one use chose while creating migration. Remove "timestamp_" from your migration file name to get it)

    0 讨论(0)
  • 2020-12-07 08:50

    You can also run a down migration like so:

    rake db:migrate:down VERSION=versionnumber
    

    Refer to the Ruby on Rails guide on migrations for more info.

    0 讨论(0)
  • 2020-12-07 08:52

    This also works in Rails 5.

    If the migration was the most recent one you can remove the database column(s) that the migration added by doing:

    rake db:rollback
    

    then remove the migration file itself by running:

    rails d migration WhateverYourMigrationWasNamed.rb 
    
    0 讨论(0)
  • 2020-12-07 08:54

    None of these answers quite fit the problem i had as the migration i wanted to delete was missing: I had created and run a migration in some other branch, which was then discarded. The problem is when a migration is run, rails adds the version into a schema_migrations table in the database. So even if it isn't listed in your db structure or schema, rails looks for it. You can reveal these orphaned migrations by running:

    rails db:migrate:status

    Note the versions of the missing migrations and head into the db console:

    rails dbconsole

    Now remove the versions from the migration table manually:

    delete from schema_migrations where version='<version>';

    You should now be good.

    0 讨论(0)
提交回复
热议问题