"""Task: Add project_id and created_by_user_id to edit_plans

Revision ID: 023
Revises: 022
Create Date: 2026-07-05

新增 project_id 和 created_by_user_id 字段到 edit_plans 表，
用于项目归属鉴权和用户归属追踪，修复审计发现的 P1 越权漏洞。
"""

import sqlalchemy as sa

from alembic import op

revision = "023"
down_revision = "022"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "edit_plans",
        sa.Column("project_id", sa.String(32), nullable=False, server_default=""),
    )
    op.create_index(
        op.f("ix_edit_plans_project_id"),
        "edit_plans",
        ["project_id"],
        unique=False,
    )

    op.add_column(
        "edit_plans",
        sa.Column("created_by_user_id", sa.String(32), nullable=False, server_default=""),
    )
    op.create_index(
        op.f("ix_edit_plans_created_by_user_id"),
        "edit_plans",
        ["created_by_user_id"],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index(
        op.f("ix_edit_plans_created_by_user_id"),
        table_name="edit_plans",
    )
    op.drop_column("edit_plans", "created_by_user_id")

    op.drop_index(
        op.f("ix_edit_plans_project_id"),
        table_name="edit_plans",
    )
    op.drop_column("edit_plans", "project_id")
