Skip to content

Commit

Permalink
(PC-32747)[API] feat: fix schema typing and set startDatetime and end…
Browse files Browse the repository at this point in the history
…Datetime not null on collective stock
  • Loading branch information
jcicurel-pass committed Dec 26, 2024
1 parent 2aac204 commit 470dbef
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/alembic_version_conflict_detection.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
f8588023c126 (pre) (head)
b18478ab2ea8 (post) (head)
a8d2c442cc67 (post) (head)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Add NOT NULL constraint on "collective_stock.startDatetime" and "collective_stock.endDatetime" (step 3 of 4)
"""

from alembic import op


# pre/post deployment: post
# revision identifiers, used by Alembic.
revision = "4302137d444a"
down_revision = "89d6c28597ef"
branch_labels: tuple[str] | None = None
depends_on: list[str] | None = None


def upgrade() -> None:
op.alter_column("collective_stock", "startDatetime", nullable=False)
op.alter_column("collective_stock", "endDatetime", nullable=False)


def downgrade() -> None:
op.alter_column("collective_stock", "startDatetime", nullable=True)
op.alter_column("collective_stock", "endDatetime", nullable=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Add NOT NULL constraint on "collective_stock.startDatetime" and "collective_stock.endDatetime" (step 2 of 4)
"""

from alembic import op

from pcapi import settings

# pre/post deployment: post
# revision identifiers, used by Alembic.
revision = "89d6c28597ef"
down_revision = "a09ff41c9e94"
branch_labels: tuple[str] | None = None
depends_on: list[str] | None = None


def upgrade() -> None:
with op.get_context().autocommit_block():
op.execute("SET SESSION statement_timeout = '300s'")
op.execute(
'ALTER TABLE "collective_stock" VALIDATE CONSTRAINT "collective_stock_startDatetime_not_null_constraint"'
)
op.execute(
'ALTER TABLE "collective_stock" VALIDATE CONSTRAINT "collective_stock_endDatetime_not_null_constraint"'
)
op.execute(f"SET SESSION statement_timeout={settings.DATABASE_STATEMENT_TIMEOUT}")


def downgrade() -> None:
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Add NOT NULL constraint on "collective_stock.startDatetime" and "collective_stock.endDatetime" (step 1 of 4)
"""

from alembic import op


# pre/post deployment: post
# revision identifiers, used by Alembic.
revision = "a09ff41c9e94"
down_revision = "b18478ab2ea8"
branch_labels: tuple[str] | None = None
depends_on: list[str] | None = None


def upgrade() -> None:
op.execute(
"""
ALTER TABLE "collective_stock" DROP CONSTRAINT IF EXISTS "collective_stock_startDatetime_not_null_constraint";
ALTER TABLE "collective_stock" ADD CONSTRAINT "collective_stock_startDatetime_not_null_constraint" CHECK ("startDatetime" IS NOT NULL) NOT VALID;
"""
)
op.execute(
"""
ALTER TABLE "collective_stock" DROP CONSTRAINT IF EXISTS "collective_stock_endDatetime_not_null_constraint";
ALTER TABLE "collective_stock" ADD CONSTRAINT "collective_stock_endDatetime_not_null_constraint" CHECK ("endDatetime" IS NOT NULL) NOT VALID;
"""
)


def downgrade() -> None:
op.drop_constraint("collective_stock_startDatetime_not_null_constraint", table_name="collective_stock")
op.drop_constraint("collective_stock_endDatetime_not_null_constraint", table_name="collective_stock")
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Add NOT NULL constraint on "collective_stock.startDatetime" and "collective_stock.endDatetime" (step 4 of 4)
"""

from alembic import op


# pre/post deployment: post
# revision identifiers, used by Alembic.
revision = "a8d2c442cc67"
down_revision = "4302137d444a"
branch_labels: tuple[str] | None = None
depends_on: list[str] | None = None


def upgrade() -> None:
op.drop_constraint("collective_stock_startDatetime_not_null_constraint", table_name="collective_stock")
op.drop_constraint("collective_stock_endDatetime_not_null_constraint", table_name="collective_stock")


def downgrade() -> None:
op.execute(
"""ALTER TABLE "collective_stock" ADD CONSTRAINT "collective_stock_startDatetime_not_null_constraint" CHECK ("startDatetime" IS NOT NULL) NOT VALID"""
)
op.execute(
"""ALTER TABLE "collective_stock" ADD CONSTRAINT "collective_stock_endDatetime_not_null_constraint" CHECK ("endDatetime" IS NOT NULL) NOT VALID"""
)
4 changes: 2 additions & 2 deletions api/src/pcapi/core/educational/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,8 +1421,8 @@ class CollectiveStock(PcObject, Base, Model):

beginningDatetime: datetime = sa.Column(sa.DateTime, index=True, nullable=False)

startDatetime: datetime = sa.Column(sa.DateTime, nullable=True)
endDatetime: datetime = sa.Column(sa.DateTime, nullable=True)
startDatetime: datetime = sa.Column(sa.DateTime, nullable=False)
endDatetime: datetime = sa.Column(sa.DateTime, nullable=False)

collectiveOfferId: int = sa.Column(
sa.BigInteger, sa.ForeignKey("collective_offer.id"), index=True, nullable=False, unique=True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ class CollectiveBookingCollectiveStockResponseModel(BaseModel):
offer_id: int
event_beginning_datetime: str
event_start_datetime: str
event_end_datetime: str | None
event_end_datetime: str
offer_isbn: str | None
offer_is_educational: bool
number_of_tickets: int
booking_limit_datetime: str | None
booking_limit_datetime: str

class Config:
alias_generator = to_camel
Expand Down Expand Up @@ -214,7 +214,7 @@ def _serialize_collective_booking_status_info(
def serialize_collective_booking_stock(
collective_booking: models.CollectiveBooking,
) -> CollectiveBookingCollectiveStockResponseModel:
return CollectiveBookingCollectiveStockResponseModel( # type: ignore[call-arg]
return CollectiveBookingCollectiveStockResponseModel(
offerName=collective_booking.collectiveStock.collectiveOffer.name,
offerId=collective_booking.collectiveStock.collectiveOfferId,
eventBeginningDatetime=typing.cast(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
/* tslint:disable */
/* eslint-disable */
export type CollectiveBookingCollectiveStockResponseModel = {
bookingLimitDatetime?: string | null;
bookingLimitDatetime: string;
eventBeginningDatetime: string;
eventEndDatetime?: string | null;
eventEndDatetime: string;
eventStartDatetime: string;
numberOfTickets: number;
offerId: number;
Expand Down

0 comments on commit 470dbef

Please sign in to comment.