Question

How to add a new app to an existing Django project in production (Droplet platform)?

I need to add a new app to an existing Django project that has been running for 6 months for production use. What is the best way I can migrate the app’s model to create a table in the managed database that is connected to this project?

This Django project was installed in a Ubuntu 20 Droplet following these tutorials: 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.


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 4, 2023

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:

Pre-Migration Checklist:

  1. Backup Database: Always start by creating a backup of your production database.
  2. Version Control: Ensure that all your changes are committed to version control (e.g., git).
  3. Test Locally: Apply the migrations on your local development environment first and test thoroughly.
  4. Staging Environment: If possible, apply the migrations to a staging environment that mirrors production.
  5. Announce Downtime: If downtime is expected, communicate this to your users in advance.
  6. Read-Only Mode: Consider putting your application in a read-only mode during the migration if the changes are significant.

Migration Steps:

  1. Add the New App: Update your settings.py to include the new app in the INSTALLED_APPS list.

  2. Create Migration Files: Generate the migration files for your new app/models by running:

    python manage.py makemigrations your_new_app
    
  3. Review Migration Files: Check the migration files to ensure they accurately represent the changes you want to apply to the database.

  4. Prepare the Production Environment:

    • Update your production code by pulling the latest changes from your version control system.
    • Install any new dependencies or update the existing ones as required by your new app.
  5. 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.

  6. Monitor Logs: Keep an eye on your server logs for any errors or warnings that may arise during the migration.

  7. Post-Deployment Checks:

    • Ensure that your Django application is running correctly.
    • Verify that the new tables have been created successfully and the schema is correct.
    • If you have a monitoring system in place, check for any alerts.
  8. 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.

Tips for Minimizing Downtime:

  • Zero-Downtime Migrations: If possible, structure your migrations to be backward-compatible, which may involve several phased releases.
  • Feature Toggles: Use feature toggles to enable new features only after confirming the migrations are successful.
  • Blue-Green Deployment: Consider blue-green deployments to switch traffic to a new production environment with the latest changes applied.

Using Nginx to Minimize Impact:

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel