90 lines
4.4 KiB
Python
90 lines
4.4 KiB
Python
"""add favorites table
|
|
|
|
Revision ID: 247c26a69e33
|
|
Revises: 92815e03f343
|
|
Create Date: 2026-06-15 16:53:53.086035
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '247c26a69e33'
|
|
down_revision = '92815e03f343'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.create_table('conversations',
|
|
sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False),
|
|
sa.Column('listing_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
|
|
sa.Column('buyer_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
|
|
sa.Column('seller_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
|
|
sa.Column('last_message_at', sa.DateTime(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['buyer_id'], ['users.id'], ),
|
|
sa.ForeignKeyConstraint(['listing_id'], ['listings.id'], ),
|
|
sa.ForeignKeyConstraint(['seller_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('listing_id', 'buyer_id', name='uq_conv_listing_buyer')
|
|
)
|
|
with op.batch_alter_table('conversations', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_conversations_buyer_id'), ['buyer_id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_conversations_last_message_at'), ['last_message_at'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_conversations_listing_id'), ['listing_id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_conversations_seller_id'), ['seller_id'], unique=False)
|
|
|
|
op.create_table('favorites',
|
|
sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False),
|
|
sa.Column('user_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
|
|
sa.Column('listing_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['listing_id'], ['listings.id'], ),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('user_id', 'listing_id', name='uq_fav_user_listing')
|
|
)
|
|
with op.batch_alter_table('favorites', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_favorites_listing_id'), ['listing_id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_favorites_user_id'), ['user_id'], unique=False)
|
|
|
|
op.create_table('messages',
|
|
sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False),
|
|
sa.Column('conversation_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
|
|
sa.Column('sender_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
|
|
sa.Column('body', sa.Text(), nullable=False),
|
|
sa.Column('read_at', sa.DateTime(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ),
|
|
sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
with op.batch_alter_table('messages', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_messages_conversation_id'), ['conversation_id'], unique=False)
|
|
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
def downgrade():
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
with op.batch_alter_table('messages', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_messages_conversation_id'))
|
|
|
|
op.drop_table('messages')
|
|
with op.batch_alter_table('favorites', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_favorites_user_id'))
|
|
batch_op.drop_index(batch_op.f('ix_favorites_listing_id'))
|
|
|
|
op.drop_table('favorites')
|
|
with op.batch_alter_table('conversations', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_conversations_seller_id'))
|
|
batch_op.drop_index(batch_op.f('ix_conversations_listing_id'))
|
|
batch_op.drop_index(batch_op.f('ix_conversations_last_message_at'))
|
|
batch_op.drop_index(batch_op.f('ix_conversations_buyer_id'))
|
|
|
|
op.drop_table('conversations')
|
|
# ### end Alembic commands ###
|