"""Add positions tables Revision ID: 002_add_positions Revises: 001_initial_schema Create Date: 2026-01-20 15:00:00.000000 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = '002_add_positions' down_revision = '001_initial_schema' branch_labels = None depends_on = None def upgrade() -> None: # Create positions table op.create_table( 'positions', sa.Column('id', sa.Integer(), nullable=False), sa.Column('account_id', sa.Integer(), nullable=False), sa.Column('symbol', sa.String(length=50), nullable=False), sa.Column('option_symbol', sa.String(length=100), nullable=True), sa.Column('position_type', sa.Enum('STOCK', 'CALL', 'PUT', name='positiontype'), nullable=False), sa.Column('status', sa.Enum('OPEN', 'CLOSED', name='positionstatus'), nullable=False), sa.Column('open_date', sa.Date(), nullable=False), sa.Column('close_date', sa.Date(), nullable=True), sa.Column('total_quantity', sa.Numeric(precision=20, scale=8), nullable=False), sa.Column('avg_entry_price', sa.Numeric(precision=20, scale=8), nullable=True), sa.Column('avg_exit_price', sa.Numeric(precision=20, scale=8), nullable=True), sa.Column('realized_pnl', sa.Numeric(precision=20, scale=2), nullable=True), sa.Column('unrealized_pnl', sa.Numeric(precision=20, scale=2), nullable=True), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), sa.ForeignKeyConstraint(['account_id'], ['accounts.id'], ondelete='CASCADE'), sa.PrimaryKeyConstraint('id') ) op.create_index(op.f('ix_positions_id'), 'positions', ['id'], unique=False) op.create_index(op.f('ix_positions_account_id'), 'positions', ['account_id'], unique=False) op.create_index(op.f('ix_positions_symbol'), 'positions', ['symbol'], unique=False) op.create_index(op.f('ix_positions_option_symbol'), 'positions', ['option_symbol'], unique=False) op.create_index(op.f('ix_positions_status'), 'positions', ['status'], unique=False) op.create_index('idx_account_status', 'positions', ['account_id', 'status'], unique=False) op.create_index('idx_account_symbol_status', 'positions', ['account_id', 'symbol', 'status'], unique=False) # Create position_transactions junction table op.create_table( 'position_transactions', sa.Column('position_id', sa.Integer(), nullable=False), sa.Column('transaction_id', sa.Integer(), nullable=False), sa.ForeignKeyConstraint(['position_id'], ['positions.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint(['transaction_id'], ['transactions.id'], ondelete='CASCADE'), sa.PrimaryKeyConstraint('position_id', 'transaction_id') ) def downgrade() -> None: op.drop_table('position_transactions') op.drop_index('idx_account_symbol_status', table_name='positions') op.drop_index('idx_account_status', table_name='positions') op.drop_index(op.f('ix_positions_status'), table_name='positions') op.drop_index(op.f('ix_positions_option_symbol'), table_name='positions') op.drop_index(op.f('ix_positions_symbol'), table_name='positions') op.drop_index(op.f('ix_positions_account_id'), table_name='positions') op.drop_index(op.f('ix_positions_id'), table_name='positions') op.drop_table('positions') op.execute('DROP TYPE positionstatus') op.execute('DROP TYPE positiontype')