Database Migration Route — Using this power in the Django Universe!

Anderson Magalhaes
3 min readAug 25, 2023


Hi, there! My name is Anderson, I have working as Software Engineer for over 15 years and I’m passionate about this.

Note: this article is just one type of approach. We have other ways to accomplish this separation of responsibility, but it makes sense for us to discuss manageable migration solutions, pros and cons.

Welcome, web development heroes and heroines, to an exciting journey through the world of migrations in the Django Universe! Just as the Avengers unite to face cosmic threats, we unite models and migrations to create powerful applications.

In this article, you’ll learn how to dance gracefully on the migrations stage using an approach even Iron Man would approve of!

Assembling the Model Squad

Before we dive into the epic battle of migrations, we need to assemble our model squad, the true superheroes of Django. Each model is like a character with unique abilities, representing tables in our database. Prepare for an Assembly of Models!

The Foolproof Plan

Much like Tony Stark meticulously plans every detail of his Iron Man suit, you’ll craft your migrations with precision. With a magical touch, you’ll effortlessly create automatic schemas using the makemigrations command, orchestrating the perfect choreography. Execute them like a true Django Sorcerer Supreme!

python makemigrations

After this we will send this change to production environment and wait the best moment to change our database.

The Saga of the Migration Route

Our story continues, and now we find ourselves in a parallel dimension: the Migration Route! Imagine this route as Doctor Strange’s own Portal of Travel, leading us to a realm where migrations await the proper execution.

Unleashing the Spell

Now is the time to conjure our spell: creating a route to execute migrations only when appropriate. But remember, don’t let any code Thanos access this! Let’s create the magical route that only responds to heroes from the staff team.

The Decisive Battle

The climax of our saga has arrived. With the route created, we need to define what happens when our heroes visit it. Use the legendary execute_from_command_line to perform migrations with confidence. Only a true Django Guardian of the Galaxy could pull this off!

from django.http import HttpResponse, HttpResponseForbidden
from import execute_from_command_line

def perform_migration(request):
if request.user.is_staff:
# Execute migrations with the might of a Marvel hero
execute_from_command_line(['', 'migrate'])
return HttpResponse("Migrations performed with the Avengers' power!")
except Exception as e:
return HttpResponse(f"Oops, something went wrong!", status=500)
return HttpResponseForbidden("Only VIP heroes can perform migrations!")

The End… Or Is It a New Beginning?

Just like Marvel movies, our journey comes to an end, yet the cycle continues. Now you wield mastery over migrations with Marvel style, using a special route to execute migrations as needed, keeping your apps aligned like the Infinity Stones.

Embrace the unknown, explore new features, and create something extraordinary with Django, following the example of the greatest heroes of our time!

Advantages of using this approach

  • Explicit Control: By separating migrations into a dedicated route, you gain explicit control over when migrations are executed. This allows you to avoid unexpected database updates during the CI/CD flow.
  • Responsibility Isolation: Keeping migrations in a separate route helps isolate database update tasks from other parts of the application. This simplifies the understanding of data flow and prevents concurrency issues.
  • Security: Having a dedicated route for migrations can be more secure, as you can enforce stricter access controls on this route, ensuring that only authorized individuals can perform migrations.
  • Auditing and Logging: When performing migrations through a dedicated route, you can implement a detailed log of the migrations carried out. This can be valuable for audit purposes and for tracking the history of database updates.
  • Flexibility: If you need to make adjustments to migrations or test them individually, having a dedicated route allows you to do so without affecting other parts of the application.


In this epic adventure through Django migrations, we drew inspiration from Marvel heroes to craft applications that are true technological gems. Always remember to safeguard your routes with proper defenses and bestow the right powers upon the right heroes.

Thank you for you attention. See you!




Anderson Magalhaes

Software Engineer | Python | NodeJS | FastAPI | Django | React | Typescript | AWS CLF-C02 | Fullstack Developer