Initial extraction from monorepo
This commit is contained in:
commit
f7ef7ca4ab
56 changed files with 6163 additions and 0 deletions
61
alembic/env.py
Normal file
61
alembic/env.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import asyncio
|
||||
import os
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
from alembic import context
|
||||
from fire_planner.db import SCHEMA_NAME, Base
|
||||
|
||||
config = context.config
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
db_url = os.environ.get("DB_CONNECTION_STRING")
|
||||
if db_url:
|
||||
config.set_main_option("sqlalchemy.url", db_url)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
# Alembic's version_table lives inside SCHEMA_NAME, so the schema must
|
||||
# exist before context.configure() tries to create alembic_version.
|
||||
connection.exec_driver_sql(f'CREATE SCHEMA IF NOT EXISTS "{SCHEMA_NAME}"')
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
version_table_schema=SCHEMA_NAME,
|
||||
include_schemas=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_migrations_online() -> None:
|
||||
configuration = config.get_section(config.config_ini_section, {})
|
||||
connectable = async_engine_from_config(configuration, prefix="sqlalchemy.")
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
await connection.commit()
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=config.get_main_option("sqlalchemy.url"),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
version_table_schema=SCHEMA_NAME,
|
||||
include_schemas=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
asyncio.run(run_migrations_online())
|
||||
173
alembic/versions/0001_initial.py
Normal file
173
alembic/versions/0001_initial.py
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
"""initial schema — 6 tables in fire_planner schema
|
||||
|
||||
Revision ID: 0001
|
||||
Revises:
|
||||
Create Date: 2026-04-25 00:00:00.000000
|
||||
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0001"
|
||||
down_revision: str | None = None
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
SCHEMA = "fire_planner"
|
||||
|
||||
|
||||
def _jsonb() -> sa.types.TypeEngine[object]:
|
||||
"""Postgres JSONB; falls back to plain JSON on SQLite (tests)."""
|
||||
return postgresql.JSONB().with_variant(sa.JSON(), "sqlite")
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
|
||||
|
||||
op.create_table(
|
||||
"account_snapshot",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("external_id", sa.Text(), nullable=False, unique=True),
|
||||
sa.Column("snapshot_date", sa.Date(), nullable=False),
|
||||
sa.Column("account_id", sa.Text(), nullable=False),
|
||||
sa.Column("account_name", sa.Text(), nullable=False),
|
||||
sa.Column("account_type", sa.Text(), nullable=False),
|
||||
sa.Column("currency", sa.CHAR(3), nullable=False, server_default="GBP"),
|
||||
sa.Column("market_value", sa.Numeric(14, 2), nullable=False),
|
||||
sa.Column("market_value_gbp", sa.Numeric(14, 2), nullable=False),
|
||||
sa.Column("cost_basis_gbp", sa.Numeric(14, 2), nullable=True),
|
||||
sa.Column("raw_extraction", _jsonb(), nullable=True),
|
||||
sa.Column("created_at",
|
||||
sa.TIMESTAMP(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()")),
|
||||
schema=SCHEMA,
|
||||
)
|
||||
op.create_index("idx_account_snapshot_date",
|
||||
"account_snapshot", ["snapshot_date"],
|
||||
schema=SCHEMA)
|
||||
op.create_index("idx_account_snapshot_account",
|
||||
"account_snapshot", ["account_id"],
|
||||
schema=SCHEMA)
|
||||
|
||||
op.create_table(
|
||||
"scenario",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("external_id", sa.Text(), nullable=False, unique=True),
|
||||
sa.Column("jurisdiction", sa.Text(), nullable=False),
|
||||
sa.Column("strategy", sa.Text(), nullable=False),
|
||||
sa.Column("leave_uk_year", sa.Integer(), nullable=False),
|
||||
sa.Column("glide_path", sa.Text(), nullable=False),
|
||||
sa.Column("spending_gbp", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("horizon_years", sa.Integer(), nullable=False, server_default=sa.text("60")),
|
||||
sa.Column("nw_seed_gbp", sa.Numeric(14, 2), nullable=False),
|
||||
sa.Column("savings_per_year_gbp",
|
||||
sa.Numeric(12, 2),
|
||||
nullable=False,
|
||||
server_default=sa.text("0")),
|
||||
sa.Column("config_json", _jsonb(), nullable=False),
|
||||
sa.Column("created_at",
|
||||
sa.TIMESTAMP(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()")),
|
||||
schema=SCHEMA,
|
||||
)
|
||||
op.create_index("idx_scenario_jurisdiction", "scenario", ["jurisdiction"], schema=SCHEMA)
|
||||
op.create_index("idx_scenario_strategy", "scenario", ["strategy"], schema=SCHEMA)
|
||||
|
||||
op.create_table(
|
||||
"mc_run",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("scenario_id", sa.Integer(), nullable=False),
|
||||
sa.Column("run_at",
|
||||
sa.TIMESTAMP(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()")),
|
||||
sa.Column("n_paths", sa.Integer(), nullable=False),
|
||||
sa.Column("seed", sa.Integer(), nullable=False),
|
||||
sa.Column("success_rate", sa.Numeric(6, 4), nullable=False),
|
||||
sa.Column("p10_ending_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p50_ending_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p90_ending_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("median_lifetime_tax_gbp", sa.Numeric(14, 2), nullable=False),
|
||||
sa.Column("median_years_to_ruin", sa.Numeric(6, 2), nullable=True),
|
||||
sa.Column("elapsed_seconds", sa.Numeric(8, 3), nullable=False),
|
||||
sa.Column("sequence_risk_correlation", sa.Numeric(6, 4), nullable=True),
|
||||
sa.Column("extra", _jsonb(), nullable=True),
|
||||
schema=SCHEMA,
|
||||
)
|
||||
op.create_index("idx_mc_run_scenario", "mc_run", ["scenario_id"], schema=SCHEMA)
|
||||
op.create_index("idx_mc_run_at", "mc_run", ["run_at"], schema=SCHEMA)
|
||||
|
||||
op.create_table(
|
||||
"mc_path",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("mc_run_id", sa.Integer(), nullable=False),
|
||||
sa.Column("path_idx", sa.Integer(), nullable=False),
|
||||
sa.Column("bucket", sa.Text(), nullable=False),
|
||||
sa.Column("year_idx", sa.Integer(), nullable=False),
|
||||
sa.Column("portfolio_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("withdrawal_gbp", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("tax_paid_gbp", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("real_portfolio_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
schema=SCHEMA,
|
||||
)
|
||||
op.create_index("idx_mc_path_run", "mc_path", ["mc_run_id"], schema=SCHEMA)
|
||||
|
||||
op.create_table(
|
||||
"projection_yearly",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("mc_run_id", sa.Integer(), nullable=False),
|
||||
sa.Column("year_idx", sa.Integer(), nullable=False),
|
||||
sa.Column("p10_portfolio_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p25_portfolio_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p50_portfolio_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p75_portfolio_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p90_portfolio_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p50_withdrawal_gbp", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("p50_tax_gbp", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("survival_rate", sa.Numeric(6, 4), nullable=False),
|
||||
schema=SCHEMA,
|
||||
)
|
||||
op.create_index("idx_projection_run", "projection_yearly", ["mc_run_id"], schema=SCHEMA)
|
||||
|
||||
op.create_table(
|
||||
"scenario_summary",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("scenario_id", sa.Integer(), nullable=False, unique=True),
|
||||
sa.Column("mc_run_id", sa.Integer(), nullable=False),
|
||||
sa.Column("jurisdiction", sa.Text(), nullable=False),
|
||||
sa.Column("strategy", sa.Text(), nullable=False),
|
||||
sa.Column("leave_uk_year", sa.Integer(), nullable=False),
|
||||
sa.Column("glide_path", sa.Text(), nullable=False),
|
||||
sa.Column("spending_gbp", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("success_rate", sa.Numeric(6, 4), nullable=False),
|
||||
sa.Column("p10_ending_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p50_ending_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("p90_ending_gbp", sa.Numeric(16, 2), nullable=False),
|
||||
sa.Column("median_lifetime_tax_gbp", sa.Numeric(14, 2), nullable=False),
|
||||
sa.Column("median_years_to_ruin", sa.Numeric(6, 2), nullable=True),
|
||||
sa.Column("updated_at",
|
||||
sa.TIMESTAMP(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()")),
|
||||
schema=SCHEMA,
|
||||
)
|
||||
op.create_index("idx_summary_jurisdiction",
|
||||
"scenario_summary", ["jurisdiction"],
|
||||
schema=SCHEMA)
|
||||
op.create_index("idx_summary_strategy", "scenario_summary", ["strategy"], schema=SCHEMA)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("scenario_summary", schema=SCHEMA)
|
||||
op.drop_table("projection_yearly", schema=SCHEMA)
|
||||
op.drop_table("mc_path", schema=SCHEMA)
|
||||
op.drop_table("mc_run", schema=SCHEMA)
|
||||
op.drop_table("scenario", schema=SCHEMA)
|
||||
op.drop_table("account_snapshot", schema=SCHEMA)
|
||||
op.execute(f"DROP SCHEMA IF EXISTS {SCHEMA}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue