What is the best way to resolve Rails orphaned migrations?

后端 未结 10 977
陌清茗
陌清茗 2020-12-22 22:29

I have been switching between branches in a project and each of them have different migrations... This is the scenario:

$ rake db:migrate:status

<
10条回答
  •  别那么骄傲
    2020-12-22 23:32

    Migrations are stored in your database. If you want to remove the abandoned migrations, remove them from the db.

    Example for Postgres:

    1. Open psql:

      psql
      
    2. Connect to your db:

      \c your_database
      
    3. If you're curious, display schema_migrations:

      SELECT * FROM schema_migrations;
      
    4. If you're curious, check if the abandoned migrations are present:

      SELECT version FROM schema_migrations WHERE version IN 
      ('20130320144219', '20130320161939', '20130320184628', '20130403190042',
       '20130403195300', '20130403214000', '20130410194222');
      
    5. Delete them:

      DELETE FROM schema_migrations WHERE version IN ();
      

    Now if you run bundle exec rake db:migrate:status, you'll see the orphaned migrations have been successfully removed.

提交回复
热议问题