"""Add crawl_site.managed_by — distinguishes YAML-sourced vs UI-created rows.

Revision ID: 0003_add_managed_by
Revises: 0002_schema_v2_rebuild
Create Date: 2026-04-23

Background: the admin dashboard gained full CRUD on crawl_site / crawl_target /
site_department (§站点管理 表单). Operators may now create sites directly in
the DB instead of via YAML. `config.sync.sync_dir` used to auto-disable any
DB row not present in the YAML directory — that rule would nuke UI-created
rows.

Fix: tag the origin on `crawl_site.managed_by`:
  'yaml' → row came from sync_dir, subject to YAML-is-truth rule
  'ui'   → row created via POST /admin/api/sites, sync_dir must skip it

Column is NOT NULL with server_default 'yaml' so existing rows keep old
semantics on upgrade. CHECK constraint pins it to the 2-value enum.
"""
from alembic import op
import sqlalchemy as sa

revision = "0003_add_managed_by"
down_revision = "0002_schema_v2_rebuild"
branch_labels = None
depends_on = None


def upgrade() -> None:
    with op.batch_alter_table("crawl_site") as batch_op:
        batch_op.add_column(
            sa.Column(
                "managed_by",
                sa.String(10),
                nullable=False,
                server_default="yaml",
            )
        )
        batch_op.create_check_constraint(
            "ck_crawl_site_managed_by",
            "managed_by IN ('yaml','ui')",
        )


def downgrade() -> None:
    with op.batch_alter_table("crawl_site") as batch_op:
        batch_op.drop_constraint("ck_crawl_site_managed_by", type_="check")
        batch_op.drop_column("managed_by")
