Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
image: postgres:19.0
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ docker compose exec app pytest

- **Bot**: python-telegram-bot, webhook in prod / `start-polling.py` locally
- **API**: FastAPI + Uvicorn
- **DB**: PostgreSQL 14, SQLAlchemy `Table` objects in `src/database.py` (not ORM models)
- **DB**: PostgreSQL 19, SQLAlchemy `Table` objects in `src/database.py` (not ORM models)
- **Cache/Queue**: Redis recommendation queues
- **Jobs**: Prefect via `scripts/serve_flows.py`
- **Describe Memes**: free OpenRouter vision only — see `AGENTS.md` / `specs/describe-memes.md`
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ services:
app_db:
container_name: app_db
hostname: app_db
image: library/postgres:14.1
image: postgres:19.0
env_file:
- .env
volumes:
- app_pg_data:/var/lib/postgresql/data
- app_pg19_data:/var/lib/postgresql
- app_pg_data_backups:/backups
- ./scripts/postgres:/scripts
ports:
Expand All @@ -87,7 +87,7 @@ services:


volumes:
app_pg_data:
app_pg19_data:
driver: "local"
app_pg_data_backups:
driver: "local"
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ httpx==0.28.*

pydantic==2.14.*
pydantic-settings==2.16.*
asyncpg==0.31.*
asyncpg==0.32.*
fastapi==0.136.*
uvicorn[standard]==0.46.*

Expand Down
31 changes: 13 additions & 18 deletions src/tgbot/repo/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,21 @@ async def create_or_update_user(
indicates if user was created (True) or updated (False).
"""
sql = f"""
WITH upsert AS (
INSERT
INTO "user"
(id, type, last_active_at)
VALUES ({id}, '{UserType.USER.value}', NOW())
ON CONFLICT(id)
DO UPDATE SET
blocked_bot_at = NULL,
last_active_at = NOW(),
type = CASE
WHEN "user".type = '{UserType.BLOCKED_BOT.value}'
THEN '{UserType.USER.value}'
ELSE "user".type
END
RETURNING "user".*, (xmax = 0) as created
)
SELECT *, created FROM upsert
INSERT INTO "user" (id, type, last_active_at)
VALUES (:id, '{UserType.USER.value}', NOW())
ON CONFLICT(id)
DO UPDATE SET
blocked_bot_at = NULL,
last_active_at = NOW(),
type = CASE
WHEN "user".type = '{UserType.BLOCKED_BOT.value}'
THEN '{UserType.USER.value}'
ELSE "user".type
END
RETURNING new.*, old.id IS NULL AS created
"""

result = await fetch_one(text(sql))
result = await fetch_one(text(sql), {"id": id})
created = result.pop("created")
return result, created

Expand Down
18 changes: 18 additions & 0 deletions tests/tgbot/test_create_or_update_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sqlalchemy import func

from src.database import engine, user
from src.tgbot.repo.users import create_or_update_user


async def test_create_or_update_user_reports_created_and_unblocks():
try:
row, created = await create_or_update_user(id=90501)
assert created is True and row["id"] == 90501 and row["type"] == "user"

async with engine.begin() as conn:
await conn.execute(user.update().where(user.c.id == 90501).values(type="blocked_bot", blocked_bot_at=func.now()))
row, created = await create_or_update_user(id=90501)
assert created is False and row["type"] == "user" and row["blocked_bot_at"] is None
finally:
async with engine.begin() as conn:
await conn.execute(user.delete().where(user.c.id == 90501))
Loading