From a44bc52ad9375c0795b66ef39036810328b7c403 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin <win10@list.ru> Date: Fri, 1 Oct 2021 14:25:29 +0400 Subject: [PATCH] Fixed postgres healthcheck (#26) Description: * Fixed health command. * Fixed initialize_db for tortoise fixture in conftest. Signed-off-by: Pavel Kirilin <win10@list.ru> --- .../deploy/docker-compose.yml | 8 +++++-- .../{{cookiecutter.project_name}}/conftest.py | 24 ++----------------- pyproject.toml | 2 +- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml index c93d33a..639a7ee 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml @@ -6,6 +6,7 @@ services: context: . dockerfile: ./deploy/Dockerfile image: {{cookiecutter.project_name}}:{{"${" }}{{cookiecutter.project_name | upper }}_VERSION:-latest{{"}"}} + restart: always env_file: - .env {%- if ((cookiecutter.db_info.name != "none" and cookiecutter.db_info.name != "sqlite") or @@ -51,8 +52,9 @@ services: - "POSTGRES_DB={{cookiecutter.project_name}}" volumes: - {{cookiecutter.project_name}}-db-data:/var/lib/postgresql/data + restart: always healthcheck: - test: ["CMD-SHELL", "pg_isready"] + test: pg_isready -U {{cookiecutter.project_name}} interval: 2s timeout: 3s retries: 40 @@ -62,6 +64,7 @@ services: db: image: {{cookiecutter.db_info.image}} hostname: {{cookiecutter.project_name}}-db + restart: always environment: - "ALLOW_EMPTY_PASSWORD=yes" - "MYSQL_ROOT_PASSWORD={{cookiecutter.project_name}}" @@ -115,10 +118,11 @@ services: redis: image: bitnami/redis:6.2.5 hostname: {{cookiecutter.project_name}}-redis + restart: always environment: - "ALLOW_EMPTY_PASSWORD=yes" healthcheck: - test: ["CMD", "redis-cli", "ping"] + test: redis-cli ping interval: 1s timeout: 3s retries: 30 diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/conftest.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/conftest.py index 8ab5548..245d408 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/conftest.py +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/conftest.py @@ -22,8 +22,7 @@ from sqlalchemy.orm import sessionmaker from {{cookiecutter.project_name}}.db.dependencies import get_db_session from {{cookiecutter.project_name}}.db.utils import create_database, drop_database {%- elif cookiecutter.orm == "tortoise" %} -from tortoise.contrib.test import finalizer, initializer, _restore_default # noqa: WPS450 -from tortoise import Tortoise +from tortoise.contrib.test import finalizer, initializer from {{cookiecutter.project_name}}.db.config import MODELS_MODULES {%- endif %} {%- endif %} @@ -124,7 +123,7 @@ async def transaction(_engine: AsyncEngine) -> AsyncGenerator[AsyncConnection, N await conn.rollback() {%- elif cookiecutter.orm == "tortoise" %} -@pytest.fixture(scope="session", autouse=True) +@pytest.fixture(autouse=True) def initialize_db(event_loop: AbstractEventLoop) -> Generator[None, None, None]: """ Initialize models and database. @@ -143,25 +142,6 @@ def initialize_db(event_loop: AbstractEventLoop) -> Generator[None, None, None]: finalizer() -@pytest.fixture(autouse=True) -@pytest.mark.asyncio -async def clean_db() -> AsyncGenerator[None, None]: - """ - Removes all data from database after test. - - :yields: Nothing. - """ - yield - - _restore_default() - for app in Tortoise.apps.values(): - for model in app.values(): - meta = model._meta # noqa: WPS437 - quote_char = meta.db.query_class._builder().QUOTE_CHAR # noqa: WPS437 - await meta.db.execute_script( - f"DELETE FROM {quote_char}{meta.db_table}{quote_char}" # noqa: S608 - ) - {%- endif %} {%- endif %} diff --git a/pyproject.toml b/pyproject.toml index 3658f44..39133be 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "fastapi_template" -version = "3.1.2" +version = "3.1.3" description = "Feature-rich robust FastAPI template" authors = ["Pavel Kirilin <win10@list.ru>"] packages = [{ include = "fastapi_template" }] -- GitLab