From 6e6a6e96725c85cbdc23e0d82bc620d08d7e2d53 Mon Sep 17 00:00:00 2001 From: NguyenND Date: Tue, 16 Jun 2026 10:56:24 -0400 Subject: [PATCH] 06/16 Migration script to update db --- ...4_phase_3_5_schema_messaging_favorites_.py | 222 ++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 migrations/versions/f2d6b231e3e4_phase_3_5_schema_messaging_favorites_.py diff --git a/migrations/versions/f2d6b231e3e4_phase_3_5_schema_messaging_favorites_.py b/migrations/versions/f2d6b231e3e4_phase_3_5_schema_messaging_favorites_.py new file mode 100644 index 0000000..f8649b3 --- /dev/null +++ b/migrations/versions/f2d6b231e3e4_phase_3_5_schema_messaging_favorites_.py @@ -0,0 +1,222 @@ +"""phase 3-5 schema: messaging, favorites, billing, ads + +Revision ID: f2d6b231e3e4 +Revises: 92815e03f343 +Create Date: 2026-06-16 10:54:41.003239 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f2d6b231e3e4' +down_revision = '92815e03f343' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('ads', + sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False), + sa.Column('advertiser_name', sa.String(length=120), nullable=False), + sa.Column('slot', sa.Enum('header', 'sidebar', 'inline', 'footer', name='ad_slot'), nullable=False), + sa.Column('creative_path', sa.String(length=255), nullable=True), + sa.Column('target_url', sa.String(length=512), nullable=False), + sa.Column('alt_text', sa.String(length=200), nullable=True), + sa.Column('lang', sa.String(length=5), nullable=True), + sa.Column('geo_state', sa.String(length=2), nullable=True), + sa.Column('starts_at', sa.DateTime(), nullable=False), + sa.Column('ends_at', sa.DateTime(), nullable=False), + sa.Column('impressions', sa.Integer(), nullable=False), + sa.Column('clicks', sa.Integer(), nullable=False), + sa.Column('is_active', sa.Boolean(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('ads', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_ads_ends_at'), ['ends_at'], unique=False) + batch_op.create_index(batch_op.f('ix_ads_starts_at'), ['starts_at'], unique=False) + + op.create_table('sponsors', + sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False), + sa.Column('name', sa.String(length=120), nullable=False), + sa.Column('logo_path', sa.String(length=255), nullable=True), + sa.Column('url', sa.String(length=512), nullable=False), + sa.Column('tagline', sa.String(length=160), nullable=True), + sa.Column('tier', sa.Enum('directory', 'category', name='sponsor_tier'), nullable=False), + sa.Column('category_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=True), + sa.Column('starts_at', sa.DateTime(), nullable=False), + sa.Column('ends_at', sa.DateTime(), nullable=False), + sa.Column('is_active', sa.Boolean(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('subscriptions', + 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('plan_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False), + sa.Column('stripe_customer_id', sa.String(length=64), nullable=True), + sa.Column('stripe_sub_id', sa.String(length=64), nullable=True), + sa.Column('status', sa.Enum('active', 'past_due', 'canceled', 'trialing', name='sub_status'), nullable=False), + sa.Column('current_period_end', sa.DateTime(), nullable=True), + sa.Column('cancel_at_period_end', sa.Boolean(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['plan_id'], ['plans.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('subscriptions', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_subscriptions_stripe_customer_id'), ['stripe_customer_id'], unique=False) + batch_op.create_index(batch_op.f('ix_subscriptions_stripe_sub_id'), ['stripe_sub_id'], unique=True) + batch_op.create_index(batch_op.f('ix_subscriptions_user_id'), ['user_id'], unique=True) + + op.create_table('transactions', + 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('type', sa.Enum('subscription', 'boost', 'refund', name='txn_type'), nullable=False), + sa.Column('amount_cents', sa.Integer(), nullable=False), + sa.Column('currency', sa.String(length=3), nullable=False), + sa.Column('stripe_object_id', sa.String(length=64), nullable=True), + sa.Column('status', sa.String(length=20), nullable=False), + sa.Column('meta', sa.JSON(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('transactions', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_transactions_stripe_object_id'), ['stripe_object_id'], unique=False) + batch_op.create_index(batch_op.f('ix_transactions_user_id'), ['user_id'], unique=False) + + op.create_table('boosts', + 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('user_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False), + sa.Column('type', sa.Enum('featured', 'bump', 'highlight', 'urgent', name='boost_type'), nullable=False), + sa.Column('expires_at', sa.DateTime(), nullable=False), + sa.Column('transaction_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['listing_id'], ['listings.id'], ), + sa.ForeignKeyConstraint(['transaction_id'], ['transactions.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('boosts', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_boosts_expires_at'), ['expires_at'], unique=False) + batch_op.create_index(batch_op.f('ix_boosts_listing_id'), ['listing_id'], unique=False) + + 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('promoted_keywords', + sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False), + sa.Column('keyword', sa.String(length=80), nullable=False), + sa.Column('listing_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False), + sa.Column('priority', sa.Integer(), nullable=False), + sa.Column('expires_at', sa.DateTime(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['listing_id'], ['listings.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('keyword', 'listing_id', name='uq_pk_kw_listing') + ) + with op.batch_alter_table('promoted_keywords', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_promoted_keywords_expires_at'), ['expires_at'], unique=False) + batch_op.create_index(batch_op.f('ix_promoted_keywords_keyword'), ['keyword'], 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('promoted_keywords', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_promoted_keywords_keyword')) + batch_op.drop_index(batch_op.f('ix_promoted_keywords_expires_at')) + + op.drop_table('promoted_keywords') + 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') + with op.batch_alter_table('boosts', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_boosts_listing_id')) + batch_op.drop_index(batch_op.f('ix_boosts_expires_at')) + + op.drop_table('boosts') + with op.batch_alter_table('transactions', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_transactions_user_id')) + batch_op.drop_index(batch_op.f('ix_transactions_stripe_object_id')) + + op.drop_table('transactions') + with op.batch_alter_table('subscriptions', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_subscriptions_user_id')) + batch_op.drop_index(batch_op.f('ix_subscriptions_stripe_sub_id')) + batch_op.drop_index(batch_op.f('ix_subscriptions_stripe_customer_id')) + + op.drop_table('subscriptions') + op.drop_table('sponsors') + with op.batch_alter_table('ads', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_ads_starts_at')) + batch_op.drop_index(batch_op.f('ix_ads_ends_at')) + + op.drop_table('ads') + # ### end Alembic commands ###