Commit 6948d920 authored by Sergio Gimenez's avatar Sergio Gimenez
Browse files

Add postgres adapter

parent 291f11c2
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
services:
  postgres:
    image: postgres:16-alpine
    container_name: fm-postgres
    environment:
      POSTGRES_USER: fm
      POSTGRES_PASSWORD: fm
      POSTGRES_DB: fm_db
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U fm -d fm_db"]
      interval: 3s
      timeout: 3s
      retries: 20
    volumes:
      - fm-pgdata:/var/lib/postgresql/data

volumes:
  fm-pgdata:
+6 −0
Original line number Diff line number Diff line
@@ -9,7 +9,10 @@ description = "Federation Manager (OOP Release 2.0)"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "asyncpg>=0.30",
    "fastapi[standard]>=0.115",
    "pydantic-settings>=2.4",
    "sqlalchemy[asyncio]>=2.0",
]

[project.optional-dependencies]
@@ -49,3 +52,6 @@ files = ["src/federation_manager", "tests"]
testpaths = ["tests"]
asyncio_mode = "auto"
pythonpath = ["."]
markers = [
    "integration: requires a running Postgres (docker compose -f docker-compose.dev.yaml up -d)",
]
+0 −0

Empty file added.

+0 −0

Empty file added.

+21 −0
Original line number Diff line number Diff line
from sqlalchemy.ext.asyncio import (
    AsyncEngine,
    AsyncSession,
    async_sessionmaker,
    create_async_engine,
)

from federation_manager.adapters.database.tables import metadata


def build_engine(url: str, echo: bool = False) -> AsyncEngine:
    return create_async_engine(url, echo=echo)


def build_session_maker(engine: AsyncEngine) -> async_sessionmaker[AsyncSession]:
    return async_sessionmaker(engine, expire_on_commit=False)


async def create_schema(engine: AsyncEngine) -> None:
    async with engine.begin() as conn:
        await conn.run_sync(metadata.create_all)
Loading