mirror of
https://github.com/xlorepdarkhelm/numinar-coding-project.git
synced 2026-09-06 22:28:25 -04:00
75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
"""Adding a unique constraint.
|
|
|
|
Revision ID: 0007_adding_unique_constraint_to_invitees
|
|
Revises: 0006_make_bookings_tz_aware
|
|
Create Date: 2025-09-02 16:45:42.662899
|
|
|
|
"""
|
|
|
|
from typing import Sequence
|
|
from typing import Union
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "0007"
|
|
down_revision: Union[str, Sequence[str], None] = "0006"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""Upgrade schema."""
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
# Only create the unique constraint if it does not already exist
|
|
conn = op.get_bind()
|
|
constraint_exists = conn.execute(
|
|
sa.text(
|
|
"""
|
|
SELECT 1 FROM pg_constraint WHERE conname = 'uq_invitee_booking_user'
|
|
"""
|
|
)
|
|
).first()
|
|
if not constraint_exists:
|
|
op.create_unique_constraint(
|
|
"uq_invitee_booking_user", "invitees", ["booking_id", "user_email"]
|
|
)
|
|
op.drop_constraint(op.f("invitees_booking_id_fkey"), "invitees", type_="foreignkey")
|
|
op.drop_constraint(op.f("invitees_user_email_fkey"), "invitees", type_="foreignkey")
|
|
op.create_foreign_key(
|
|
None,
|
|
"invitees",
|
|
"users",
|
|
["user_email"],
|
|
["email"],
|
|
onupdate="CASCADE",
|
|
ondelete="CASCADE",
|
|
)
|
|
op.create_foreign_key(
|
|
None,
|
|
"invitees",
|
|
"bookings",
|
|
["booking_id"],
|
|
["id"],
|
|
onupdate="CASCADE",
|
|
ondelete="CASCADE",
|
|
)
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""Downgrade schema."""
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.drop_constraint("invitees_user_email_fkey", "invitees", type_="foreignkey")
|
|
op.drop_constraint("invitees_booking_id_fkey", "invitees", type_="foreignkey")
|
|
op.create_foreign_key(
|
|
op.f("invitees_user_email_fkey"), "invitees", "users", ["user_email"], ["email"]
|
|
)
|
|
op.create_foreign_key(
|
|
op.f("invitees_booking_id_fkey"), "invitees", "bookings", ["booking_id"], ["id"]
|
|
)
|
|
op.drop_constraint("uq_invitee_booking_user", "invitees", type_="unique")
|
|
# ### end Alembic commands ###
|