Migration successfull. Does not cause any problems and new foreign keys work
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
"""Id columns changed to integer, foreign_keys added
|
||||
|
||||
Revision ID: b50c0f45030a
|
||||
Revises: b2cfe2d3aabc
|
||||
Create Date: 2025-12-02 11:06:25.850790
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'b50c0f45030a'
|
||||
down_revision: Union[str, Sequence[str], None] = 'b2cfe2d3aabc'
|
||||
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! ###
|
||||
|
||||
# Drop composite FK constraint first (references guest_id columns)
|
||||
op.drop_constraint(
|
||||
'conversions_hotel_id_guest_id_fkey', 'conversions', type_='foreignkey'
|
||||
)
|
||||
|
||||
# Now convert the guest_id columns
|
||||
op.alter_column('conversion_guests', 'guest_id',
|
||||
existing_type=sa.VARCHAR(),
|
||||
type_=sa.Integer(),
|
||||
existing_nullable=False,
|
||||
postgresql_using='guest_id::integer')
|
||||
op.alter_column('conversion_guests', 'is_regular',
|
||||
existing_type=sa.BOOLEAN(),
|
||||
nullable=True)
|
||||
op.drop_constraint(op.f('conversion_guests_hashed_customer_id_fkey'), 'conversion_guests', type_='foreignkey')
|
||||
op.create_foreign_key(op.f('fk_conversion_guests_hashed_customer_id_hashed_customers'), 'conversion_guests', 'hashed_customers', ['hashed_customer_id'], ['id'])
|
||||
# Create FK with NOT VALID to skip checking existing data
|
||||
# (hotels table will be populated from config when app starts)
|
||||
op.create_foreign_key(
|
||||
op.f('fk_conversion_guests_hotel_id_hotels'),
|
||||
'conversion_guests',
|
||||
'hotels',
|
||||
['hotel_id'],
|
||||
['hotel_id'],
|
||||
ondelete='CASCADE',
|
||||
postgresql_not_valid=True
|
||||
)
|
||||
op.alter_column('conversions', 'hotel_id',
|
||||
existing_type=sa.VARCHAR(),
|
||||
nullable=False)
|
||||
op.alter_column('conversions', 'pms_reservation_id',
|
||||
existing_type=sa.VARCHAR(),
|
||||
type_=sa.Integer(),
|
||||
nullable=False,
|
||||
postgresql_using='pms_reservation_id::integer')
|
||||
op.alter_column('conversions', 'guest_id',
|
||||
existing_type=sa.VARCHAR(),
|
||||
type_=sa.Integer(),
|
||||
existing_nullable=True,
|
||||
postgresql_using='guest_id::integer')
|
||||
op.alter_column('conversions', 'directly_attributable',
|
||||
existing_type=sa.BOOLEAN(),
|
||||
nullable=True)
|
||||
op.alter_column('conversions', 'guest_matched',
|
||||
existing_type=sa.BOOLEAN(),
|
||||
nullable=True)
|
||||
|
||||
# Re-create composite FK constraint after column type changes
|
||||
op.create_foreign_key(
|
||||
'conversions_hotel_id_guest_id_fkey',
|
||||
'conversions',
|
||||
'conversion_guests',
|
||||
['hotel_id', 'guest_id'],
|
||||
['hotel_id', 'guest_id'],
|
||||
ondelete='SET NULL'
|
||||
)
|
||||
|
||||
op.create_unique_constraint('uq_conversion_hotel_reservation', 'conversions', ['hotel_id', 'pms_reservation_id'])
|
||||
# Create FK with NOT VALID for same reason as above
|
||||
op.create_foreign_key(
|
||||
op.f('fk_conversions_hotel_id_hotels'),
|
||||
'conversions',
|
||||
'hotels',
|
||||
['hotel_id'],
|
||||
['hotel_id'],
|
||||
ondelete='CASCADE',
|
||||
postgresql_not_valid=True
|
||||
)
|
||||
op.drop_constraint(op.f('customers_contact_id_key'), 'customers', type_='unique')
|
||||
op.create_unique_constraint(op.f('uq_customers_contact_id'), 'customers', ['contact_id'])
|
||||
op.drop_constraint(op.f('hashed_customers_contact_id_key'), 'hashed_customers', type_='unique')
|
||||
op.drop_constraint(op.f('hashed_customers_customer_id_key'), 'hashed_customers', type_='unique')
|
||||
op.create_unique_constraint(op.f('uq_hashed_customers_contact_id'), 'hashed_customers', ['contact_id'])
|
||||
op.create_unique_constraint(op.f('uq_hashed_customers_customer_id'), 'hashed_customers', ['customer_id'])
|
||||
op.drop_index(op.f('ix_reservations_hashed_customer_id'), table_name='reservations')
|
||||
op.drop_constraint(op.f('reservations_md5_unique_id_key'), 'reservations', type_='unique')
|
||||
op.drop_constraint(op.f('reservations_unique_id_key'), 'reservations', type_='unique')
|
||||
op.create_unique_constraint(op.f('uq_reservations_md5_unique_id'), 'reservations', ['md5_unique_id'])
|
||||
op.create_unique_constraint(op.f('uq_reservations_unique_id'), 'reservations', ['unique_id'])
|
||||
op.drop_index(op.f('idx_room_availability_inventory_date'), table_name='room_availability')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_index(op.f('idx_room_availability_inventory_date'), 'room_availability', ['inventory_id', 'date'], unique=False)
|
||||
op.drop_constraint(op.f('uq_reservations_unique_id'), 'reservations', type_='unique')
|
||||
op.drop_constraint(op.f('uq_reservations_md5_unique_id'), 'reservations', type_='unique')
|
||||
op.create_unique_constraint(op.f('reservations_unique_id_key'), 'reservations', ['unique_id'], postgresql_nulls_not_distinct=False)
|
||||
op.create_unique_constraint(op.f('reservations_md5_unique_id_key'), 'reservations', ['md5_unique_id'], postgresql_nulls_not_distinct=False)
|
||||
op.create_index(op.f('ix_reservations_hashed_customer_id'), 'reservations', ['hashed_customer_id'], unique=False)
|
||||
op.drop_constraint(op.f('uq_hashed_customers_customer_id'), 'hashed_customers', type_='unique')
|
||||
op.drop_constraint(op.f('uq_hashed_customers_contact_id'), 'hashed_customers', type_='unique')
|
||||
op.create_unique_constraint(op.f('hashed_customers_customer_id_key'), 'hashed_customers', ['customer_id'], postgresql_nulls_not_distinct=False)
|
||||
op.create_unique_constraint(op.f('hashed_customers_contact_id_key'), 'hashed_customers', ['contact_id'], postgresql_nulls_not_distinct=False)
|
||||
op.drop_constraint(op.f('uq_customers_contact_id'), 'customers', type_='unique')
|
||||
op.create_unique_constraint(op.f('customers_contact_id_key'), 'customers', ['contact_id'], postgresql_nulls_not_distinct=False)
|
||||
op.drop_constraint(op.f('fk_conversions_hotel_id_hotels'), 'conversions', type_='foreignkey')
|
||||
op.drop_constraint('uq_conversion_hotel_reservation', 'conversions', type_='unique')
|
||||
|
||||
# Drop composite FK constraint before changing column types back
|
||||
op.drop_constraint(
|
||||
'conversions_hotel_id_guest_id_fkey', 'conversions', type_='foreignkey'
|
||||
)
|
||||
op.alter_column('conversions', 'guest_matched',
|
||||
existing_type=sa.BOOLEAN(),
|
||||
nullable=False)
|
||||
op.alter_column('conversions', 'directly_attributable',
|
||||
existing_type=sa.BOOLEAN(),
|
||||
nullable=False)
|
||||
op.alter_column('conversions', 'guest_id',
|
||||
existing_type=sa.Integer(),
|
||||
type_=sa.VARCHAR(),
|
||||
existing_nullable=True)
|
||||
op.alter_column('conversions', 'pms_reservation_id',
|
||||
existing_type=sa.Integer(),
|
||||
type_=sa.VARCHAR(),
|
||||
nullable=True)
|
||||
op.alter_column('conversions', 'hotel_id',
|
||||
existing_type=sa.VARCHAR(),
|
||||
nullable=True)
|
||||
op.drop_constraint(op.f('fk_conversion_guests_hotel_id_hotels'), 'conversion_guests', type_='foreignkey')
|
||||
op.drop_constraint(op.f('fk_conversion_guests_hashed_customer_id_hashed_customers'), 'conversion_guests', type_='foreignkey')
|
||||
op.create_foreign_key(op.f('conversion_guests_hashed_customer_id_fkey'), 'conversion_guests', 'hashed_customers', ['hashed_customer_id'], ['id'], ondelete='SET NULL')
|
||||
op.alter_column('conversion_guests', 'is_regular',
|
||||
existing_type=sa.BOOLEAN(),
|
||||
nullable=False)
|
||||
op.alter_column('conversion_guests', 'guest_id',
|
||||
existing_type=sa.Integer(),
|
||||
type_=sa.VARCHAR(),
|
||||
existing_nullable=False)
|
||||
|
||||
# Re-create composite FK constraint after reverting column types
|
||||
op.create_foreign_key(
|
||||
'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