E
E
EdvardYoch2021-03-29 22:58:19
Python
EdvardYoch, 2021-03-29 22:58:19

Why doesn't the python manage.py migrate command exit after running and return control to the console?

I am trying to solve the following problem. There is an application on Django and I need to implement the launch of this application through Docker. As a database, we connect a container with PostgreSQL or MySQL. When running through docker-compose, you need to make sure that Django creates the necessary tables in the database before starting the Django application. To do this, in the docker-compose.yml file, in the comand attribute, before the server start command, we write the python manage.py migrate command. This command creates all the necessary tables, but after execution, the process does not end and, in fact, does not allow the next command to run.

some lines....
web_1  |   Applying datapage.0001_initial... OK
web_1  |   Applying datapage.0002_auto_20190709_1955... OK
web_1  |   Applying datapage.0003_auto_20190713_1358... OK
web_1  |   Applying datapage.0004_auto_20190720_1107... OK
web_1  |   Applying sessions.0001_initial... OK


Moreover, if we use the following approach: We get the same thing:

docker-compose exec web python manage.py migrate


some lines....
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying datapage.0001_initial... OK
  Applying datapage.0002_auto_20190709_1955... OK
  Applying datapage.0003_auto_20190713_1358... OK
  Applying datapage.0004_auto_20190720_1107... OK
  Applying sessions.0001_initial... OK


By pressing Cntrl + C, we get the following error:

^CException ignored in: <module 'threading' from '/usr/local/lib/python3.7/threading.py'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/threading.py", line 1307, in _shutdown
    lock.acquire()
KeyboardInterrupt


The docker-compose.yml file configuration is as follows:

version: '3'

services:
  web:
    build: .
    command: >
      bash -c "
            sleep 40 &&
            python manage.py migrate --noinput &&
            python manage.py runserver 0.0.0.0:8000 --noreload"
    
    ports:
      - '8000:8000'
    depends_on:
      - db
  db:
    image: mysql:5.7
    ports:
      - '3306:3306'
    environment:
      MYSQL_DATABASE: 'db_django'
      MYSQL_ROOT_PASSWORD: 'password'
    restart: always


It is not necessary to use python manage.py makemigrations in this case, since this is the first initiation of tables in the database, but if we apply, we get the same problem as with python manage.py migrate, that is, after execution it does not give control to the console:

No changes detected
(Жмём Cntrl+C)
^CException ignored in: <module 'threading' from '/usr/local/lib/python3.7/threading.py'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/threading.py", line 1307, in _shutdown
    lock.acquire()
KeyboardInterrupt


We get this behavior when working with PostgreSQL and MySQL both through docker and locally installed, but when working with SQLite, this command behaves correctly, that is, after completion it returns to the console. I also left this command to be executed at night to make sure that it was hanging, and not being executed for a long time, as a result of looking in the morning, this command was still working.

When changing versions between python 3.6-3.9 and Django 2.1-2.2 the situation did not change.

3 hours of “google” in Russian and in English did not give any results, the only embarrassing thing is that other people used the same approach with the python manage.py migrate command in the docker-compose.yml file, and it seems that they do not have such a problem arose. But after trying to run a friend on his machine, got the same error.

Actually, I want to know why python manage.py migrate gives this behavior, maybe it's a Django ORM bug or something. And also, how can I fix this, or what alternative can I use so that the migrations are performed through docker-compose.yml, and not prescribed separately after running the containers through docker-compose exec.

I also attach the source code of this application on GIthub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexbprofit, 2021-03-30
@alexbprofit

here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question