Files
marriage/.history/patch_alembic_template_20250808202000.sh
Andrey K. Choi cc87dcc0fa
Some checks failed
continuous-integration/drone/push Build is failing
api development
2025-08-08 21:58:36 +09:00

65 lines
1.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -euo pipefail
SERVICES=(auth profiles match chat payments)
for s in "${SERVICES[@]}"; do
TPL="services/$s/alembic/script.py.mako"
mkdir -p "services/$s/alembic"
cat > "$TPL" <<'MAKO'
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}
MAKO
echo "[ok] template updated: $TPL"
done
# Убедимся, что в env.py импортированы модели (для автогенерации)
for s in "${SERVICES[@]}"; do
ENV="services/$s/alembic/env.py"
if ! grep -q "from app import models" "$ENV"; then
awk '
/from app\.db\.session import Base/ && !x {print; print "from app import models # noqa: F401"; x=1; next}
{print}
' "$ENV" > "$ENV.tmp" && mv "$ENV.tmp" "$ENV"
echo "[ok] added 'from app import models' to $ENV"
fi
done
# удалить ревизии, созданные с битым шаблоном
for s in auth profiles match chat payments; do
rm -f services/$s/alembic/versions/*.py
done
# поднять Postgres (если не запущен)
docker compose up -d postgres
# автогенерация первичных ревизий (каждая сохранится в services/<svc>/alembic/versions/)
for s in auth profiles match chat payments; do
echo "[gen] $s"
docker compose run --rm -v "$PWD/services/$s":/app "$s" \
sh -lc 'alembic revision --autogenerate -m "init"'
done
for s in auth profiles match chat payments; do
echo "---- $s"
ls -1 services/$s/alembic/versions/
done