Holy db migrations batman
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""remove_composite_fk_from_conversions
|
||||
|
||||
Revision ID: 694d52a883c3
|
||||
Revises: b50c0f45030a
|
||||
Create Date: 2025-12-03 09:50:18.506030
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '694d52a883c3'
|
||||
down_revision: Union[str, Sequence[str], None] = 'b50c0f45030a'
|
||||
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! ###
|
||||
op.drop_constraint(op.f('conversions_hotel_id_guest_id_fkey'), 'conversions', type_='foreignkey')
|
||||
|
||||
# Rename hotel_code to hotel_id (preserving data) and add FK to hotels
|
||||
op.add_column('reservations', sa.Column('hotel_id', sa.String(), nullable=True))
|
||||
op.execute('UPDATE reservations SET hotel_id = hotel_code')
|
||||
op.drop_column('reservations', 'hotel_code')
|
||||
|
||||
# Add FK constraint without immediate validation (NOT VALID)
|
||||
# This allows existing rows with non-existent hotel_ids to remain
|
||||
# Future inserts/updates will still be validated
|
||||
op.execute(
|
||||
'ALTER TABLE reservations ADD CONSTRAINT fk_reservations_hotel_id_hotels '
|
||||
'FOREIGN KEY (hotel_id) REFERENCES hotels (hotel_id) ON DELETE CASCADE NOT VALID'
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
# Drop FK and rename hotel_id back to hotel_code (preserving data)
|
||||
op.drop_constraint(op.f('fk_reservations_hotel_id_hotels'), 'reservations', type_='foreignkey')
|
||||
op.add_column('reservations', sa.Column('hotel_code', sa.VARCHAR(), autoincrement=False, nullable=True))
|
||||
op.execute('UPDATE reservations SET hotel_code = hotel_id')
|
||||
op.drop_column('reservations', 'hotel_id')
|
||||
|
||||
op.create_foreign_key(op.f('conversions_hotel_id_guest_id_fkey'), 'conversions', 'conversion_guests', ['hotel_id', 'guest_id'], ['hotel_id', 'guest_id'], ondelete='SET NULL')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user