Big refactor of gift categories model
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
"""refactor_gift_categories_to_support_multiple_events
|
||||
|
||||
Revision ID: 60c6bfef5416
|
||||
Revises: 38bf9e7e74b3
|
||||
Create Date: 2025-03-16 14:48:08.137051
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
from sqlalchemy import text
|
||||
from datetime import datetime
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '60c6bfef5416'
|
||||
down_revision: Union[str, None] = '38bf9e7e74b3'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('event_gift_categories',
|
||||
sa.Column('event_id', sa.UUID(), nullable=False),
|
||||
sa.Column('category_id', sa.UUID(), nullable=False),
|
||||
sa.Column('display_order', sa.Integer(), nullable=True),
|
||||
sa.Column('is_visible', sa.Boolean(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['category_id'], ['gift_categories.id'], ),
|
||||
sa.ForeignKeyConstraint(['event_id'], ['events.id'], ),
|
||||
sa.PrimaryKeyConstraint('event_id', 'category_id')
|
||||
)
|
||||
|
||||
# Migrate existing data from gift_categories to event_gift_categories
|
||||
# Get connection
|
||||
connection = op.get_bind()
|
||||
|
||||
# Get all existing gift categories
|
||||
categories = connection.execute(
|
||||
text("SELECT id, event_id, display_order, is_visible FROM gift_categories")
|
||||
).fetchall()
|
||||
|
||||
# Insert data into the new association table
|
||||
for category in categories:
|
||||
category_id = category[0]
|
||||
event_id = category[1]
|
||||
display_order = category[2] if category[2] is not None else 0
|
||||
is_visible = category[3] if category[3] is not None else True
|
||||
now = datetime.now().isoformat()
|
||||
|
||||
connection.execute(
|
||||
text(f"""
|
||||
INSERT INTO event_gift_categories
|
||||
(event_id, category_id, display_order, is_visible, created_at)
|
||||
VALUES
|
||||
('{event_id}', '{category_id}', {display_order}, {is_visible}, '{now}')
|
||||
""")
|
||||
)
|
||||
|
||||
# Now we can safely modify the gift_categories table
|
||||
op.alter_column('event_themes', 'asset_image_urls',
|
||||
existing_type=postgresql.JSON(astext_type=sa.Text()),
|
||||
nullable=True)
|
||||
op.drop_constraint('uq_event_category_name', 'gift_categories', type_='unique')
|
||||
op.create_unique_constraint(None, 'gift_categories', ['name'])
|
||||
op.drop_constraint('gift_categories_event_id_fkey', 'gift_categories', type_='foreignkey')
|
||||
op.drop_column('gift_categories', 'display_order')
|
||||
op.drop_column('gift_categories', 'event_id')
|
||||
op.drop_column('gift_categories', 'is_visible')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('gift_categories', sa.Column('is_visible', sa.BOOLEAN(), autoincrement=False, nullable=True))
|
||||
op.add_column('gift_categories', sa.Column('event_id', sa.UUID(), autoincrement=False, nullable=False))
|
||||
op.add_column('gift_categories', sa.Column('display_order', sa.INTEGER(), autoincrement=False, nullable=True))
|
||||
op.create_foreign_key('gift_categories_event_id_fkey', 'gift_categories', 'events', ['event_id'], ['id'])
|
||||
op.drop_constraint(None, 'gift_categories', type_='unique')
|
||||
op.create_unique_constraint('uq_event_category_name', 'gift_categories', ['event_id', 'name'])
|
||||
op.alter_column('event_themes', 'asset_image_urls',
|
||||
existing_type=postgresql.JSON(astext_type=sa.Text()),
|
||||
nullable=False)
|
||||
op.drop_table('event_gift_categories')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user