Squashing multiple South migrations into one migration

后端 未结 3 1056
慢半拍i
慢半拍i 2021-02-10 14:09

During development I created many migrations, often going back and forth about how I wanted to implement something.

Now it is time to push it to production, but I am get

3条回答
  •  野的像风
    2021-02-10 14:44

    It's possible, and I've done it myself a few times. You basically have two methods:

    1) This is by far the easiest and preferred method. Simply, rollback your dev database to right before the first migration you want to include in the "squash", then delete all the migrations from that one on. Finally, re-run the schemamigration management command. You'll end up with all the migrations necessary all in one file.

    2) If for some reason you can't do #1, it's still technically possible, but will be much more exacting of a procedure. Literally copy and paste the contents of the forwards and backwards methods of each migration into one migration file (your lowest numbered one). That part is easy enough, but you'll then have to also make manual modifications to the frozen models at the bottom of the file. I've found the best way is to simply try to run this new migration. You'll likely get errors about such-and-such model has no attribute such-and-such. That's a signal to go add that field to the frozen model. When the migration runs without errors, you're golden.

    Note: Remember when you delete those old migration files, delete them with the any versioning system you're using in mind. In other words, use git rm or svn rm, etc. If you end up with those old migrations tagging along in your release, you'll bork your production instance. For my part, I never commit migrations until I'm ready to release my changes. You can always regenerate the migration if you lose it.

提交回复
热议问题