programme-pulse-chat/alembic/versions/0001_initial.py
DJP b70d148b94 Productionise Programme Pulse
Backend
- Routes moved under /api/, JWT bearer auth via @before_request
- DEV_AUTH_BYPASS escape hatch for local dev
- In-memory chat history and report state replaced with Postgres tables
  (preferences, chat_messages, reports, feedback_events) keyed on user
- SQLAlchemy 2.x + Alembic migrations run on container start
- Graceful Airtable failure handling — bad creds no longer 500 the API
- Per-user data isolation via g.user_email from validated token

Frontend
- React + Vite + TypeScript SPA at /programme-pulse/
- MSAL.js (PKCE, sessionStorage, ID token to backend)
- VITE_DEV_AUTH_BYPASS mirrors backend bypass for local dev
- Streaming chat via fetch ReadableStream + SSE parsing
- Charts via chart.js, markdown via react-markdown + remark-gfm
- Full UI parity with the original templates/index.html

Deploy (optical-dev split-build pattern)
- Dockerfile + docker-compose.yml (name: programme-pulse pinned;
  app + Postgres; 127.0.0.1 binding only)
- deploy/apache-programme-pulse.conf.tmpl with flushpackets=on for SSE
- deploy/deploy.sh mirrors OSOP — port auto-pick (5051..5099),
  apache conf render, frontend build in throwaway node container,
  rsync to /var/www/html/programme-pulse, /api/health poll

Tests
- 49 passing; new tests for DB-backed preferences and JWT auth helpers
- SQLite-backed test fixture in tests/conftest.py

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 11:08:28 -04:00

70 lines
2.5 KiB
Python

"""initial schema
Revision ID: 0001
Revises:
Create Date: 2026-05-07
"""
from __future__ import annotations
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
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:
op.create_table(
"preferences",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("user_email", sa.String(320), nullable=False),
sa.Column("text", sa.Text, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_preferences_user_email", "preferences", ["user_email"])
op.create_table(
"chat_messages",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("user_email", sa.String(320), nullable=False),
sa.Column("role", sa.String(16), nullable=False),
sa.Column("content", sa.Text, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_chat_user_created", "chat_messages", ["user_email", "created_at"])
op.create_table(
"reports",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("user_email", sa.String(320), nullable=False),
sa.Column("summary_md", sa.Text, nullable=False),
sa.Column("summary_doc_path", sa.String(512), nullable=False),
sa.Column("full_doc_path", sa.String(512), nullable=False),
sa.Column("generated_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_reports_user_email", "reports", ["user_email"])
op.create_table(
"feedback_events",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("user_email", sa.String(320), nullable=False),
sa.Column("rating", sa.String(8), nullable=False),
sa.Column("message_text", sa.Text, nullable=False),
sa.Column("extracted_insight", sa.Text, nullable=False),
sa.Column("preference_id", sa.Integer, sa.ForeignKey("preferences.id"), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_feedback_user_email", "feedback_events", ["user_email"])
def downgrade() -> None:
op.drop_table("feedback_events")
op.drop_table("reports")
op.drop_table("chat_messages")
op.drop_table("preferences")