feat: store messenger data in PostgreSQL
Replace the in-memory store with PostgreSQL 18.6: SQLAlchemy 2.1 async engine on asyncpg, alembic migrations with an async env.py, repos on AsyncSession and services that own the transaction. Username uniqueness is enforced by the database (ON CONFLICT), a chat and its members are created in one transaction, member order is kept in chat_members.position, and a composite foreign key makes the sender a member of the chat. History and chat lists use keyset pagination on (chat_id, created_at, id) and (user_id, created_at, chat_id). The compose stand runs postgres, a one-shot migrate service on the API image and the API. All Postgres addresses come from .env without literals; only the 127.0.0.1 publish address stays literal. make run_dev starts Postgres and applies migrations before the server, and make env_setup generates a random Postgres password. Unavailable PostgreSQL yields 503: get_db_session bounds the request with asyncio and classifies driver errors. BoundedCloseConnection limits asyncpg's graceful close, which otherwise waited without limit on a frozen server and held the request until it was resumed. Add scripts/restart.sh for make test-persistence and make test_isolated, which runs the image on a throwaway test database. Remove make run_no_compose. X-User-Id identification is unchanged until the session-auth change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
"""`make test_isolated`: образ на временной базе `test_<db>`.
|
||||
|
||||
Создаёт базу в Postgres стенда, накатывает на неё миграции сервисом
|
||||
`migrate`, поднимает отдельный контейнер `api` из определения compose с
|
||||
подменой только `DATABASE__NAME`, прогоняет `make test` и
|
||||
`make student_test`, затем удаляет контейнер и базу — при любом исходе,
|
||||
в том числе по Ctrl+C. Рабочая база и рабочий контейнер API не
|
||||
трогаются. Только stdlib: всё через docker compose и make. Решение 12
|
||||
design change `postgres-storage`.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
ENV_FILE = ROOT / ".env"
|
||||
DB_NAME_RE = re.compile(r"^[a-z0-9_]+$")
|
||||
READY_TIMEOUT_S = 60
|
||||
LOG_TAIL_LINES = 50
|
||||
|
||||
|
||||
def say(message: str) -> None:
|
||||
print(f"[test_isolated] {message}", flush=True)
|
||||
|
||||
|
||||
def run(
|
||||
*args: str,
|
||||
check: bool = True,
|
||||
stdin: str | None = None,
|
||||
quiet: bool = False,
|
||||
) -> subprocess.CompletedProcess[str]:
|
||||
"""Команда из корня репозитория; вывод — в терминал, если не quiet."""
|
||||
return subprocess.run(
|
||||
args,
|
||||
cwd=ROOT,
|
||||
check=check,
|
||||
input=stdin,
|
||||
text=True,
|
||||
capture_output=quiet,
|
||||
)
|
||||
|
||||
|
||||
def read_env() -> dict[str, str]:
|
||||
"""`KEY=VALUE` из `.env`; подстановок `${...}` нужные ключи не имеют."""
|
||||
values: dict[str, str] = {}
|
||||
for line in ENV_FILE.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
values[key.strip()] = value.strip()
|
||||
return values
|
||||
|
||||
|
||||
def compose_project() -> str:
|
||||
config = run(
|
||||
"docker",
|
||||
"compose",
|
||||
"config",
|
||||
"--format",
|
||||
"json",
|
||||
quiet=True,
|
||||
)
|
||||
return json.loads(config.stdout)["name"]
|
||||
|
||||
|
||||
def psql(sql: str) -> None:
|
||||
"""SQL в служебной базе `postgres` под `POSTGRES_USER` контейнера."""
|
||||
run(
|
||||
"docker",
|
||||
"compose",
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"sh",
|
||||
"-c",
|
||||
'exec psql -U "$POSTGRES_USER" -d postgres -v ON_ERROR_STOP=1 -X -q',
|
||||
stdin=sql,
|
||||
)
|
||||
|
||||
|
||||
def drop_database(name: str) -> None:
|
||||
# WITH (FORCE) рвёт оставшиеся соединения, если контейнер не ушёл
|
||||
psql(f'DROP DATABASE IF EXISTS "{name}" WITH (FORCE);\n')
|
||||
|
||||
|
||||
def remove_container(name: str) -> None:
|
||||
run("docker", "rm", "-f", name, check=False, quiet=True)
|
||||
|
||||
|
||||
def wait_ready(url: str, container: str) -> None:
|
||||
deadline = time.monotonic() + READY_TIMEOUT_S
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=2) as response:
|
||||
if response.status == 200:
|
||||
return
|
||||
except (urllib.error.URLError, OSError):
|
||||
pass
|
||||
state = run(
|
||||
"docker",
|
||||
"inspect",
|
||||
"-f",
|
||||
"{{.State.Running}}",
|
||||
container,
|
||||
check=False,
|
||||
quiet=True,
|
||||
)
|
||||
if state.stdout.strip() != "true":
|
||||
raise RuntimeError(f"container {container} is not running")
|
||||
time.sleep(1)
|
||||
raise RuntimeError(f"{url} not ready in {READY_TIMEOUT_S}s")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
parser.add_argument(
|
||||
"--keep-db",
|
||||
action="store_true",
|
||||
help="не удалять базу после прогона (KEEP_TEST_DB=1)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
env = read_env()
|
||||
test_db = f"test_{env['POSTGRES_DB']}"
|
||||
if not DB_NAME_RE.fullmatch(test_db):
|
||||
say(f"unsupported database name: {test_db}")
|
||||
return 2
|
||||
port = env["TEST_APP_PORT"]
|
||||
app_port = env.get("APP__PORT", "8080")
|
||||
container = f"{compose_project()}-test-api"
|
||||
base_url = f"http://127.0.0.1:{port}"
|
||||
|
||||
say(f"database={test_db} container={container} base_url={base_url}")
|
||||
remove_container(container)
|
||||
drop_database(test_db)
|
||||
|
||||
results: dict[str, int] = {}
|
||||
failed = True
|
||||
try:
|
||||
say("build image")
|
||||
run("docker", "compose", "build", "api")
|
||||
say(f"create database {test_db}")
|
||||
psql(f'CREATE DATABASE "{test_db}";\n')
|
||||
say("migrations")
|
||||
run(
|
||||
"docker",
|
||||
"compose",
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"-e",
|
||||
f"DATABASE__NAME={test_db}",
|
||||
"migrate",
|
||||
)
|
||||
say("start api container")
|
||||
run(
|
||||
"docker",
|
||||
"compose",
|
||||
"run",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
"--name",
|
||||
container,
|
||||
"-e",
|
||||
f"DATABASE__NAME={test_db}",
|
||||
"-p",
|
||||
f"127.0.0.1:{port}:{app_port}",
|
||||
"api",
|
||||
quiet=True,
|
||||
)
|
||||
wait_ready(f"{base_url}/health/ready", container)
|
||||
say("ready")
|
||||
for target in ("test", "student_test"):
|
||||
say(f"make {target}")
|
||||
results[target] = run(
|
||||
"make",
|
||||
target,
|
||||
f"BASE_URL={base_url}",
|
||||
check=False,
|
||||
).returncode
|
||||
failed = any(results.values())
|
||||
except KeyboardInterrupt:
|
||||
say("interrupted")
|
||||
results["interrupted"] = 130
|
||||
except (subprocess.CalledProcessError, RuntimeError) as e:
|
||||
say(f"setup failed: {e}")
|
||||
results["setup"] = 1
|
||||
finally:
|
||||
if failed:
|
||||
say(f"last {LOG_TAIL_LINES} lines of {container}")
|
||||
run(
|
||||
"docker",
|
||||
"logs",
|
||||
"--tail",
|
||||
str(LOG_TAIL_LINES),
|
||||
container,
|
||||
check=False,
|
||||
)
|
||||
say("cleanup")
|
||||
run("docker", "stop", container, check=False, quiet=True)
|
||||
remove_container(container)
|
||||
try:
|
||||
if args.keep_db:
|
||||
say(f"kept database {test_db}")
|
||||
else:
|
||||
drop_database(test_db)
|
||||
except subprocess.CalledProcessError as e:
|
||||
say(f"cleanup failed: {e}")
|
||||
results["cleanup"] = 1
|
||||
|
||||
say(f"results: {results}")
|
||||
return next((code for code in results.values() if code), 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user