"""initial schema: estimates, versions and the reference corpus The three early revisions (engagement, VAT rate) were squashed into this one before the first installation. Once a database exists anywhere, add new revisions instead of editing this file. Revision ID: 0001 Revises: Create Date: 2026-11-02 """ import sqlalchemy as sa from alembic import op from pgvector.sqlalchemy import Vector from sqlalchemy.dialects import postgresql revision = "0001" down_revision = None branch_labels = None depends_on = None EMBEDDING_DIM = 1536 # must match estimator.domain.embeddings.EMBEDDING_DIM JSON = sa.JSON().with_variant(postgresql.JSONB(), "postgresql") EMBEDDING = sa.JSON().with_variant(Vector(EMBEDDING_DIM), "postgresql") TS = sa.DateTime(timezone=True) def upgrade() -> None: postgres = op.get_bind().dialect.name == "postgresql" if postgres: op.execute("CREATE EXTENSION IF NOT EXISTS vector") op.create_table( "estimates", sa.Column("id", sa.String(32), primary_key=True), sa.Column("client", sa.String(100), nullable=False), sa.Column("latest_version", sa.Integer, nullable=False), sa.Column("idempotency_key", sa.String(200), nullable=True), sa.Column("request_hash", sa.String(64), nullable=True), sa.Column("created_by", sa.String(100), nullable=False, server_default=""), sa.Column("created_at", TS, nullable=False), sa.UniqueConstraint("client", "idempotency_key", name="uq_estimates_client_idem"), ) op.create_index("ix_estimates_client_created", "estimates", ["client", "created_at", "id"]) op.create_table( "estimate_versions", sa.Column( "estimate_id", sa.String(32), sa.ForeignKey("estimates.id", ondelete="CASCADE"), primary_key=True, ), sa.Column("version", sa.Integer, primary_key=True), sa.Column("status", sa.String(20), nullable=False), sa.Column("step", sa.String(20), nullable=True), sa.Column("source", sa.String(20), nullable=False), sa.Column("requirements", sa.Text, nullable=False), sa.Column("region", sa.String(8), nullable=False), sa.Column("currency", sa.String(8), nullable=False), sa.Column("answers", JSON, nullable=False), sa.Column("proceed_with_assumptions", sa.Boolean, nullable=False), sa.Column("engagement", sa.String(20), nullable=False, server_default="fixed_price"), sa.Column("vat_rate", sa.Float, nullable=False, server_default="0"), sa.Column("market_version", sa.String(32), nullable=True), sa.Column("result", JSON, nullable=True), sa.Column("price", JSON, nullable=True), sa.Column("market_check", JSON, nullable=True), sa.Column("error_code", sa.String(40), nullable=True), sa.Column("error", sa.Text, nullable=True), sa.Column("created_by", sa.String(100), nullable=False, server_default=""), sa.Column("created_at", TS, nullable=False), sa.Column("started_at", TS, nullable=True), sa.Column("finished_at", TS, nullable=True), ) op.create_index("ix_versions_status", "estimate_versions", ["status"]) op.create_table( "reference_tasks", sa.Column("id", sa.String(32), primary_key=True), sa.Column("dedup_key", sa.String(64), nullable=False, unique=True), sa.Column("project", sa.String(200), nullable=False), sa.Column("title", sa.String(300), nullable=False), sa.Column("description", sa.Text, nullable=False), sa.Column("role", sa.String(20), nullable=True), sa.Column("hours", sa.Float, nullable=False), sa.Column("hours_kind", sa.String(10), nullable=False), sa.Column("source", sa.String(200), nullable=False), sa.Column("content_hash", sa.String(64), nullable=False), sa.Column("embedding", EMBEDDING, nullable=True), sa.Column("embedding_model", sa.String(100), nullable=True), sa.Column("created_by", sa.String(100), nullable=False, server_default=""), sa.Column("created_at", TS, nullable=False), sa.Column("updated_at", TS, nullable=False), ) op.create_index("ix_reference_tasks_project", "reference_tasks", ["project"]) op.create_index("ix_reference_tasks_model", "reference_tasks", ["embedding_model"]) if postgres: op.execute( "CREATE INDEX ix_reference_tasks_embedding_hnsw ON reference_tasks " "USING hnsw (embedding vector_cosine_ops)" ) def downgrade() -> None: op.drop_table("reference_tasks") op.drop_index("ix_versions_status", table_name="estimate_versions") op.drop_table("estimate_versions") op.drop_index("ix_estimates_client_created", table_name="estimates") op.drop_table("estimates")