Adjusted the migrations so they work on the prod db

This commit is contained in:
Jonas Linter
2025-11-19 19:10:25 +01:00
parent 661a6e830c
commit 278d082215
5 changed files with 144 additions and 22 deletions

View File

@@ -178,9 +178,18 @@ def upgrade() -> None:
["room_number"],
unique=False,
)
op.create_index(
op.f("ix_acked_requests_username"), "acked_requests", ["username"], unique=False
)
# Create index on acked_requests if it doesn't exist
connection = op.get_bind()
inspector = sa.inspect(connection)
# Get existing indices on acked_requests
acked_requests_indices = [idx['name'] for idx in inspector.get_indexes('acked_requests')]
# Only create index if it doesn't exist
if "ix_acked_requests_username" not in acked_requests_indices:
op.create_index(
op.f("ix_acked_requests_username"), "acked_requests", ["username"], unique=False
)
# ### end Alembic commands ###

View File

@@ -20,6 +20,12 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
connection = op.get_bind()
# Check if hashed_customer_id column already exists in reservations
inspector = sa.inspect(connection)
reservations_columns = [col['name'] for col in inspector.get_columns('reservations')]
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('hashed_customers', 'customer_id',
existing_type=sa.INTEGER(),
@@ -29,22 +35,22 @@ def upgrade() -> None:
op.drop_constraint(op.f('reservations_customer_id_fkey'), 'reservations', type_='foreignkey')
op.create_foreign_key(None, 'reservations', 'customers', ['customer_id'], ['id'], ondelete='SET NULL')
# Add hashed_customer_id column to reservations with cascade delete
op.add_column('reservations', sa.Column('hashed_customer_id', sa.Integer(), nullable=True))
op.create_index(op.f('ix_reservations_hashed_customer_id'), 'reservations', ['hashed_customer_id'], unique=False)
op.create_foreign_key(None, 'reservations', 'hashed_customers', ['hashed_customer_id'], ['id'], ondelete='CASCADE')
# ### end Alembic commands ###
# Add hashed_customer_id column to reservations if it doesn't exist
if 'hashed_customer_id' not in reservations_columns:
op.add_column('reservations', sa.Column('hashed_customer_id', sa.Integer(), nullable=True))
op.create_index(op.f('ix_reservations_hashed_customer_id'), 'reservations', ['hashed_customer_id'], unique=False)
op.create_foreign_key(None, 'reservations', 'hashed_customers', ['hashed_customer_id'], ['id'], ondelete='CASCADE')
# Data migration: Populate hashed_customer_id from customer relationship
connection = op.get_bind()
update_stmt = sa.text("""
UPDATE reservations r
SET hashed_customer_id = hc.id
FROM hashed_customers hc
WHERE r.customer_id = hc.customer_id
AND hc.customer_id IS NOT NULL
""")
connection.execute(update_stmt)
# Data migration: Populate hashed_customer_id from customer relationship
update_stmt = sa.text("""
UPDATE reservations r
SET hashed_customer_id = hc.id
FROM hashed_customers hc
WHERE r.customer_id = hc.customer_id
AND hc.customer_id IS NOT NULL
""")
connection.execute(update_stmt)
# ### end Alembic commands ###
def downgrade() -> None:

View File

@@ -20,10 +20,21 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# Add hashed_customer_id column to conversion_guests
op.add_column('conversion_guests', sa.Column('hashed_customer_id', sa.Integer(), nullable=True))
op.create_index(op.f('ix_conversion_guests_hashed_customer_id'), 'conversion_guests', ['hashed_customer_id'], unique=False)
op.create_foreign_key(None, 'conversion_guests', 'hashed_customers', ['hashed_customer_id'], ['id'], ondelete='SET NULL')
connection = op.get_bind()
inspector = sa.inspect(connection)
# Check if conversion_guests table and hashed_customer_id column exist
tables = inspector.get_table_names()
# Only proceed if conversion_guests table exists
if 'conversion_guests' in tables:
conversion_guests_columns = [col['name'] for col in inspector.get_columns('conversion_guests')]
# Add hashed_customer_id column if it doesn't exist
if 'hashed_customer_id' not in conversion_guests_columns:
op.add_column('conversion_guests', sa.Column('hashed_customer_id', sa.Integer(), nullable=True))
op.create_index(op.f('ix_conversion_guests_hashed_customer_id'), 'conversion_guests', ['hashed_customer_id'], unique=False)
op.create_foreign_key(None, 'conversion_guests', 'hashed_customers', ['hashed_customer_id'], ['id'], ondelete='SET NULL')
def downgrade() -> None: