From 2b89811423c6bf65ff53d47cee70baac0a6e41da Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 6 Nov 2026 16:24:36 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D1=81=D0=B5=D1=80=D0=B8=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BC=D0=B8=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20alembic=20=D1=87=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B7=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B?= =?UTF-8?q?=D0=B9=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20=D0=B8=20adviso?= =?UTF-8?q?ry=20lock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- app/migrations/env.py | 12 +++++++++--- compose.yaml | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/migrations/env.py b/app/migrations/env.py index f03e6e70..e5e2d128 100644 --- a/app/migrations/env.py +++ b/app/migrations/env.py @@ -28,9 +28,15 @@ def run_migrations_online() -> None: poolclass=pool.NullPool, ) with connectable.connect() as connection: - context.configure(connection=connection, target_metadata=target_metadata) - with context.begin_transaction(): - context.run_migrations() + # Advisory lock сериализует параллельные запуски alembic upgrade + # (например, при старте нескольких реплик одновременно). + connection.exec_driver_sql("SELECT pg_advisory_lock(727274)") + try: + context.configure(connection=connection, target_metadata=target_metadata) + with context.begin_transaction(): + context.run_migrations() + finally: + connection.exec_driver_sql("SELECT pg_advisory_unlock(727274)") if context.is_offline_mode(): diff --git a/compose.yaml b/compose.yaml index ef2c7a4b..e0670768 100644 --- a/compose.yaml +++ b/compose.yaml @@ -57,6 +57,16 @@ services: command: ["sh", "-c", "pip install --quiet boto3==1.35.76 && python /bootstrap_s3.py"] restart: "no" + migrate: + build: . + environment: + DATABASE_URL: postgresql+psycopg://messenger:messenger@postgres:5432/messenger + depends_on: + postgres: + condition: service_healthy + command: ["alembic", "upgrade", "head"] + restart: "no" + api-1: build: . environment: @@ -80,7 +90,9 @@ services: condition: service_healthy s3-init: condition: service_completed_successfully - command: ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8080"] + migrate: + condition: service_completed_successfully + command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"] api-2: build: . @@ -105,7 +117,9 @@ services: condition: service_healthy s3-init: condition: service_completed_successfully - command: ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8080"] + migrate: + condition: service_completed_successfully + command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"] lb: image: nginx:1.27-alpine