s3rius / FastAPI-template

Feature rich robust FastAPI template.
MIT License
1.79k stars 161 forks source link

alembic upgrade "head" Error. #175

Closed shpilevskiyevgeniy closed 9 months ago

shpilevskiyevgeniy commented 1 year ago

Error:

raise OSError(err, f'Connect call failed {address}') ConnectionRefusedError: [Errno 111] Connect call failed ('127.0.0.1', 5432)

s3rius commented 1 year ago

Hi, and thanks for raising this issue. How do you run your migrations?

shpilevskiyevgeniy commented 1 year ago

alembic revision and alembic upgrade "head"

s3rius commented 1 year ago

Are you sure that you have a database running on localhost? Can you connect to it?

shpilevskiyevgeniy commented 1 year ago

Yes, I can join. I am starting your project from scratch, adding a new model, what further commands do I need to do to migrate this model to the database? In the project description, this is the command: alembic upgrade "head" but it throws above error.

s3rius commented 1 year ago

This command only works if you have a running and accessible database.

You can check whether postgresql is running by trying to connecting to it directly using postgresql CLI.

psql -h localhost

This template doesn't deploy a database on your machine. But it generates docker-compose configuration that have a database inside.

Instead of running these commands in your shell, you can run them inside docker containers where database is 100% accessible.

docker compose run --rm -it --build -v $(pwd):/app/src/ api bash

This command will start a docker-compose and will create additional container with configuration as your api service and will spawn a shell here, so you can run commands. Or instead of opening bash, you can run alembic command directly.

docker compose run --rm -it --build -v $(pwd):/app/src/ api alembic upgrade "head"
shpilevskiyevgeniy commented 1 year ago

This template starts a database postgres:13.8-bullseye in docker, how do I make migrations to it?

s3rius commented 1 year ago

If you don't have a database running on your machine on port 5432 you can modify docker-compose configuration by adding

  ports:
  - 5432:5432

to your database service.

Or you can get it's IP address and set it in your .env file. To get IP addresses of all containers, run this command:

docker compose ps -q | xargs docker inspect -f "{{.Name}} {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}"

After you have the IP of the database, set it in your .env file.

shpilevskiyevgeniy commented 1 year ago

Still do not understand. You have a migrator in your project that does migrations, it works, but for example, I created a model, then changed its field and I need to make a revision, but when I make a new revision so that the migrator sends it to the database, I get an error: alembic revision --autogenerate -m "update model User"

raise OSError(err, f'Connect call failed {address}') ConnectionRefusedError: [Errno 111] Connect call failed ('127.0.0.1', 5432)

I didn’t change anything in the project, I just built it and launched it, these containers started up for me:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 65bdbd6ffd0b caireer_hr:latest "taskiq worker caire…" 7 minutes ago Up 7 minutes caireer_hr_taskiq-worker_1 4bb251580513 caireer_hr:latest "/usr/local/bin/pyth…" 7 minutes ago Up 7 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp caireer_hr_api_1 b7b3c3554e5b bitnami/kafka:3.2.0 "/opt/bitnami/script…" 7 minutes ago Up 7 minutes (healthy) 9092/tcp caireer_hr_kafka_1 aa5610d5eb9a bitnami/zookeeper:3.7.1 "/opt/bitnami/script…" 7 minutes ago Up 7 minutes (healthy) 2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp caireer_hr_zookeeper_1 86fe85285aac bitnami/redis:6.2.5 "/opt/bitnami/script…" 7 minutes ago Up 7 minutes (healthy) 6379/tcp caireer_hr_redis_1 3583c6a15351 postgres:13.8-bullseye "docker-entrypoint.s…" 7 minutes ago Up 7 minutes (healthy) 5432/tcp caireer_hr_db_1

How can I make revisions out of the box in your project, and then migrations?

s3rius commented 1 year ago

As I said before. Add ports in docker-compose if you want postgresql be accessible on your localhost.

By default it creates containers that ARE NOT ACCESSIBLE from your local machine. To open some ports, please modify docker-compose.

s3rius commented 9 months ago

Closed due to inactivity. The problem seems to be solved.