Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>TL;DR</strong>:</p> <ul> <li>If the change is not in production yet, you can usually get away with modifying migrations.</li> <li>It's best practice to keep migration files as clean and limited in number and scope as possible.</li> <li>If the change is already in production or would make life difficult for other devs if you modified it, then add additional migrations to fix the errant previous migrations.</li> <li>You can be a little looser with these guidelines if you're only <em>adding</em> functionality (the ability to reverse the migration), not changing what the migration would originally have done.</li> </ul> <p><strong>Full answer:</strong></p> <p>If you haven't pushed anything to the remote/central repository yet, you can do whatever you want with respect to your new changes, since it only affects you. Just change the database and migration files to suit your needs. And in fact, it's <em>best practice</em> to change the migration files here rather than cluttering the source base with unnecessary migrations.</p> <p>If you have pushed to remote but you <em>know for certain</em> that nobody else has pulled from that branch yet (if for example, you are the only user of that branch, or if it's a small team and you've communicated with all members), again you can do whatever you want with the recent changes, including migrations.</p> <p>Now, if someone else has pulled, but the change is not live yet, you can still usually do whatever you want if the team is not too large. Just be aware that your changing an already-committed migration file could cause another developer an annoying development environment re-bootstrap if they're not aware of what you're doing, so tell them exactly what they need to do to get their dev environment back in sync. So just communicate clearly with the rest of the team in this case.</p> <p>Finally, what if the migration has already been pulled into production and run (or if the development team is too large to efficiently coordinate changing migrations with the other devs)? In this case, it's usually better to write a new migration to reverse the errant one. There are rare cases where a good cowboy programmer can get away with manually editing already-run migrations in production (and where that might even be a good idea), but you have to be very careful in that case because you're playing with fire (and have an easy revert mechanism if you screw up).</p> <p>In any case, if you're testing thoroughly, it should be very rare for a bad migration to make it all the way to production. It should be unusual for a bad migration to even make it to the central repository (in any branch that more than one dev is working on, that is).</p> <p>With that out of the way, let's say following the above guidelines you've already decided it's okay to change your migrations. Then, to answer your specific questions:</p> <blockquote> <p>Can I just change an applied migration to fix what is wrong and then re-run rake db:migrate?</p> </blockquote> <p>No, once a migration has been run, its id is stored in the database, and it won't be run again even if the file changes.</p> <blockquote> <p>Or do I have to modify the migration with up/down methods, revert the migration and then migrate again? </p> </blockquote> <p>You should modify the migration with up/down methods, yes. But as you already said, you can't revert the migration the official way. Instead you can either re-bootstrap your environment from an empty slate, or you can manually revert the migration using a sql queries (remember to also remove the associated id from the migrations table). Then, re-run the migration.</p> <blockquote> <p>Or maybe I shoud drop the table with a new migration and re-create it?</p> </blockquote> <p>Don't do this unless you are forced to (and even then be careful that when running it in production you don't inadvertently kill data during this process!).</p> <p>Finally, it should be noted that if the only change you're making to the migration file is splitting a "change" into "up/down" methods, and the "up" part is exactly the same after your modification, you can even get away with skirting the above guidelines a bit because it won't hurt other developers (they can now just rollback something they couldn't have rolled back before). But err on the side of caution in that case -- you don't want to screw up and accidentally change the up method here even slightly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload