Handle Production Migrations and Development Migrations in Django - django

While developing a Django project, all your migrations are stored within each app folder, however, in production I don't want those migrations, I want to keep a Production database, and a Development database:
How do I handle Django migrations in a Production and Development environment?
I'm asking this question because it's been really hard to update my deployed project with new additions in the development one, my ideal scenario would be to keep each set of migrations in a folder outside my source code, just like the databases.

The best idea is to keep production and development migrations the same and while developing you clean migrations before pushing the code and you should push migrations into your Version Control System too.
In development, you might end up deleting a table and re-creating it so make sure you don't push the un-intended migrations. The thing is you should treat migrations as code, not an automated script. I have done a lot of mistakes in the past, so, I came to the conclusion of including migrations in code. and that's effective and gives more control.
Moreover you might have to do data migrations in production, how will you do if you wont push the code?

Related

Django Migrations Tree Issues - Deployment PythonAnywhere

I have been having this issue when managing Django migrations on deployment, and I would like to know what approach should I take:
I am developing an application using Django, and I am using PythonAnywhere to deploy the web app. I am using SQLite as the database. I understand the Django migrations work like a tree or a sequence (001, 002).
Every time I make a change in a field locally, it works fine because the tree has been saved and the sequence has not changed. But when deploying the changes through GitHub (having deployed the web app and calling the migrations and migrate command, which creates another migrations files and sequence), I usually get an error indicating that the migration tree is broken. So I have to go to the app's migration folder, delete them, and call the migrations and migrate commands again.
This is causing me a lot of problems, as I do not want to mess with schema of the database and lose the information.
It is just me or anyone else is having this issue the migrations tree, not only on PythonAnywhere but on others servers?
Thank you!
Thank you guys! #Ankit Tiwari, not I did not as the Django documentation says that it is important to keep the migrations files on deployment as #caseneuve recommended. Even thought I read somewhere that on the server side is not necessary to call the makemigrations command, just the migrate as the migrations files already exist; so I try it and so far it is not giving me more errors. Thanks for the answers.

Run reverse Django migration on Heroku after release failure

I have a running Django application on Heroku with migrations auto-run on release. While in most times this works fine sometimes there is a problem when:
There are more than one migration in given release (they can be in different apps)
Some migration will fail, but not the first one
In this case manage.py migrate will fail so Heroku will not finish the release and will not deploy the new code. This means that code is in the old version and the database is in the state "somewhere between old and new".
Is there a simple way to autorun Django run reversed migrations in case of the failure of the release command on Heroku?
Transactions won't help here as there might be more than one migration (multiple apps) and Django run each migration in seperate transaction.
As I couldn't find any existing solutions I am posting a gist I have written to solve this.
https://gist.github.com/pax0r/0591855e73b9892c28d3e3cdd15f4985
The code stores the state of migrations before running the migration and in case of any exception reverts back to this state. It also checks if all migrations are reversible during the migrate step.
It's not yet well tested, but I will work forward to create a library from it for easier use by others.

Difficulty with Django Migrations

What are some good best practices to prevent the following.
Often, during django development I use postgres. If I roll back to an earlier commit, often new migrations will break things irreparably, because the postgres context is unknown to the older code. I have tried the syncdb etc, I have tried makemigrations on single apps, but neither seem to fix the problem.
Also, if creating a new postgres db and deleting all the migrations and pycache folders, this too often causes problems that are not straightforward to fix.
Is the answer to simply use sqlite during development, has anyone else encountered this?
Details: django 2.0.5 postgres: most current.

How to update database (postgresql) of a deployed django-app, after modifying a model?

I'm having trouble with my Django-app that's been deployed.
It was working fine but I had to do a minor modification (augmented the max_length) of a Charfield of some model. I did migrations and everything was working fine in the local version.
Then I commited the changes without a problem and the mentioned field of the web version now accepts more characters, as expected, but whenever I click the save button a Server Error rises.
I assume I have to do some kind of migration/DB update for the web version but I don't seem to find how.
(I'm working with Django 1.11, postgresql 9.6, and DigitalOcean).
EDIT
I've just realized that the 'minor modification' also included a field deletion in the model.
Short answer
You have to run
python manage.py migrate
on the server, too. Before you do that, make sure all migration scripts you have locally are also present on the server.
Explanation
After changing the model, you probably locally ran
python manage.py makemigrations
This creates migration scripts that'll transform database schema accordingly. Hopefully, you've committed these newly created scripts to Git, together with the changed model. (If not, you can still do so now.)
after running makemigrations (either before or after committing, that shouldn't matter), you've probably locally ran
python manage.py migrate
This applies the migration scripts to the database that haven't been applied to it, yet. (The information which ones have already been applied is stored in the database itself.)
You probably (and hopefully) haven't checked in your local database into Git, so when you pushed your tracked changes to a remote repo and pulled them down on your server (or however else the new Git revisions got there), the changes to the server database haven't happened, yet. So you have to repeat the last local step (migrate) on the server.
Further reading
For more information, refer to the Django 1.11 documentation w.r.t. migrations. (You can e.g. limit migration creation or migration application to a single Django app, instead of the whole Django project.) To get the grip of these things, I can recomment the free Django Girls tutorial.

Should we re-use the migration scripts created by the dev server on the production server?

I am using South for database migration for a Django project. And I was wondering if it is a good idea to commit the migration scripts that were generated by my dev server to the repository & then reusing it on the production server?
Yes. That is the point of migrations to allow you to develop and test database schema changes and then deploy in test and production in a reproducible way.
I wouldn't consider it reuse so much as developed and tested in development and then deployed on production.
Make sure you also develop and test backward migrations to ensure you can retreat.