Squashed 'core/' content from commit 92ec910
git-subtree-dir: core git-subtree-split: 92ec910a132e379a3a6e442a75bcb07cac0f0010
This commit is contained in:
53
backend/alembic/versions/005_quota_tables.py
Normal file
53
backend/alembic/versions/005_quota_tables.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Add quota tables
|
||||
|
||||
Revision ID: 005_quota
|
||||
Revises: 004_charging
|
||||
Create Date: 2026-04-03
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "005_quota"
|
||||
down_revision = "004_charging"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# --- energy_quotas ---
|
||||
op.create_table(
|
||||
"energy_quotas",
|
||||
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
|
||||
sa.Column("name", sa.String(200), nullable=False),
|
||||
sa.Column("target_type", sa.String(50), nullable=False),
|
||||
sa.Column("target_id", sa.Integer, nullable=False),
|
||||
sa.Column("energy_type", sa.String(50), nullable=False),
|
||||
sa.Column("period", sa.String(20), nullable=False),
|
||||
sa.Column("quota_value", sa.Float, nullable=False),
|
||||
sa.Column("unit", sa.String(20), default="kWh"),
|
||||
sa.Column("warning_threshold_pct", sa.Float, default=80),
|
||||
sa.Column("alert_threshold_pct", sa.Float, default=95),
|
||||
sa.Column("is_active", sa.Boolean, default=True),
|
||||
sa.Column("created_by", sa.Integer, sa.ForeignKey("users.id")),
|
||||
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()),
|
||||
)
|
||||
|
||||
# --- quota_usage ---
|
||||
op.create_table(
|
||||
"quota_usage",
|
||||
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
|
||||
sa.Column("quota_id", sa.Integer, sa.ForeignKey("energy_quotas.id"), nullable=False),
|
||||
sa.Column("period_start", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("period_end", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("actual_value", sa.Float, default=0),
|
||||
sa.Column("quota_value", sa.Float, nullable=False),
|
||||
sa.Column("usage_rate_pct", sa.Float, default=0),
|
||||
sa.Column("status", sa.String(20), default="normal"),
|
||||
sa.Column("calculated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("quota_usage")
|
||||
op.drop_table("energy_quotas")
|
||||
Reference in New Issue
Block a user