
- Set up project structure with FastAPI - Implement user and account management - Add send and receive money functionality - Set up transaction processing system - Add JWT authentication - Configure SQLAlchemy with SQLite - Set up Alembic for database migrations - Create comprehensive API documentation
118 lines
5.0 KiB
Python
118 lines
5.0 KiB
Python
"""Initial migration
|
|
|
|
Revision ID: f86c03d2e03d
|
|
Revises:
|
|
Create Date: 2023-06-01 12:00:00.000000
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
import enum
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = 'f86c03d2e03d'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
# Enums for account and transaction tables
|
|
class AccountType(str, enum.Enum):
|
|
SAVINGS = "savings"
|
|
CHECKING = "checking"
|
|
INVESTMENT = "investment"
|
|
|
|
|
|
class TransactionType(str, enum.Enum):
|
|
DEPOSIT = "deposit"
|
|
WITHDRAWAL = "withdrawal"
|
|
TRANSFER = "transfer"
|
|
|
|
|
|
class TransactionStatus(str, enum.Enum):
|
|
PENDING = "pending"
|
|
COMPLETED = "completed"
|
|
FAILED = "failed"
|
|
CANCELLED = "cancelled"
|
|
|
|
|
|
def upgrade():
|
|
# Create users table
|
|
op.create_table(
|
|
'users',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('email', sa.String(), nullable=False),
|
|
sa.Column('first_name', sa.String(), nullable=False),
|
|
sa.Column('last_name', sa.String(), nullable=False),
|
|
sa.Column('hashed_password', sa.String(), nullable=False),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True, default=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)')),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
|
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
|
|
|
# Create accounts table
|
|
op.create_table(
|
|
'accounts',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('account_number', sa.String(), nullable=False),
|
|
sa.Column('owner_id', sa.Integer(), nullable=False),
|
|
sa.Column('account_type', sa.Enum('savings', 'checking', 'investment', name='accounttype'), nullable=False),
|
|
sa.Column('balance', sa.Float(), nullable=False, default=0.0),
|
|
sa.Column('currency', sa.String(), nullable=False, default='USD'),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)')),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
|
sa.ForeignKeyConstraint(['owner_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_accounts_account_number'), 'accounts', ['account_number'], unique=True)
|
|
op.create_index(op.f('ix_accounts_id'), 'accounts', ['id'], unique=False)
|
|
|
|
# Create transactions table
|
|
op.create_table(
|
|
'transactions',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('transaction_reference', sa.String(), nullable=False),
|
|
sa.Column('sender_id', sa.Integer(), nullable=True),
|
|
sa.Column('sender_account_id', sa.Integer(), nullable=True),
|
|
sa.Column('receiver_id', sa.Integer(), nullable=True),
|
|
sa.Column('receiver_account_id', sa.Integer(), nullable=True),
|
|
sa.Column('amount', sa.Float(), nullable=False),
|
|
sa.Column('currency', sa.String(), nullable=False, default='USD'),
|
|
sa.Column('transaction_type', sa.Enum('deposit', 'withdrawal', 'transfer', name='transactiontype'), nullable=False),
|
|
sa.Column('status', sa.Enum('pending', 'completed', 'failed', 'cancelled', name='transactionstatus'), nullable=False, default='pending'),
|
|
sa.Column('description', sa.String(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)')),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
|
|
sa.ForeignKeyConstraint(['receiver_account_id'], ['accounts.id'], ),
|
|
sa.ForeignKeyConstraint(['receiver_id'], ['users.id'], ),
|
|
sa.ForeignKeyConstraint(['sender_account_id'], ['accounts.id'], ),
|
|
sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_transactions_id'), 'transactions', ['id'], unique=False)
|
|
op.create_index(op.f('ix_transactions_transaction_reference'), 'transactions', ['transaction_reference'], unique=True)
|
|
|
|
|
|
def downgrade():
|
|
# Drop tables in reverse order
|
|
op.drop_index(op.f('ix_transactions_transaction_reference'), table_name='transactions')
|
|
op.drop_index(op.f('ix_transactions_id'), table_name='transactions')
|
|
op.drop_table('transactions')
|
|
|
|
op.drop_index(op.f('ix_accounts_id'), table_name='accounts')
|
|
op.drop_index(op.f('ix_accounts_account_number'), table_name='accounts')
|
|
op.drop_table('accounts')
|
|
|
|
op.drop_index(op.f('ix_users_id'), table_name='users')
|
|
op.drop_index(op.f('ix_users_email'), table_name='users')
|
|
op.drop_table('users')
|
|
|
|
# Drop enum types
|
|
sa.Enum(name='transactionstatus').drop(op.get_bind(), checkfirst=False)
|
|
sa.Enum(name='transactiontype').drop(op.get_bind(), checkfirst=False)
|
|
sa.Enum(name='accounttype').drop(op.get_bind(), checkfirst=False) |