"""user_disk_usage 表(§7.5 #4 软配额).

Revision ID: 0008
Revises: 0007
Create Date: 2026-05-27

per-user 工作目录字节使用快照,lifespan 后台 task 周期(默 15min)扫描 user_root 落库;
write 前 gate(DockerExecutor / /v1/files/upload)查这表对比 yaml `quotas.disk_bytes_per_user`,
超额返 [Error] 硬阻。

扫描间隙写入轻微突破上限接受(race-tolerant,跟 image/video 配额一致);外部用户开放前
OS 层 xfs prjquota 兜底真上限(§7.5 #4)。
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op


revision: str = "0008"
down_revision: Union[str, None] = "0007"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "user_disk_usage",
        sa.Column("user_id", sa.UUID(as_uuid=True), nullable=False),
        sa.Column("bytes_used", sa.BigInteger(), nullable=False,
                  server_default=sa.text("0")),
        sa.Column("file_count", sa.Integer(), nullable=False,
                  server_default=sa.text("0")),
        sa.Column("scanned_at", sa.DateTime(timezone=True), nullable=False,
                  server_default=sa.func.now()),
        sa.PrimaryKeyConstraint("user_id"),
        sa.ForeignKeyConstraint(
            ["user_id"], ["users.user_id"], ondelete="CASCADE",
        ),
    )


def downgrade() -> None:
    op.drop_table("user_disk_usage")
