v1.1.0: Alembic, pytest, домен и документация

- Миграции PostgreSQL через Alembic; DDL убран из lifespan приложения.
- Тесты: health, status, ingress Grafana; моки Vault/Grafana/Forgejo.
- Пакет onguard24/domain/ (сущности, шина событий), docs/DOMAIN.md.
- Обновлены README, CHANGELOG, ARCHITECTURE.

Made-with: Cursor
This commit is contained in:
Alexandr
2026-04-03 08:36:35 +03:00
parent 4da9b13a86
commit 85eb61b576
21 changed files with 611 additions and 32 deletions

57
alembic/env.py Normal file
View File

@ -0,0 +1,57 @@
"""Alembic: синхронный движок SQLAlchemy + psycopg3 (отдельно от asyncpg в рантайме)."""
from __future__ import annotations
import os
from logging.config import fileConfig
from pathlib import Path
from alembic import context
from dotenv import load_dotenv
from sqlalchemy import create_engine, pool
ROOT = Path(__file__).resolve().parent.parent
load_dotenv(ROOT / ".env")
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = None
def get_sync_url() -> str:
url = os.environ.get("DATABASE_URL", "").strip()
if not url:
raise RuntimeError("Задай DATABASE_URL для alembic upgrade")
if url.startswith("postgres://"):
url = url.replace("postgres://", "postgresql://", 1)
if url.startswith("postgresql://") and "+psycopg" not in url and "+asyncpg" not in url:
url = url.replace("postgresql://", "postgresql+psycopg://", 1)
if "+asyncpg" in url:
url = url.replace("+asyncpg", "+psycopg")
return url
def run_migrations_offline() -> None:
context.configure(
url=get_sync_url(),
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
connectable = create_engine(get_sync_url(), poolclass=pool.NullPool)
with connectable.connect() as connection:
context.configure(connection=connection)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

26
alembic/script.py.mako Normal file
View File

@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@ -0,0 +1,40 @@
"""initial ingress_events
Revision ID: 001_initial
Revises:
Create Date: 2026-04-03
"""
from typing import Sequence, Union
from alembic import op
revision: str = "001_initial"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.execute(
"""
CREATE TABLE IF NOT EXISTS ingress_events (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
source text NOT NULL,
received_at timestamptz NOT NULL DEFAULT now(),
body jsonb NOT NULL
);
"""
)
op.execute(
"""
CREATE INDEX IF NOT EXISTS ingress_events_received_at_idx
ON ingress_events (received_at DESC);
"""
)
def downgrade() -> None:
op.execute("DROP INDEX IF EXISTS ingress_events_received_at_idx;")
op.execute("DROP TABLE IF EXISTS ingress_events;")