Initial commit
This commit is contained in:
51
backend/alembic/env.py
Normal file
51
backend/alembic/env.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
# Import all models so Alembic can detect them
|
||||
from app.database import Base
|
||||
import app.models # noqa: F401 — registers all models with Base.metadata
|
||||
|
||||
config = context.config
|
||||
|
||||
# Override sqlalchemy.url from environment if set
|
||||
sync_url = os.environ.get("SYNC_DATABASE_URL")
|
||||
if sync_url:
|
||||
config.set_main_option("sqlalchemy.url", sync_url)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
26
backend/alembic/script.py.mako
Normal file
26
backend/alembic/script.py.mako
Normal file
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
205
backend/alembic/versions/0001_initial_schema.py
Normal file
205
backend/alembic/versions/0001_initial_schema.py
Normal file
@@ -0,0 +1,205 @@
|
||||
"""initial schema
|
||||
|
||||
Revision ID: 0001
|
||||
Revises:
|
||||
Create Date: 2025-01-01 00:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
|
||||
revision: str = "0001"
|
||||
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:
|
||||
# ── members ──────────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"members",
|
||||
sa.Column("bioguide_id", sa.String(), primary_key=True),
|
||||
sa.Column("name", sa.String(), nullable=False),
|
||||
sa.Column("first_name", sa.String()),
|
||||
sa.Column("last_name", sa.String()),
|
||||
sa.Column("party", sa.String(10)),
|
||||
sa.Column("state", sa.String(5)),
|
||||
sa.Column("chamber", sa.String(10)),
|
||||
sa.Column("district", sa.String(10)),
|
||||
sa.Column("photo_url", sa.String()),
|
||||
sa.Column("official_url", sa.String()),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
# ── bills ─────────────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"bills",
|
||||
sa.Column("bill_id", sa.String(), primary_key=True),
|
||||
sa.Column("congress_number", sa.Integer(), nullable=False),
|
||||
sa.Column("bill_type", sa.String(10), nullable=False),
|
||||
sa.Column("bill_number", sa.Integer(), nullable=False),
|
||||
sa.Column("title", sa.Text()),
|
||||
sa.Column("short_title", sa.Text()),
|
||||
sa.Column("sponsor_id", sa.String(), sa.ForeignKey("members.bioguide_id"), nullable=True),
|
||||
sa.Column("introduced_date", sa.Date()),
|
||||
sa.Column("latest_action_date", sa.Date()),
|
||||
sa.Column("latest_action_text", sa.Text()),
|
||||
sa.Column("status", sa.String(100)),
|
||||
sa.Column("chamber", sa.String(10)),
|
||||
sa.Column("congress_url", sa.String()),
|
||||
sa.Column("govtrack_url", sa.String()),
|
||||
sa.Column("last_checked_at", sa.DateTime(timezone=True)),
|
||||
sa.Column("actions_fetched_at", sa.DateTime(timezone=True)),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
op.create_index("ix_bills_congress_number", "bills", ["congress_number"])
|
||||
op.create_index("ix_bills_latest_action_date", "bills", ["latest_action_date"])
|
||||
op.create_index("ix_bills_introduced_date", "bills", ["introduced_date"])
|
||||
op.create_index("ix_bills_chamber", "bills", ["chamber"])
|
||||
op.create_index("ix_bills_sponsor_id", "bills", ["sponsor_id"])
|
||||
|
||||
# Full-text search vector (tsvector generated column) — manual, not in autogenerate
|
||||
op.execute("""
|
||||
ALTER TABLE bills ADD COLUMN search_vector tsvector
|
||||
GENERATED ALWAYS AS (
|
||||
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
|
||||
setweight(to_tsvector('english', coalesce(short_title, '')), 'A') ||
|
||||
setweight(to_tsvector('english', coalesce(latest_action_text, '')), 'C')
|
||||
) STORED
|
||||
""")
|
||||
op.execute("CREATE INDEX ix_bills_search_vector ON bills USING GIN(search_vector)")
|
||||
|
||||
# ── bill_actions ──────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"bill_actions",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("bill_id", sa.String(), sa.ForeignKey("bills.bill_id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("action_date", sa.Date()),
|
||||
sa.Column("action_text", sa.Text()),
|
||||
sa.Column("action_type", sa.String(100)),
|
||||
sa.Column("chamber", sa.String(10)),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
op.create_index("ix_bill_actions_bill_id", "bill_actions", ["bill_id"])
|
||||
op.create_index("ix_bill_actions_action_date", "bill_actions", ["action_date"])
|
||||
|
||||
# ── bill_documents ────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"bill_documents",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("bill_id", sa.String(), sa.ForeignKey("bills.bill_id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("doc_type", sa.String(50)),
|
||||
sa.Column("doc_version", sa.String(50)),
|
||||
sa.Column("govinfo_url", sa.String()),
|
||||
sa.Column("raw_text", sa.Text()),
|
||||
sa.Column("fetched_at", sa.DateTime(timezone=True)),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
op.create_index("ix_bill_documents_bill_id", "bill_documents", ["bill_id"])
|
||||
|
||||
# ── bill_briefs ───────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"bill_briefs",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("bill_id", sa.String(), sa.ForeignKey("bills.bill_id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("document_id", sa.Integer(), sa.ForeignKey("bill_documents.id", ondelete="SET NULL"), nullable=True),
|
||||
sa.Column("summary", sa.Text()),
|
||||
sa.Column("key_points", JSONB()),
|
||||
sa.Column("risks", JSONB()),
|
||||
sa.Column("deadlines", JSONB()),
|
||||
sa.Column("topic_tags", JSONB()),
|
||||
sa.Column("llm_provider", sa.String(50)),
|
||||
sa.Column("llm_model", sa.String(100)),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
op.create_index("ix_bill_briefs_bill_id", "bill_briefs", ["bill_id"])
|
||||
op.execute("CREATE INDEX ix_bill_briefs_topic_tags ON bill_briefs USING GIN(topic_tags)")
|
||||
|
||||
# ── committees ────────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"committees",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("committee_code", sa.String(20), unique=True, nullable=False),
|
||||
sa.Column("name", sa.String(500)),
|
||||
sa.Column("chamber", sa.String(10)),
|
||||
sa.Column("committee_type", sa.String(50)),
|
||||
)
|
||||
|
||||
# ── committee_bills ───────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"committee_bills",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("committee_id", sa.Integer(), sa.ForeignKey("committees.id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("bill_id", sa.String(), sa.ForeignKey("bills.bill_id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("referral_date", sa.Date()),
|
||||
)
|
||||
op.create_index("ix_committee_bills_bill_id", "committee_bills", ["bill_id"])
|
||||
op.create_index("ix_committee_bills_committee_id", "committee_bills", ["committee_id"])
|
||||
|
||||
# ── news_articles ─────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"news_articles",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("bill_id", sa.String(), sa.ForeignKey("bills.bill_id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("source", sa.String(200)),
|
||||
sa.Column("headline", sa.Text()),
|
||||
sa.Column("url", sa.String(), unique=True),
|
||||
sa.Column("published_at", sa.DateTime(timezone=True)),
|
||||
sa.Column("relevance_score", sa.Float(), default=0.0),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
op.create_index("ix_news_articles_bill_id", "news_articles", ["bill_id"])
|
||||
op.create_index("ix_news_articles_published_at", "news_articles", ["published_at"])
|
||||
|
||||
# ── trend_scores ──────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"trend_scores",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("bill_id", sa.String(), sa.ForeignKey("bills.bill_id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("score_date", sa.Date(), nullable=False),
|
||||
sa.Column("newsapi_count", sa.Integer(), default=0),
|
||||
sa.Column("gnews_count", sa.Integer(), default=0),
|
||||
sa.Column("gtrends_score", sa.Float(), default=0.0),
|
||||
sa.Column("composite_score", sa.Float(), default=0.0),
|
||||
sa.UniqueConstraint("bill_id", "score_date", name="uq_trend_scores_bill_date"),
|
||||
)
|
||||
op.create_index("ix_trend_scores_bill_id", "trend_scores", ["bill_id"])
|
||||
op.create_index("ix_trend_scores_score_date", "trend_scores", ["score_date"])
|
||||
op.create_index("ix_trend_scores_composite", "trend_scores", ["composite_score"])
|
||||
|
||||
# ── follows ───────────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"follows",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("follow_type", sa.String(20), nullable=False),
|
||||
sa.Column("follow_value", sa.String(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
sa.UniqueConstraint("follow_type", "follow_value", name="uq_follows_type_value"),
|
||||
)
|
||||
|
||||
# ── app_settings ──────────────────────────────────────────────────────────
|
||||
op.create_table(
|
||||
"app_settings",
|
||||
sa.Column("key", sa.String(), primary_key=True),
|
||||
sa.Column("value", sa.String()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("app_settings")
|
||||
op.drop_table("follows")
|
||||
op.drop_table("trend_scores")
|
||||
op.drop_table("news_articles")
|
||||
op.drop_table("committee_bills")
|
||||
op.drop_table("committees")
|
||||
op.drop_table("bill_briefs")
|
||||
op.drop_table("bill_documents")
|
||||
op.drop_table("bill_actions")
|
||||
op.drop_table("bills")
|
||||
op.drop_table("members")
|
||||
30
backend/alembic/versions/0002_widen_chamber_party_columns.py
Normal file
30
backend/alembic/versions/0002_widen_chamber_party_columns.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""widen chamber and party columns
|
||||
|
||||
Revision ID: 0002
|
||||
Revises: 0001
|
||||
Create Date: 2026-02-28 00:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0002"
|
||||
down_revision: Union[str, None] = "0001"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.alter_column("members", "chamber", type_=sa.String(50))
|
||||
op.alter_column("members", "party", type_=sa.String(50))
|
||||
op.alter_column("bills", "chamber", type_=sa.String(50))
|
||||
op.alter_column("bill_actions", "chamber", type_=sa.String(50))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.alter_column("bill_actions", "chamber", type_=sa.String(10))
|
||||
op.alter_column("bills", "chamber", type_=sa.String(10))
|
||||
op.alter_column("members", "party", type_=sa.String(10))
|
||||
op.alter_column("members", "chamber", type_=sa.String(10))
|
||||
26
backend/alembic/versions/0003_widen_member_state_district.py
Normal file
26
backend/alembic/versions/0003_widen_member_state_district.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""widen member state and district columns
|
||||
|
||||
Revision ID: 0003
|
||||
Revises: 0002
|
||||
Create Date: 2026-03-01 00:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0003"
|
||||
down_revision: Union[str, None] = "0002"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.alter_column("members", "state", type_=sa.String(50))
|
||||
op.alter_column("members", "district", type_=sa.String(50))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.alter_column("members", "district", type_=sa.String(10))
|
||||
op.alter_column("members", "state", type_=sa.String(5))
|
||||
27
backend/alembic/versions/0004_add_brief_type.py
Normal file
27
backend/alembic/versions/0004_add_brief_type.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""add brief_type to bill_briefs
|
||||
|
||||
Revision ID: 0004
|
||||
Revises: 0003
|
||||
Create Date: 2026-03-01 00:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0004"
|
||||
down_revision: Union[str, None] = "0003"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
"bill_briefs",
|
||||
sa.Column("brief_type", sa.String(20), nullable=False, server_default="full"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("bill_briefs", "brief_type")
|
||||
Reference in New Issue
Block a user