Repository navigation
117 lines (110 loc) · 4.57 KB
/
Copy pathmigrate.yml
File metadata and controls
117 lines (110 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
name: migrate
on:
workflow_dispatch:
inputs:
revision:
description: Alembic revision (default head)
required: false
default: head
telegram_env:
description: Telegram env for notifications (dev/prod)
required: false
default: dev
permissions:
contents: read
jobs:
migrate:
runs-on: ubuntu-latest
env:
TELEGRAM_ENV: ${{ github.ref_name == 'prod' && 'prod' || 'dev' }}
steps:
- name: Run migrations on VM
uses: appleboy/ssh-action@v1.0.3
env:
REVISION: ${{ inputs.revision }}
GHCR_USER: ${{ secrets.DEV_GHCR_USER }}
GHCR_PAT: ${{ secrets.DEV_GHCR_PAT }}
DATABASE_URL: ${{ secrets.DEV_DATABASE_URL }}
with:
host: ${{ secrets.DEV_SSH_HOST }}
username: ${{ secrets.DEV_SSH_USER }}
key: ${{ secrets.DEV_SSH_KEY }}
port: ${{ secrets.DEV_SSH_PORT }}
envs: REVISION,GHCR_USER,GHCR_PAT,DATABASE_URL
script: |
set -e
cd ${{ secrets.DEV_DEPLOY_PATH }}
docker login ghcr.io -u "$GHCR_USER" -p "$GHCR_PAT"
docker compose pull backend
docker compose run --rm -e DATABASE_URL="$DATABASE_URL" backend python - <<'PY'
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from app.settings import settings
async def main():
engine = create_async_engine(settings.DATABASE_URL)
async with engine.begin() as conn:
meta = await conn.execute(
text(
"select current_database(), current_schema(), "
"inet_server_addr(), inet_server_port()"
)
)
print("DB meta:", meta.first())
rows = await conn.execute(
text(
"select table_name from information_schema.tables "
"where table_schema='public' order by 1"
)
)
tables = [r[0] for r in rows]
print(f"Tables before migrate: {len(tables)}")
await engine.dispose()
asyncio.run(main())
PY
docker compose run --rm -e DATABASE_URL="$DATABASE_URL" backend alembic upgrade "$REVISION"
docker compose run --rm -e DATABASE_URL="$DATABASE_URL" backend python - <<'PY'
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from app.settings import settings
async def main():
engine = create_async_engine(settings.DATABASE_URL)
async with engine.begin() as conn:
ver = await conn.execute(
text("select to_regclass('public.alembic_version')")
)
ver_table = ver.scalar()
rows = await conn.execute(
text(
"select table_name from information_schema.tables "
"where table_schema='public' order by 1"
)
)
tables = [r[0] for r in rows]
await engine.dispose()
if not ver_table or not tables:
raise SystemExit(
f"Migration check failed: alembic_version={ver_table}, tables={tables}"
)
print(f"Migration check ok: tables={len(tables)}")
asyncio.run(main())
PY
- name: Notify Telegram
if: always()
env:
TELEGRAM_ENV: ${{ inputs.telegram_env || env.TELEGRAM_ENV || 'dev' }}
TG_TOKEN: ${{ secrets.DEV_NOTIFY_BOT_TOKEN }}
TG_CHAT_ID: ${{ secrets.DEV_NOTIFY_CHAT_ID }}
STATUS: ${{ job.status }}
REPO: ${{ github.repository }}
SHA: ${{ github.sha }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
if [ -z "$TG_TOKEN" ] || [ -z "$TG_CHAT_ID" ]; then
exit 0
fi
msg=$'AssRep core migrate\nstatus: '"$STATUS"$'\nenv: '"$TELEGRAM_ENV"$'\nrepo: '"$REPO"$'\nsha: '"$SHA"$'\nrun: '"$RUN_URL"
curl -sS -X POST "https://api.telegram.org/bot${TG_TOKEN}/sendMessage" \
-d chat_id="$TG_CHAT_ID" \
-d text="$msg" >/dev/null