"""PostgreSQL migration: drop unique index on url, add version column.

Drops the ``UNIQUE INDEX`` on ``url`` so multiple parsed versions of
the same URL can coexist.  Adds a ``version`` column (INTEGER, default 1)
for monotonically increasing content version tracking.

Together with the complementary Doris migration (0017), this converts
``web_parsed_pages`` from upsert-by-URL to append-only versioned insert
— matching the ``web_html_raw`` pattern.

Revision ID: 0027
"""
from __future__ import annotations

from repo_core.migrations.builder import MigrationBuilder
from repo_core.migrations.runner import Migration

VERSION = "0027"


def get_postgres_migrations() -> list[Migration]:
    return [
        Migration(
            version="parsed_page_versioning_0001",
            description=(
                "Drop unique index on url, add version column for "
                "append-only versioned inserts"
            ),
            dialect="postgresql",
            up=(
                MigrationBuilder("web_parsed_pages")
                .drop_index("uq_web_parsed_pages_url")
                .add_column("version", "INTEGER", nullable=True, default="1")
                .alter_table()
            ),
            down=(
                MigrationBuilder("web_parsed_pages")
                .drop_column("version")
                .index(["url"], unique=True, name="uq_web_parsed_pages_url")
                .alter_table()
            ),
        ),
    ]
