Introduced groups and added all related endpoints
All checks were successful
AWS Deploy on Push / build (push) Successful in 7m39s
All checks were successful
AWS Deploy on Push / build (push) Successful in 7m39s
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
"""add groups table and migrate group_title data
|
||||
|
||||
Revision ID: 0a455608256f
|
||||
Revises: 95b61a92455a
|
||||
Create Date: 2025-06-10 09:22:11.820035
|
||||
|
||||
"""
|
||||
import uuid
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '0a455608256f'
|
||||
down_revision: Union[str, None] = '95b61a92455a'
|
||||
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.create_table('groups',
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
sa.Column('name', sa.String(), nullable=False),
|
||||
sa.Column('sort_order', sa.Integer(), nullable=False, server_default='0'),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('name')
|
||||
)
|
||||
|
||||
# Create temporary table for group mapping
|
||||
group_mapping = op.create_table(
|
||||
'group_mapping',
|
||||
sa.Column('group_title', sa.String(), nullable=False),
|
||||
sa.Column('group_id', sa.UUID(), nullable=False)
|
||||
)
|
||||
|
||||
# Get existing group titles and create groups
|
||||
conn = op.get_bind()
|
||||
distinct_groups = conn.execute(
|
||||
sa.text("SELECT DISTINCT group_title FROM channels")
|
||||
).fetchall()
|
||||
|
||||
for group in distinct_groups:
|
||||
group_title = group[0]
|
||||
group_id = str(uuid.uuid4())
|
||||
conn.execute(
|
||||
sa.text(
|
||||
"INSERT INTO groups (id, name, sort_order) "
|
||||
"VALUES (:id, :name, 0)"
|
||||
).bindparams(id=group_id, name=group_title)
|
||||
)
|
||||
conn.execute(
|
||||
group_mapping.insert().values(
|
||||
group_title=group_title,
|
||||
group_id=group_id
|
||||
)
|
||||
)
|
||||
|
||||
# Add group_id column (nullable first)
|
||||
op.add_column('channels', sa.Column('group_id', sa.UUID(), nullable=True))
|
||||
|
||||
# Update channels with group_ids
|
||||
conn.execute(
|
||||
sa.text(
|
||||
"UPDATE channels c SET group_id = gm.group_id "
|
||||
"FROM group_mapping gm WHERE c.group_title = gm.group_title"
|
||||
)
|
||||
)
|
||||
|
||||
# Now make group_id non-nullable and add constraints
|
||||
op.alter_column('channels', 'group_id', nullable=False)
|
||||
op.drop_constraint(op.f('uix_group_title_name'), 'channels', type_='unique')
|
||||
op.create_unique_constraint('uix_group_id_name', 'channels', ['group_id', 'name'])
|
||||
op.create_foreign_key('fk_channels_group_id', 'channels', 'groups', ['group_id'], ['id'])
|
||||
|
||||
# Clean up and drop group_title
|
||||
op.drop_table('group_mapping')
|
||||
op.drop_column('channels', 'group_title')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('channels', sa.Column('group_title', sa.VARCHAR(), autoincrement=False, nullable=True))
|
||||
|
||||
# Restore group_title values from groups table
|
||||
conn = op.get_bind()
|
||||
conn.execute(
|
||||
sa.text(
|
||||
"UPDATE channels c SET group_title = g.name "
|
||||
"FROM groups g WHERE c.group_id = g.id"
|
||||
)
|
||||
)
|
||||
|
||||
# Now make group_title non-nullable
|
||||
op.alter_column('channels', 'group_title', nullable=False)
|
||||
|
||||
# Drop constraints and columns
|
||||
op.drop_constraint('fk_channels_group_id', 'channels', type_='foreignkey')
|
||||
op.drop_constraint('uix_group_id_name', 'channels', type_='unique')
|
||||
op.create_unique_constraint(op.f('uix_group_title_name'), 'channels', ['group_title', 'name'])
|
||||
op.drop_column('channels', 'group_id')
|
||||
op.drop_table('groups')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user