Automated Action 3a29a346a9 Initial implementation of cryptocurrency data API service using CoinCap API
* Created FastAPI application structure
* Added database models for assets, exchanges, markets, and rates
* Integrated with CoinCap API
* Implemented REST API endpoints
* Setup SQLite persistence with Alembic migrations
* Added comprehensive documentation

Generated with BackendIM... (backend.im)
2025-05-14 12:18:27 +00:00

140 lines
6.5 KiB
Python

"""Initial migration
Revision ID: 01_initial_migration
Revises:
Create Date: 2025-05-14
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '01_initial_migration'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create Asset table
op.create_table(
'assets',
sa.Column('id', sa.String(), nullable=False),
sa.Column('rank', sa.Integer(), nullable=True),
sa.Column('symbol', sa.String(), nullable=True),
sa.Column('name', sa.String(), nullable=True),
sa.Column('supply', sa.Float(), nullable=True),
sa.Column('max_supply', sa.Float(), nullable=True),
sa.Column('market_cap_usd', sa.Float(), nullable=True),
sa.Column('volume_usd_24hr', sa.Float(), nullable=True),
sa.Column('price_usd', sa.Float(), nullable=True),
sa.Column('change_percent_24hr', sa.Float(), nullable=True),
sa.Column('vwap_24hr', sa.Float(), nullable=True),
sa.Column('explorer', sa.String(), nullable=True),
sa.Column('last_updated', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_assets_id'), 'assets', ['id'], unique=False)
op.create_index(op.f('ix_assets_name'), 'assets', ['name'], unique=False)
op.create_index(op.f('ix_assets_symbol'), 'assets', ['symbol'], unique=False)
# Create AssetPriceHistory table
op.create_table(
'asset_price_history',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('asset_id', sa.String(), nullable=True),
sa.Column('price_usd', sa.Float(), nullable=True),
sa.Column('timestamp', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['asset_id'], ['assets.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_asset_price_history_asset_id'), 'asset_price_history', ['asset_id'], unique=False)
op.create_index(op.f('ix_asset_price_history_id'), 'asset_price_history', ['id'], unique=False)
op.create_index(op.f('ix_asset_price_history_timestamp'), 'asset_price_history', ['timestamp'], unique=False)
# Create Exchange table
op.create_table(
'exchanges',
sa.Column('exchange_id', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('rank', sa.Integer(), nullable=True),
sa.Column('percent_total_volume', sa.Float(), nullable=True),
sa.Column('volume_usd', sa.Float(), nullable=True),
sa.Column('trading_pairs', sa.Integer(), nullable=True),
sa.Column('socket', sa.Boolean(), nullable=True),
sa.Column('exchange_url', sa.String(), nullable=True),
sa.Column('updated_timestamp', sa.Integer(), nullable=True),
sa.Column('last_updated', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('exchange_id')
)
op.create_index(op.f('ix_exchanges_exchange_id'), 'exchanges', ['exchange_id'], unique=False)
op.create_index(op.f('ix_exchanges_name'), 'exchanges', ['name'], unique=False)
# Create Market table
op.create_table(
'markets',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('exchange_id', sa.String(), nullable=True),
sa.Column('rank', sa.Integer(), nullable=True),
sa.Column('base_symbol', sa.String(), nullable=True),
sa.Column('base_id', sa.String(), nullable=True),
sa.Column('quote_symbol', sa.String(), nullable=True),
sa.Column('quote_id', sa.String(), nullable=True),
sa.Column('price_quote', sa.Float(), nullable=True),
sa.Column('price_usd', sa.Float(), nullable=True),
sa.Column('volume_usd_24hr', sa.Float(), nullable=True),
sa.Column('percent_exchange_volume', sa.Float(), nullable=True),
sa.Column('trades_count_24hr', sa.Integer(), nullable=True),
sa.Column('updated_timestamp', sa.Integer(), nullable=True),
sa.Column('last_updated', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_markets_base_id'), 'markets', ['base_id'], unique=False)
op.create_index(op.f('ix_markets_base_symbol'), 'markets', ['base_symbol'], unique=False)
op.create_index(op.f('ix_markets_exchange_id'), 'markets', ['exchange_id'], unique=False)
op.create_index(op.f('ix_markets_quote_id'), 'markets', ['quote_id'], unique=False)
op.create_index(op.f('ix_markets_quote_symbol'), 'markets', ['quote_symbol'], unique=False)
# Create Rate table
op.create_table(
'rates',
sa.Column('id', sa.String(), nullable=False),
sa.Column('symbol', sa.String(), nullable=True),
sa.Column('currency_symbol', sa.String(), nullable=True),
sa.Column('type', sa.String(), nullable=True),
sa.Column('rate_usd', sa.Float(), nullable=True),
sa.Column('last_updated', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_rates_id'), 'rates', ['id'], unique=False)
op.create_index(op.f('ix_rates_symbol'), 'rates', ['symbol'], unique=False)
def downgrade() -> None:
op.drop_index(op.f('ix_rates_symbol'), table_name='rates')
op.drop_index(op.f('ix_rates_id'), table_name='rates')
op.drop_table('rates')
op.drop_index(op.f('ix_markets_quote_symbol'), table_name='markets')
op.drop_index(op.f('ix_markets_quote_id'), table_name='markets')
op.drop_index(op.f('ix_markets_exchange_id'), table_name='markets')
op.drop_index(op.f('ix_markets_base_symbol'), table_name='markets')
op.drop_index(op.f('ix_markets_base_id'), table_name='markets')
op.drop_table('markets')
op.drop_index(op.f('ix_exchanges_name'), table_name='exchanges')
op.drop_index(op.f('ix_exchanges_exchange_id'), table_name='exchanges')
op.drop_table('exchanges')
op.drop_index(op.f('ix_asset_price_history_timestamp'), table_name='asset_price_history')
op.drop_index(op.f('ix_asset_price_history_id'), table_name='asset_price_history')
op.drop_index(op.f('ix_asset_price_history_asset_id'), table_name='asset_price_history')
op.drop_table('asset_price_history')
op.drop_index(op.f('ix_assets_symbol'), table_name='assets')
op.drop_index(op.f('ix_assets_name'), table_name='assets')
op.drop_index(op.f('ix_assets_id'), table_name='assets')
op.drop_table('assets')