90 lines
4.5 KiB
Python
90 lines
4.5 KiB
Python
"""Initial migration: create all tables
|
|
|
|
Revision ID: 001
|
|
Revises:
|
|
Create Date: 2024-01-01 00:00:00.000000
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = '001'
|
|
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:
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
|
|
# Create reports table
|
|
op.create_table('reports',
|
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column('symbol', sa.String(length=20), nullable=False, comment='证券代码'),
|
|
sa.Column('market', sa.String(length=20), nullable=False, comment='交易市场'),
|
|
sa.Column('status', sa.String(length=20), nullable=False, comment='报告状态'),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
|
|
# Create analysis_modules table
|
|
op.create_table('analysis_modules',
|
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column('report_id', postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column('module_type', sa.String(length=50), nullable=False, comment='模块类型'),
|
|
sa.Column('module_order', sa.Integer(), nullable=False, comment='模块顺序'),
|
|
sa.Column('title', sa.String(length=200), nullable=False, comment='模块标题'),
|
|
sa.Column('content', postgresql.JSONB(astext_type=sa.Text()), nullable=True, comment='模块内容'),
|
|
sa.Column('status', sa.String(length=20), nullable=False, comment='模块状态'),
|
|
sa.Column('started_at', sa.DateTime(timezone=True), nullable=True, comment='开始时间'),
|
|
sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True, comment='完成时间'),
|
|
sa.Column('error_message', sa.Text(), nullable=True, comment='错误信息'),
|
|
sa.ForeignKeyConstraint(['report_id'], ['reports.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
|
|
# Create progress_tracking table
|
|
op.create_table('progress_tracking',
|
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column('report_id', postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column('step_name', sa.String(length=100), nullable=False, comment='步骤名称'),
|
|
sa.Column('step_order', sa.Integer(), nullable=False, comment='步骤顺序'),
|
|
sa.Column('status', sa.String(length=20), nullable=False, comment='步骤状态'),
|
|
sa.Column('started_at', sa.DateTime(timezone=True), nullable=True, comment='开始时间'),
|
|
sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True, comment='完成时间'),
|
|
sa.Column('duration_ms', sa.Integer(), nullable=True, comment='耗时(毫秒)'),
|
|
sa.Column('error_message', sa.Text(), nullable=True, comment='错误信息'),
|
|
sa.ForeignKeyConstraint(['report_id'], ['reports.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
|
|
# Create system_config table
|
|
op.create_table('system_config',
|
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column('config_key', sa.String(length=100), nullable=False, comment='配置键'),
|
|
sa.Column('config_value', postgresql.JSONB(astext_type=sa.Text()), nullable=False, comment='配置值'),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('config_key')
|
|
)
|
|
|
|
# Set default values for status columns
|
|
op.execute("ALTER TABLE reports ALTER COLUMN status SET DEFAULT 'generating'")
|
|
op.execute("ALTER TABLE analysis_modules ALTER COLUMN status SET DEFAULT 'pending'")
|
|
op.execute("ALTER TABLE progress_tracking ALTER COLUMN status SET DEFAULT 'pending'")
|
|
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
def downgrade() -> None:
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.drop_table('system_config')
|
|
op.drop_table('progress_tracking')
|
|
op.drop_table('analysis_modules')
|
|
op.drop_table('reports')
|
|
# ### end Alembic commands ### |