Skip to content

Commit

Permalink
Database and models overall improvements (#409)
Browse files Browse the repository at this point in the history
- remove version in docker compose
- keep unused import during alembic hook
- configuration to use annotated declarative table

---------

Co-authored-by: Armand Didierjean <[email protected]>
  • Loading branch information
julien4215 and armanddidierjean authored Apr 27, 2024
1 parent 6f28242 commit 3a87006
Show file tree
Hide file tree
Showing 26 changed files with 98 additions and 61 deletions.
3 changes: 2 additions & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ hooks=ruff_check, ruff_format

ruff_check.type = exec
ruff_check.executable = ruff
ruff_check.options = check --fix REVISION_SCRIPT_FILENAME
# We want to keep the imports in case we have to manually edit the migration file after generating the revision
ruff_check.options = check --ignore F401 --fix REVISION_SCRIPT_FILENAME

ruff_format.type = exec
ruff_format.executable = ruff
Expand Down
2 changes: 1 addition & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from app.core.config import Settings
from app.core.groups.groups_type import GroupType
from app.core.log import LogConfig
from app.database import Base
from app.dependencies import get_db_engine, get_redis_client, get_settings
from app.types.sqlalchemy import Base
from app.utils import initialization
from app.utils.redis import limiter

Expand Down
3 changes: 1 addition & 2 deletions app/core/auth/models_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class AuthorizationCode(Base):
Expand Down
3 changes: 1 addition & 2 deletions app/core/models_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
from sqlalchemy import Date, Enum, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base
from app.types.floors_type import FloorsType
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class CoreMembership(Base):
Expand Down
3 changes: 1 addition & 2 deletions app/core/notification/models_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from sqlalchemy.orm import Mapped, mapped_column

from app.core.notification.notification_types import Topic
from app.database import Base
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class Message(Base):
Expand Down
7 changes: 0 additions & 7 deletions app/database.py

This file was deleted.

3 changes: 1 addition & 2 deletions app/modules/advert/models_advert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class Advertiser(Base):
Expand Down
3 changes: 1 addition & 2 deletions app/modules/amap/models_amap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.models_core import CoreUser
from app.database import Base
from app.modules.amap.types_amap import AmapSlotType, DeliveryStatusType
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class AmapOrderContent(Base):
Expand Down
3 changes: 1 addition & 2 deletions app/modules/booking/models_booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.models_core import CoreUser
from app.database import Base
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class Manager(Base):
Expand Down
3 changes: 1 addition & 2 deletions app/modules/calendar/models_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.models_core import CoreUser
from app.database import Base
from app.modules.calendar.types_calendar import CalendarEventType
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class Event(Base):
Expand Down
2 changes: 1 addition & 1 deletion app/modules/campaign/models_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core import models_core
from app.database import Base
from app.modules.campaign.types_campaign import StatusType
from app.types.sqlalchemy import Base


class ListMemberships(Base):
Expand Down
3 changes: 1 addition & 2 deletions app/modules/cinema/models_cinema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from sqlalchemy import Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, TZDateTime


class Session(Base):
Expand Down
2 changes: 1 addition & 1 deletion app/modules/loan/models_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core import models_core
from app.database import Base
from app.types.sqlalchemy import Base


class Loaner(Base):
Expand Down
2 changes: 1 addition & 1 deletion app/modules/phonebook/models_phonebook.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from sqlalchemy import Enum, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base
from app.modules.phonebook.types_phonebook import Kinds
from app.types.sqlalchemy import Base


class Membership(Base):
Expand Down
2 changes: 1 addition & 1 deletion app/modules/raffle/models_raffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.models_core import CoreGroup, CoreUser
from app.database import Base
from app.modules.raffle.types_raffle import RaffleStatusType
from app.types.sqlalchemy import Base


class Raffle(Base):
Expand Down
7 changes: 4 additions & 3 deletions app/modules/recommendation/cruds_recommendation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from collections.abc import Sequence

from sqlalchemy import delete, select, update
Expand All @@ -23,7 +24,7 @@ async def create_recommendation(


async def update_recommendation(
recommendation_id: str,
recommendation_id: uuid.UUID,
recommendation: schemas_recommendation.RecommendationEdit,
db: AsyncSession,
):
Expand All @@ -43,7 +44,7 @@ async def update_recommendation(


async def delete_recommendation(
recommendation_id: str,
recommendation_id: uuid.UUID,
db: AsyncSession,
):
result = await db.execute(
Expand All @@ -59,7 +60,7 @@ async def delete_recommendation(


async def get_recommendation_by_id(
recommendation_id: str,
recommendation_id: uuid.UUID,
db: AsyncSession,
) -> models_recommendation.Recommendation | None:
result = await db.execute(
Expand Down
12 changes: 6 additions & 6 deletions app/modules/recommendation/endpoints_recommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def create_recommendation(
"""

recommendation_db = models_recommendation.Recommendation(
id=str(uuid.uuid4()),
id=uuid.uuid4(),
creation=datetime.now(UTC),
**recommendation.model_dump(),
)
Expand All @@ -85,7 +85,7 @@ async def create_recommendation(
status_code=204,
)
async def edit_recommendation(
recommendation_id: str,
recommendation_id: uuid.UUID,
recommendation: schemas_recommendation.RecommendationEdit,
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.BDE)),
Expand All @@ -111,7 +111,7 @@ async def edit_recommendation(
status_code=204,
)
async def delete_recommendation(
recommendation_id: str,
recommendation_id: uuid.UUID,
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.BDE)),
):
Expand All @@ -136,7 +136,7 @@ async def delete_recommendation(
status_code=200,
)
async def read_recommendation_image(
recommendation_id: str,
recommendation_id: uuid.UUID,
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member),
):
Expand All @@ -156,7 +156,7 @@ async def read_recommendation_image(
return get_file_from_data(
default_asset="assets/images/default_recommendation.png",
directory="recommendations",
filename=recommendation_id,
filename=str(recommendation_id),
)


Expand All @@ -166,7 +166,7 @@ async def read_recommendation_image(
status_code=201,
)
async def create_recommendation_image(
recommendation_id: str,
recommendation_id: uuid.UUID,
image: UploadFile = File(),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.BDE)),
request_id: str = Depends(get_request_id),
Expand Down
19 changes: 8 additions & 11 deletions app/modules/recommendation/models_recommendation.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from datetime import datetime

from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Mapped

from app.database import Base
from app.types.sqlalchemy import TZDateTime
from app.types.sqlalchemy import Base, PrimaryKey


class Recommendation(Base):
__tablename__ = "recommendation"

id: Mapped[str] = mapped_column(String, primary_key=True, nullable=False)
creation: Mapped[datetime] = mapped_column(TZDateTime, nullable=False)
title: Mapped[str] = mapped_column(String, nullable=False)
code: Mapped[str | None] = mapped_column(String, nullable=True)

summary: Mapped[str] = mapped_column(String, nullable=False)
description: Mapped[str] = mapped_column(String, nullable=False)
id: Mapped[PrimaryKey]
creation: Mapped[datetime]
title: Mapped[str]
code: Mapped[str | None]
summary: Mapped[str]
description: Mapped[str]
3 changes: 2 additions & 1 deletion app/modules/recommendation/schemas_recommendation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from datetime import datetime

from pydantic import BaseModel, ConfigDict
Expand All @@ -11,7 +12,7 @@ class RecommendationBase(BaseModel):


class Recommendation(RecommendationBase):
id: str
id: uuid.UUID
creation: datetime

model_config = ConfigDict(from_attributes=True)
Expand Down
23 changes: 22 additions & 1 deletion app/types/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
import uuid
from typing import Annotated

from sqlalchemy import DateTime
from sqlalchemy import DateTime, types
from sqlalchemy.orm import DeclarativeBase, mapped_column
from sqlalchemy.types import TypeDecorator


Expand All @@ -27,3 +30,21 @@ def process_result_value(self, value, dialect):
if value is not None:
value = value.replace(tzinfo=datetime.UTC)
return value


# Pre-configured field type for UUID primary key (see https://docs.sqlalchemy.org/en/20/orm/declarative_tables.html#mapping-whole-column-declarations-to-python-types)
PrimaryKey = Annotated[uuid.UUID, mapped_column(primary_key=True)]


class Base(DeclarativeBase):
"""Base class for all models.
The type map is overriden to use our custom datetime type (see https://docs.sqlalchemy.org/en/20/orm/declarative_tables.html#customizing-the-type-map)"""

type_annotation_map = {
bool: types.Boolean(),
datetime.date: types.Date(),
datetime.datetime: TZDateTime(),
str: types.String(),
uuid.UUID: types.Uuid(),
}
2 changes: 0 additions & 2 deletions docker-compose-dev.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
hyperion-db:
image: postgres
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.8"

services:
hyperion-db:
image: postgres
Expand Down
2 changes: 1 addition & 1 deletion migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import AsyncConnection

from app.database import Base
from app.dependencies import get_db_engine, get_settings
from app.types.sqlalchemy import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand Down
32 changes: 32 additions & 0 deletions migrations/versions/8-recommendation_primary_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""change primary key type from string to UUID
Create Date: 2024-04-21 02:08:19.548067
"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "d99516f0bbcb"
down_revision: str | None = "e3d06397960d"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
with op.batch_alter_table("recommendation") as batch_op:
batch_op.alter_column(
"id",
type_=sa.types.Uuid(),
postgresql_using="id::uuid",
)


def downgrade() -> None:
with op.batch_alter_table("recommendation") as batch_op:
batch_op.alter_column(
"id",
type_=sa.types.String(),
)
2 changes: 1 addition & 1 deletion tests/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
from app.core.groups import cruds_groups
from app.core.groups.groups_type import GroupType
from app.core.users import cruds_users
from app.database import Base
from app.dependencies import get_db, get_redis_client, get_settings
from app.types.floors_type import FloorsType
from app.types.sqlalchemy import Base
from app.utils.redis import connect, disconnect
from app.utils.tools import get_random_string

Expand Down
Loading

0 comments on commit 3a87006

Please sign in to comment.