fix: сериализовать миграции alembic через отдельный сервис и advisory lock
Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
@@ -28,9 +28,15 @@ def run_migrations_online() -> None:
|
|||||||
poolclass=pool.NullPool,
|
poolclass=pool.NullPool,
|
||||||
)
|
)
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
context.configure(connection=connection, target_metadata=target_metadata)
|
# Advisory lock сериализует параллельные запуски alembic upgrade
|
||||||
with context.begin_transaction():
|
# (например, при старте нескольких реплик одновременно).
|
||||||
context.run_migrations()
|
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():
|
if context.is_offline_mode():
|
||||||
|
|||||||
+16
-2
@@ -57,6 +57,16 @@ services:
|
|||||||
command: ["sh", "-c", "pip install --quiet boto3==1.35.76 && python /bootstrap_s3.py"]
|
command: ["sh", "-c", "pip install --quiet boto3==1.35.76 && python /bootstrap_s3.py"]
|
||||||
restart: "no"
|
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:
|
api-1:
|
||||||
build: .
|
build: .
|
||||||
environment:
|
environment:
|
||||||
@@ -80,7 +90,9 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
s3-init:
|
s3-init:
|
||||||
condition: service_completed_successfully
|
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:
|
api-2:
|
||||||
build: .
|
build: .
|
||||||
@@ -105,7 +117,9 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
s3-init:
|
s3-init:
|
||||||
condition: service_completed_successfully
|
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:
|
lb:
|
||||||
image: nginx:1.27-alpine
|
image: nginx:1.27-alpine
|
||||||
|
|||||||
Reference in New Issue
Block a user