Migration to single customer table works but conversion_service still needs updating
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
"""merge_hashed_customers_into_customers
|
||||
|
||||
Revision ID: 0fbeb40dbb2c
|
||||
Revises: 694d52a883c3
|
||||
Create Date: 2025-12-03 10:44:32.243220
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '0fbeb40dbb2c'
|
||||
down_revision: Union[str, Sequence[str], None] = '694d52a883c3'
|
||||
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! ###
|
||||
# Add hashed columns to customers table
|
||||
op.add_column('customers', sa.Column('hashed_email', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_phone', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_given_name', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_surname', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_city', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_postal_code', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_country_code', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_gender', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('hashed_birth_date', sa.String(length=64), nullable=True))
|
||||
op.add_column('customers', sa.Column('created_at', sa.DateTime(timezone=True), nullable=True))
|
||||
|
||||
# Migrate data from hashed_customers to customers
|
||||
op.execute('''
|
||||
UPDATE customers c
|
||||
SET
|
||||
hashed_email = hc.hashed_email,
|
||||
hashed_phone = hc.hashed_phone,
|
||||
hashed_given_name = hc.hashed_given_name,
|
||||
hashed_surname = hc.hashed_surname,
|
||||
hashed_city = hc.hashed_city,
|
||||
hashed_postal_code = hc.hashed_postal_code,
|
||||
hashed_country_code = hc.hashed_country_code,
|
||||
hashed_gender = hc.hashed_gender,
|
||||
hashed_birth_date = hc.hashed_birth_date,
|
||||
created_at = COALESCE(c.created_at, hc.created_at)
|
||||
FROM hashed_customers hc
|
||||
WHERE c.id = hc.customer_id
|
||||
''')
|
||||
|
||||
# Update reservations to point to customers instead of hashed_customers
|
||||
# First, update reservations.customer_id from reservations.hashed_customer_id
|
||||
op.execute('''
|
||||
UPDATE reservations r
|
||||
SET customer_id = hc.customer_id
|
||||
FROM hashed_customers hc
|
||||
WHERE r.hashed_customer_id = hc.id
|
||||
AND r.customer_id IS NULL
|
||||
''')
|
||||
|
||||
# Update conversions to point to customers instead of hashed_customers
|
||||
op.execute('''
|
||||
UPDATE conversions c
|
||||
SET customer_id = hc.customer_id
|
||||
FROM hashed_customers hc
|
||||
WHERE c.hashed_customer_id = hc.id
|
||||
AND c.customer_id IS NULL
|
||||
''')
|
||||
|
||||
# Update conversion_guests to point to customers instead of hashed_customers
|
||||
op.execute('''
|
||||
UPDATE conversion_guests cg
|
||||
SET hashed_customer_id = NULL
|
||||
WHERE hashed_customer_id IS NOT NULL
|
||||
''')
|
||||
|
||||
# Now safe to drop the FK and column from reservations
|
||||
op.drop_constraint(op.f('reservations_hashed_customer_id_fkey'), 'reservations', type_='foreignkey')
|
||||
op.drop_column('reservations', 'hashed_customer_id')
|
||||
|
||||
# Note: We're keeping the hashed_customers table for now since conversion_service.py still uses it
|
||||
# It can be dropped in a future migration after updating the application code
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('reservations', sa.Column('hashed_customer_id', sa.INTEGER(), autoincrement=False, nullable=True))
|
||||
op.create_foreign_key(op.f('reservations_hashed_customer_id_fkey'), 'reservations', 'hashed_customers', ['hashed_customer_id'], ['id'], ondelete='CASCADE')
|
||||
op.drop_column('customers', 'created_at')
|
||||
op.drop_column('customers', 'hashed_birth_date')
|
||||
op.drop_column('customers', 'hashed_gender')
|
||||
op.drop_column('customers', 'hashed_country_code')
|
||||
op.drop_column('customers', 'hashed_postal_code')
|
||||
op.drop_column('customers', 'hashed_city')
|
||||
op.drop_column('customers', 'hashed_surname')
|
||||
op.drop_column('customers', 'hashed_given_name')
|
||||
op.drop_column('customers', 'hashed_phone')
|
||||
op.drop_column('customers', 'hashed_email')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user