0

I have a rather complex K8s environment that one of the Deployments is a Django application. Currently we are having a very hard time whenever I need to update a model that has already been migrated to a PostgreSQL database.

Let's say for instance that I create an application named Sample, that has a simple table on the models.py. My development process (skaffold) builds the docker and apply it locally on the minikube, after this is done I connect to the pod via kubectl exec and execute the python manage.py makemigrations and python manage.py migrate, so far so good.

After some time, let's say I need to create a new table on the models.py file of the Sample application, the skaffold builds the docker, kills the old pod, and create the new pod. So I connect as usual via kubectl exec and try to execute the makemigrations and migrate command, lo and behold, there's no migration to apply. And of course no change is made on the PostgreSQL.

Upon further searching this, I believe that the reason for this is that since the docker is built without the Sample/migrations folder, and there's already a table (the original one) on the PostgreSQL, when I run the makemigrations it creates only the 0001_initial.py file, that has all the tables but, since the table already exists, when executing the migratethe django believes that the migration is already applied, therefore it won't apply.

If what I found out is true, how can I keep this files on a PVC, so that they are always kept between each pod recreation?

Thank you.

Manual5355
  • 981
  • 10
  • 27
  • Maybe you forgot to commit your changes to the models.py and did add the new migration to your code repo. – Red Cricket Nov 26 '20 at 01:27
  • makemigration should be run before commiting your changes, you should only run python manage.py migrate at every models update – paltaa Nov 26 '20 at 02:23
  • @RedCricket, I haven't forgot to commit the changes, when I enter the pod I can see the models.py as inteded – Mateus Interciso Nov 26 '20 at 13:19
  • @paltaa, this can be a little cumbersome, since we are on a team of around 10 developers, each working on different apps, is there no other way? From what I understood I need to execute the makemigrations BEFORE creating the docker, right? And keep this on the version control, is that correct? – Mateus Interciso Nov 26 '20 at 13:20
  • Hello everyone, if this is the final answer (execute the makemigration before actually creating the docker), I'm going to have a very hard time orchestrating this with the other developers, specially when migrating to the production kubernetes. Is there any other way? – Mateus Interciso Dec 03 '20 at 18:09

0 Answers0