diff --git a/api/alembic_version_conflict_detection.txt b/api/alembic_version_conflict_detection.txt index 79870a1dfb6..281d4bd2091 100644 --- a/api/alembic_version_conflict_detection.txt +++ b/api/alembic_version_conflict_detection.txt @@ -1,2 +1,2 @@ f8588023c126 (pre) (head) -b18478ab2ea8 (post) (head) +4c53718279e7 (post) (head) diff --git a/api/src/pcapi/alembic/versions/20241220T091708_4c53718279e7_drop_headline_offer_unicity_constraints.py b/api/src/pcapi/alembic/versions/20241220T091708_4c53718279e7_drop_headline_offer_unicity_constraints.py new file mode 100644 index 00000000000..ef984a9f1ab --- /dev/null +++ b/api/src/pcapi/alembic/versions/20241220T091708_4c53718279e7_drop_headline_offer_unicity_constraints.py @@ -0,0 +1,77 @@ +""" +Drop headline offer unicity constraints on offer and venue +""" + +from alembic import op + + +# pre/post deployment: post +# revision identifiers, used by Alembic. +revision = "4c53718279e7" +down_revision = "b18478ab2ea8" +branch_labels: tuple[str] | None = None +depends_on: list[str] | None = None + + +def upgrade() -> None: + with op.get_context().autocommit_block(): + op.drop_index( + "ix_headline_offer_offerId", + table_name="headline_offer", + postgresql_concurrently=True, + if_exists=True, + ) + op.create_index( + op.f("ix_headline_offer_offerId"), + "headline_offer", + ["offerId"], + unique=False, + postgresql_concurrently=True, + if_not_exists=True, + ) + op.drop_index( + "ix_headline_offer_venueId", + table_name="headline_offer", + postgresql_concurrently=True, + if_exists=True, + ) + op.create_index( + op.f("ix_headline_offer_venueId"), + "headline_offer", + ["venueId"], + unique=False, + postgresql_concurrently=True, + if_not_exists=True, + ) + + +def downgrade() -> None: + with op.get_context().autocommit_block(): + op.drop_index( + op.f("ix_headline_offer_venueId"), + table_name="headline_offer", + postgresql_concurrently=True, + if_exists=True, + ) + op.create_index( + "ix_headline_offer_venueId", + "headline_offer", + ["venueId"], + unique=True, + postgresql_concurrently=True, + if_not_exists=True, + ) + op.drop_index( + op.f("ix_headline_offer_offerId"), + table_name="headline_offer", + postgresql_concurrently=True, + if_exists=True, + ) + op.create_index( + "ix_headline_offer_offerId", + "headline_offer", + ["offerId"], + unique=True, + postgresql_concurrently=True, + if_not_exists=True, + ) diff --git a/api/src/pcapi/core/offers/models.py b/api/src/pcapi/core/offers/models.py index 1d1409f7d7a..4afcf50d1e7 100644 --- a/api/src/pcapi/core/offers/models.py +++ b/api/src/pcapi/core/offers/models.py @@ -517,10 +517,10 @@ class HeadlineOffer(PcObject, Base, Model): __tablename__ = "headline_offer" offerId: int = sa.Column( - sa.BigInteger, sa.ForeignKey("offer.id", ondelete="CASCADE"), nullable=False, index=True, unique=True + sa.BigInteger, sa.ForeignKey("offer.id", ondelete="CASCADE"), nullable=False, index=True, unique=False ) - offer: sa_orm.Mapped["Offer"] = sa_orm.relationship("Offer", back_populates="headlineOffer") - venueId: int = sa.Column(sa.BigInteger, sa.ForeignKey("venue.id"), nullable=False, index=True, unique=True) + offer: sa_orm.Mapped["Offer"] = sa_orm.relationship("Offer", back_populates="headlineOffers") + venueId: int = sa.Column(sa.BigInteger, sa.ForeignKey("venue.id"), nullable=False, index=True, unique=False) venue: sa_orm.Mapped["Venue"] = sa_orm.relationship("Venue", back_populates="headlineOffers") dateCreated: datetime.datetime = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow) @@ -614,8 +614,8 @@ def __table_args__(self): reactions: list["Reaction"] = sa.orm.relationship( "Reaction", back_populates="offer", uselist=True, cascade="all, delete-orphan", passive_deletes=True ) - headlineOffer: sa_orm.Mapped["HeadlineOffer"] = sa_orm.relationship( - "HeadlineOffer", back_populates="offer", uselist=False + headlineOffers: sa_orm.Mapped[list["HeadlineOffer"]] = sa_orm.relationship( + "HeadlineOffer", back_populates="offer", uselist=True, cascade="all, delete-orphan", passive_deletes=True ) sa.Index("idx_offer_trgm_name", name, postgresql_using="gin")