96 lines
4.2 KiB
Python
96 lines
4.2 KiB
Python
"""add generic commodity-scoped meter cost periods
|
|||
|
|
|
||
|
|
Revision ID: 20260822_19_meter_cost_periods
|
||
|
|
Revises: 20260822_18_contract_scopes
|
||
|
|
Create Date: 2026-08-22 00:00:00.000000
|
||
|
|
|
||
|
|
This additive migration creates a separate audit ledger for non-electricity
|
||
|
|
meter costs. It deliberately does not alter, migrate, or delete rows from the
|
||
|
|
existing electricity-only energy_cost_period table.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from typing import Sequence, Union
|
||
|
|
|
||
|
|
import sqlalchemy as sa
|
||
|
|
from alembic import op
|
||
|
|
|
||
|
|
|
||
|
|
revision: str = "20260822_19_meter_cost_periods"
|
||
|
|
down_revision: Union[str, None] = "20260822_18_contract_scopes"
|
||
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
||
|
|
depends_on: Union[str, Sequence[str], None] = None
|
||
|
|
|
||
|
|
|
||
|
|
class ExactDecimal(sa.TypeDecorator):
|
||
|
|
"""Use SQLite text storage while retaining Numeric semantics elsewhere."""
|
||
|
|
|
||
|
|
impl = sa.Numeric
|
||
|
|
cache_ok = True
|
||
|
|
|
||
|
|
def __init__(self, precision: int, scale: int) -> None:
|
||
|
|
self.precision = precision
|
||
|
|
self.scale = scale
|
||
|
|
super().__init__(precision=precision, scale=scale)
|
||
|
|
|
||
|
|
def load_dialect_impl(self, dialect):
|
||
|
|
if dialect.name == "sqlite":
|
||
|
|
return dialect.type_descriptor(sa.String(self.precision + 2))
|
||
|
|
return dialect.type_descriptor(sa.Numeric(self.precision, self.scale, asdecimal=True))
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
op.create_table(
|
||
|
|
"meter_cost_period",
|
||
|
|
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||
|
|
sa.Column("commodity", sa.String(length=32), nullable=False),
|
||
|
|
sa.Column("period_start", sa.DateTime(timezone=True), nullable=False),
|
||
|
|
sa.Column("period_end", sa.DateTime(timezone=True), nullable=False),
|
||
|
|
sa.Column("meter_id", sa.Integer(), nullable=True),
|
||
|
|
sa.Column("source_binding_id", sa.Integer(), nullable=True),
|
||
|
|
sa.Column("contract_version_id", sa.Integer(), nullable=True),
|
||
|
|
# SQLite NUMERIC coercion binds Decimal values as binary floats. Store
|
||
|
|
# fixed-width decimal text there, while retaining Numeric semantics on
|
||
|
|
# other supported dialects.
|
||
|
|
sa.Column("quantity", ExactDecimal(15, 6), nullable=False),
|
||
|
|
sa.Column("cost", ExactDecimal(15, 9), nullable=False),
|
||
|
|
sa.Column("currency", sa.String(length=8), nullable=False),
|
||
|
|
sa.Column("cost_breakdown", sa.JSON(), nullable=False),
|
||
|
|
sa.Column("pricing_snapshot", sa.JSON(), nullable=False),
|
||
|
|
sa.Column("quality", sa.String(length=32), nullable=False),
|
||
|
|
sa.Column("degraded", sa.Boolean(), nullable=False, server_default=sa.false()),
|
||
|
|
sa.Column("degraded_reason", sa.String(length=255), nullable=True),
|
||
|
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||
|
|
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||
|
|
sa.ForeignKeyConstraint(["meter_id"], ["meter.id"], ondelete="RESTRICT"),
|
||
|
|
sa.ForeignKeyConstraint(
|
||
|
|
["source_binding_id"], ["meter_source_binding.id"], ondelete="RESTRICT"
|
||
|
|
),
|
||
|
|
sa.ForeignKeyConstraint(
|
||
|
|
["contract_version_id"], ["energy_contract_version.id"], ondelete="RESTRICT"
|
||
|
|
),
|
||
|
|
sa.CheckConstraint(
|
||
|
|
"degraded OR (meter_id IS NOT NULL AND source_binding_id IS NOT NULL "
|
||
|
|
"AND contract_version_id IS NOT NULL)",
|
||
|
|
name="ck_meter_cost_period_normal_audit_links",
|
||
|
|
),
|
||
|
|
sa.CheckConstraint("period_end > period_start", name="ck_meter_cost_period_positive_interval"),
|
||
|
|
sa.CheckConstraint(
|
||
|
|
"NOT degraded OR (degraded_reason IS NOT NULL AND length(trim(degraded_reason)) > 0)",
|
||
|
|
name="ck_meter_cost_period_degraded_reason",
|
||
|
|
),
|
||
|
|
sa.PrimaryKeyConstraint("id"),
|
||
|
|
sa.UniqueConstraint("commodity", "period_start", name="uq_meter_cost_period_commodity_start"),
|
||
|
|
)
|
||
|
|
op.create_index(
|
||
|
|
"ix_meter_cost_period_commodity_start", "meter_cost_period", ["commodity", "period_start"]
|
||
|
|
)
|
||
|
|
op.create_index(
|
||
|
|
"ix_meter_cost_period_source_binding_id", "meter_cost_period", ["source_binding_id"]
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
op.drop_index("ix_meter_cost_period_source_binding_id", table_name="meter_cost_period")
|
||
|
|
op.drop_index("ix_meter_cost_period_commodity_start", table_name="meter_cost_period")
|
||
|
|
op.drop_table("meter_cost_period")
|