You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.2 KiB
Python

"""add 8-K parsing: parsed_status/items_json + sec_filing_events
Revision ID: g8a9b0c1d2e3
Revises: f7a8b9c0d1e2
Create Date: 2026-04-08
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision: str = "g8a9b0c1d2e3"
down_revision: Union[str, Sequence[str], None] = "f7a8b9c0d1e2"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
conn = op.get_bind()
# Add parsed_status + items_json to sec_filings
col_names = [row[0] for row in conn.execute(
sa.text("SELECT column_name FROM information_schema.columns WHERE table_name='sec_filings'")
)]
if "parsed_status" not in col_names:
op.add_column(
"sec_filings",
sa.Column("parsed_status", sa.String(20), server_default="pending", nullable=True),
)
if "items_json" not in col_names:
op.add_column(
"sec_filings",
sa.Column("items_json", postgresql.JSON(), nullable=True),
)
# Mark all existing 8-K filings as 'pending' so the parser can backfill them
conn.execute(
sa.text(
"UPDATE sec_filings SET parsed_status = 'pending' "
"WHERE form_type IN ('8-K', '8-K/A') AND parsed_status IS NULL"
)
)
# Create sec_filing_events table
if not conn.dialect.has_table(conn, "sec_filing_events"):
op.create_table(
"sec_filing_events",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("ticker", sa.String(10), nullable=False),
sa.Column("accession_number", sa.String(30), nullable=False),
sa.Column("form_type", sa.String(20), nullable=False),
sa.Column("filing_date", postgresql.TIMESTAMP(timezone=True), nullable=False),
sa.Column("item_number", sa.String(10), nullable=False),
sa.Column("event_type", sa.String(50), nullable=False),
sa.Column("title", sa.String(512), nullable=True),
sa.Column("summary", sa.Text(), nullable=True),
sa.Column("content_source", sa.String(20), nullable=True),
sa.Column("created_at", postgresql.TIMESTAMP(timezone=True), nullable=True),
sa.Column("updated_at", postgresql.TIMESTAMP(timezone=True), nullable=True),
sa.UniqueConstraint("accession_number", "item_number", name="uq_filing_event"),
)
op.create_index("ix_filing_events_ticker", "sec_filing_events", ["ticker"])
op.create_index("ix_filing_events_accession", "sec_filing_events", ["accession_number"])
op.create_index(
"idx_filing_events_ticker_date",
"sec_filing_events",
["ticker", "filing_date"],
)
op.create_index(
"idx_filing_events_ticker_type",
"sec_filing_events",
["ticker", "event_type"],
)
op.create_index("idx_filing_events_date", "sec_filing_events", ["filing_date"])
def downgrade() -> None:
op.drop_table("sec_filing_events")
op.drop_column("sec_filings", "items_json")
op.drop_column("sec_filings", "parsed_status")