Skip to content

12-add-json-field-example #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 29, 2025
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ docker-apply-db-migrations: ## apply alembic migrations to database/schema
docker compose run --rm app alembic upgrade head

.PHONY: docker-create-db-migration
docker-create-db-migration: ## Create new alembic database migration aka database revision.
docker-create-db-migration: ## Create new alembic database migration aka database revision. Example: make docker-create-db-migration msg="add users table"
docker compose up -d db | true
docker compose run --no-deps app alembic revision --autogenerate -m "$(msg)"

Expand Down
37 changes: 37 additions & 0 deletions alembic/versions/20250729_1521_d021bd4763a5_add_json_chaos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""add json chaos

Revision ID: d021bd4763a5
Revises: 0c69050b5a3e
Create Date: 2025-07-29 15:21:19.415583

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'd021bd4763a5'
down_revision = '0c69050b5a3e'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('random_stuff',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('chaos', postgresql.JSON(astext_type=sa.Text()), nullable=False),
sa.PrimaryKeyConstraint('id'),
schema='happy_hog'
)
op.create_unique_constraint(None, 'nonsense', ['name'], schema='happy_hog')
op.create_unique_constraint(None, 'stuff', ['name'], schema='happy_hog')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'stuff', schema='happy_hog', type_='unique')
op.drop_constraint(None, 'nonsense', schema='happy_hog', type_='unique')
op.drop_table('random_stuff', schema='happy_hog')
# ### end Alembic commands ###
12 changes: 11 additions & 1 deletion app/api/stuff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
from sqlalchemy.ext.asyncio import AsyncSession

from app.database import get_db
from app.models.stuff import Stuff
from app.models.stuff import RandomStuff, Stuff
from app.schemas.stuff import RandomStuff as RandomStuffSchema
from app.schemas.stuff import StuffResponse, StuffSchema

logger = AppStructLogger().get_logger()

router = APIRouter(prefix="/v1/stuff")


@router.post("/random", status_code=status.HTTP_201_CREATED)
async def create_random_stuff(
payload: RandomStuffSchema, db_session: AsyncSession = Depends(get_db)
) -> dict[str, str]:
random_stuff = RandomStuff(**payload.model_dump())
await random_stuff.save(db_session)
return {"id": str(random_stuff.id)}


@router.post("/add_many", status_code=status.HTTP_201_CREATED)
async def create_multi_stuff(
payload: list[StuffSchema], db_session: AsyncSession = Depends(get_db)
Expand Down
11 changes: 9 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
logger = AppStructLogger().get_logger()
templates = Jinja2Templates(directory=Path(__file__).parent.parent / "templates")


@asynccontextmanager
async def lifespan(app: FastAPI):
app.redis = await get_redis()
Expand All @@ -30,12 +31,15 @@ async def lifespan(app: FastAPI):
min_size=5,
max_size=20,
)
await logger.ainfo("Postgres pool created", idle_size=app.postgres_pool.get_idle_size())
await logger.ainfo(
"Postgres pool created", idle_size=app.postgres_pool.get_idle_size()
)
yield
finally:
await app.redis.close()
await app.postgres_pool.close()


def create_app() -> FastAPI:
app = FastAPI(
title="Stuff And Nonsense API",
Expand All @@ -47,7 +51,9 @@ def create_app() -> FastAPI:
app.include_router(shakespeare_router)
app.include_router(user_router)
app.include_router(ml_router, prefix="/v1/ml", tags=["ML"])
app.include_router(health_router, prefix="/v1/public/health", tags=["Health, Public"])
app.include_router(
health_router, prefix="/v1/public/health", tags=["Health, Public"]
)
app.include_router(
health_router,
prefix="/v1/health",
Expand All @@ -61,6 +67,7 @@ def get_index(request: Request):

return app


app = create_app()

# --- Unused/experimental code and TODOs ---
Expand Down
4 changes: 3 additions & 1 deletion app/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ async def save(self, db_session: AsyncSession):
"""
try:
db_session.add(self)
return await db_session.commit()
await db_session.commit()
await db_session.refresh(self)
return self
except SQLAlchemyError as ex:
await logger.aerror(f"Error inserting instance of {self}: {repr(ex)}")
raise HTTPException(
Expand Down
12 changes: 11 additions & 1 deletion app/models/stuff.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid

from sqlalchemy import ForeignKey, String, select
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.dialects.postgresql import JSON, UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Mapped, joinedload, mapped_column, relationship

Expand All @@ -10,6 +10,16 @@
from app.utils.decorators import compile_sql_or_scalar


class RandomStuff(Base):
__tablename__ = "random_stuff"
__table_args__ = ({"schema": "happy_hog"},)

id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), default=uuid.uuid4, primary_key=True
)
chaos: Mapped[dict] = mapped_column(JSON)


class Stuff(Base):
__tablename__ = "stuff"
__table_args__ = ({"schema": "happy_hog"},)
Expand Down
5 changes: 5 additions & 0 deletions app/schemas/stuff.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from typing import Any
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

config = ConfigDict(from_attributes=True)


class RandomStuff(BaseModel):
chaos: dict[str, Any] = Field(..., description="JSON data for chaos field")


class StuffSchema(BaseModel):
name: str = Field(
title="",
Expand Down
6 changes: 3 additions & 3 deletions app/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def msg(self, message):
lineno=0,
msg=message.rstrip("\n"),
args=(),
exc_info=None
exc_info=None,
)

# Check if rotation is needed before emitting
Expand Down Expand Up @@ -78,7 +78,7 @@ def __attrs_post_init__(self):
filename=_log_path,
maxBytes=10 * 1024 * 1024, # 10MB
backupCount=5,
encoding="utf-8"
encoding="utf-8",
)
structlog.configure(
cache_logger_on_first_use=True,
Expand All @@ -90,7 +90,7 @@ def __attrs_post_init__(self):
structlog.processors.TimeStamper(fmt="iso", utc=True),
structlog.processors.JSONRenderer(serializer=orjson.dumps),
],
logger_factory=RotatingBytesLoggerFactory(_handler)
logger_factory=RotatingBytesLoggerFactory(_handler),
)
self._logger = structlog.get_logger()

Expand Down
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
- ./app:/panettone/app
- ./tests:/panettone/tests
- ./templates:/panettone/templates
- ./alembic:/panettone/alembic
ports:
- "8080:8080"
depends_on:
Expand Down