What is the procedure for completely uninstalling a Django app, complete with database removal?
What is the procedure for completely uninstalling a Django app, complete with database removal?
In my context the projects exists several times: I have a development system, some team mates have a development system, there is a staging system for the customer and a production system. This means I don't want to execute sql commands by hand. I want it to be automated.
Goal: Remove the app and all database tables.
remove all files from the app, except the folder "migrations"
Execute this command:
python manage.py makemigrations -n drop_all_tables my_app_to_remove
The directory looks now like this:
my_app_to_remove/
my_app_to_remove/__init__.py
my_app_to_remove/migrations
my_app_to_remove/migrations/0001_initial.py
my_app_to_remove/migrations/....
my_app_to_remove/migrations/0030_drop_all_tables.py
my_app_to_remove/migrations/__init__.py
Leave my_app_to_remove in the file "settings.py".
Update all projects. Tell the team mates to update their project and to run the migrations.
Now remove "my_app_to_remove" from settings.py and deploy again.
As a first step, prevent any usage of the app models and deploy. This is essential for rolling deployment. Check notes below.
Then you have two options.
Option 1
python manage.py migrate <app_name> zero. This will revert all the migrations for the app and clean the django_migrations tableINSTALLED_APPS, urls.py, etc.)python manage.py migrate)Option 2
INSTALLED_APPS, urls.py, etc.)django_migrations tableThis answer is thinking in an automated rolling deployment context (e.g. Heroku).
Manual means it normally cannot be done in automated deployment. Deploy basically refers to python manage.py migrate which is normally done in automated deployment.
Notes
settings.py (but keep it in INSTALLED_APPS so we can run migrations), urls.py, etc.migrate <app_name> zero will revert all migrations depending on those migrations. So be careful if your other apps migrations depend on these migrations.References
Safely Remove a Django app by Jordan Haines
To completely remove a Django app (with models), follow the steps below. Let’s pretend we’re removing an app called note_app:
note_app.models. You may need to also remove entries from note_app.admin file if you registered your note_app models with Django admin.makemigrations. This should create a note_app migration that deletes all of the models you just commended out. This migration should also remove fields referring to note_app models from models in other apps.Git Tip: Commit your migrations, take note of the commit, and then create a separate commit that deletes the note_app directory. When you are ready to apply your changes in a staging or production environment, checkout the commit you noted, run the migration, and then checkout the latest commit to delete the app directory. Your first commit should still have note_app in INSTALLED_APPS.note_app from your INSTALLED_APPS setting.And that’s really about it…super easy :)