Public Access
mirror of
https://github.com/xlorepdarkhelm/numinar-coding-project.git
synced 2026-09-11 10:28:45 -04:00
95 lines
2.7 KiB
Python
95 lines
2.7 KiB
Python
"""Make bookings.start_time and bookings.end_time timezone-aware (TIMESTAMP WITH TIME ZONE)
|
|
|
|
Revision ID: 0006_make_bookings_tz_aware
|
|
Revises: 0005_adding_title_to_bookings
|
|
Create Date: 2025-08-29
|
|
|
|
"""
|
|
|
|
from typing import Sequence
|
|
from typing import Union
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "0006"
|
|
down_revision: Union[str, Sequence[str], None] = "0005"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""Upgrade schema to use timezone-aware datetimes for bookings."""
|
|
# Drop exclusion constraint and check constraint
|
|
op.execute("ALTER TABLE bookings DROP CONSTRAINT IF EXISTS bookings_no_overlap;")
|
|
op.drop_constraint("ck_bookings_start_before_end", "bookings", type_="check")
|
|
|
|
# Alter columns to TIMESTAMP WITH TIME ZONE
|
|
op.alter_column(
|
|
"bookings",
|
|
"start_time",
|
|
type_=sa.TIMESTAMP(timezone=True),
|
|
existing_type=sa.TIMESTAMP(timezone=False),
|
|
postgresql_using=None,
|
|
)
|
|
op.alter_column(
|
|
"bookings",
|
|
"end_time",
|
|
type_=sa.TIMESTAMP(timezone=True),
|
|
existing_type=sa.TIMESTAMP(timezone=False),
|
|
postgresql_using=None,
|
|
)
|
|
|
|
# Re-add check constraint
|
|
op.create_check_constraint(
|
|
"ck_bookings_start_before_end", "bookings", "start_time < end_time"
|
|
)
|
|
# Re-add exclusion constraint for overlap prevention
|
|
op.execute(
|
|
"""
|
|
ALTER TABLE bookings
|
|
ADD CONSTRAINT bookings_no_overlap
|
|
EXCLUDE USING gist (
|
|
room_id WITH =,
|
|
tstzrange(start_time, end_time) WITH &&
|
|
);
|
|
"""
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""Downgrade schema to use naive datetimes for bookings."""
|
|
op.execute("ALTER TABLE bookings DROP CONSTRAINT IF EXISTS bookings_no_overlap;")
|
|
op.drop_constraint("ck_bookings_start_before_end", "bookings", type_="check")
|
|
|
|
op.alter_column(
|
|
"bookings",
|
|
"start_time",
|
|
type_=sa.TIMESTAMP(timezone=False),
|
|
existing_type=sa.TIMESTAMP(timezone=True),
|
|
postgresql_using=None,
|
|
)
|
|
op.alter_column(
|
|
"bookings",
|
|
"end_time",
|
|
type_=sa.TIMESTAMP(timezone=False),
|
|
existing_type=sa.TIMESTAMP(timezone=True),
|
|
postgresql_using=None,
|
|
)
|
|
|
|
op.create_check_constraint(
|
|
"ck_bookings_start_before_end", "bookings", "start_time < end_time"
|
|
)
|
|
op.execute(
|
|
"""
|
|
ALTER TABLE bookings
|
|
ADD CONSTRAINT bookings_no_overlap
|
|
EXCLUDE USING gist (
|
|
room_id WITH =,
|
|
tsrange(start_time, end_time) WITH &&
|
|
);
|
|
"""
|
|
)
|