0

stack: Django/Docker/Postgresql

I have made some changes in database models last month and deployed in preprod.

- remove fields 
- add fields
- alter one field constraint

All seems to be correct, changes were applied and app was running.

Yesterday, I've made some minor changes and re-deployed but when I re-build my project, new migrations are detected. These migrations are exactly the same as above. And migrate failed because trying to remove a field that did not exist anymore.

Django app update procedure:

- sudo docker-compose -f docker-compose.preprod.yml down -v
- git pull
- sudo docker-compose -f docker-compose.preprod.yml up -d --build --remove-orphans #<= error raise
- sudo docker-compose -f docker-compose.preprod.yml up

entrypoint.sh

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi

python manage.py makemigrations --noinput
python manage.py migrate

exec "$@"

1 Answer 1

0

Your entrypoint must absolutely not have makemigrations in it. If you have been running with this in production, you may be screwed (i.e. you'll have migrations in your production database that are to be found nowhere else).

makemigrations must only be run at development time, and those migrations must be committed to source control.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for replying. I did not deployed in prod so I can make changes before
I recommend you to look at your production's django_migrations table and compare it to your development version's copy. If there are migrations in one that aren't in the other, you may be in trouble.
I have checked, and 3 migrations are applied in dev database that are not in prod database because I did not push migrations files ; So if I commit theses 3 migration files, pull and re-build my app it should be ok right?
Well, should be ok so long as you end up in a situation where your prod and dev database have the same migrations – and you can also compare the current SQL schemas to make sure the actual tables are the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.