By lsidharta
I need to migrate an additional app/model to a running Django project to create a table in a PostgreSQL managed database. How to do this migration? What is the safest way to do this migration not to annoy the production system? Can anyone help me? FYI, the existing Django project runs in a Droplet with Ubuntu 20 and Nginx. The tutorials I followed to set up this project are https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04 and https://www.digitalocean.com/community/tutorials/how-to-set-up-a-scalable-django-app-with-digitalocean-managed-databases-and-spaces. Thank you.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
Migrating a new app or model within a Django project involves several steps, particularly when dealing with a production environment. The key is to ensure minimal downtime and to maintain data integrity. Here’s a general outline of steps to safely conduct the migration:
Add the New App: Update your settings.py to include the new app in the INSTALLED_APPS list.
Create Migration Files: Generate the migration files for your new app/models by running:
python manage.py makemigrations your_new_app
Review Migration Files: Check the migration files to ensure they accurately represent the changes you want to apply to the database.
Prepare the Production Environment:
Apply the Migration:
With your production environment updated, apply the migrations using:
python manage.py migrate your_new_app
You might want to do this during off-peak hours to minimize any potential impact.
Monitor Logs: Keep an eye on your server logs for any errors or warnings that may arise during the migration.
Post-Deployment Checks:
Perform Tests in Production: Although you’ve tested in development and staging, you should also perform some quick sanity checks in the production environment to ensure everything is working as expected.
While Nginx does not play a direct role in database migrations, you can use it to serve a maintenance page or to reroute traffic if necessary. You can configure Nginx to temporarily redirect traffic to a static maintenance page while you’re performing the database migration.
Also, ensure that you have a rollback plan in case anything goes wrong. This should include restoring from the database backup you took before starting the migration and rolling back your codebase to the previous stable state.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.