Report this

What is the reason for this report?

How to migrate a new app to an existing Django project (Droplet)?

Posted on June 22, 2021

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!

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.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.