Skip to content

Instantly share code, notes, and snippets.

@camara90100
Created March 21, 2022 08:28
Show Gist options
  • Select an option

  • Save camara90100/ba035d071033b1b32aa61f48ef598035 to your computer and use it in GitHub Desktop.

Select an option

Save camara90100/ba035d071033b1b32aa61f48ef598035 to your computer and use it in GitHub Desktop.
Remove (squash) Django Migrations without worrying about dependencies.
### Remote db state must be at the same exact state of the local DB
### Steps will be done on two stages:
### 1. local db stage
1. make sure you are in your django project path, and run `find . -path "*migrations*" -name "*.py" -not -path "*__init__*" -exec rm {} \;`
2. pip install --upgrade --force-reinstall Django, because this path usually removes django's own migrations.
3. Remove django migrations history table from your local database.
from django.db import connections
with connections["default"].cursor() as cursor:
cursor.execute("TRUNCATE django_migrations")
4. run make migrations
5. run migrate --fake
6. commit the new migrations files and the deleted migrations files, and don't be alarmed but the number of files deleted.
### 2. Server's db stage
1. if you have a managed server, like Elastic Beanstalk, disable the automated migrate command from your server config.. and don't allow it to run.
2. push your code.
3. run manage.py --fake manually on the server.
4. do a little dance or get a drink or something.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment