Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

19.0 KB
Raw Blame
from __future__ import annotations

import gzip
import threading
from collections.abc import Iterator
from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path
from typing import Any

import pytest

from click.testing import CliRunner
from hogli_commands import db_schema

runner = CliRunner()


def _artifact(
    artifact_id: int,
    head_sha: str,
    created_at: str,
    *,
    expired: bool = False,
    size_in_bytes: int = db_schema.MIN_SCHEMA_ARTIFACT_BYTES + 1,
    name: str = db_schema.SCHEMA_ARTIFACT_NAME,
    head_branch: str = db_schema.DEFAULT_BASE_BRANCH,
) -> db_schema.SchemaArtifact:
    return db_schema.SchemaArtifact(
        id=artifact_id,
        name=name,
        expired=expired,
        size_in_bytes=size_in_bytes,
        archive_download_url=f"https://api.github.com/artifacts/{artifact_id}/zip",
        head_sha=head_sha,
        head_branch=head_branch,
        created_at=created_at,
    )


def _write_schema(path: Path) -> None:
    with gzip.open(path, "wb") as schema:
        schema.write(b"SELECT 1;\n")


@pytest.mark.parametrize(
    "name,content",
    [
        ("not-gzip", b"this is not gzip data"),
        ("empty", b""),
        ("partial-header", b"\x1f\x8b"),
    ],
)
def test_validate_gzip_rejects_corrupt_inputs(tmp_path: Path, name: str, content: bytes) -> None:
    path = tmp_path / f"{name}.sql.gz"
    path.write_bytes(content)
    with pytest.raises(db_schema.SchemaRestoreError):
        db_schema._validate_gzip(path)


def test_validate_gzip_rejects_truncated_stream(tmp_path: Path) -> None:
    full = tmp_path / "good.sql.gz"
    _write_schema(full)
    truncated = tmp_path / "truncated.sql.gz"
    raw = full.read_bytes()
    truncated.write_bytes(raw[: len(raw) // 2])
    with pytest.raises(db_schema.SchemaRestoreError):
        db_schema._validate_gzip(truncated)


def test_select_newest_compatible_artifact_returns_most_recent() -> None:
    artifacts = [
        _artifact(1, "older", "2026-01-01T00:00:00Z"),
        _artifact(2, "newer", "2026-01-02T00:00:00Z"),
    ]

    assert db_schema.select_newest_compatible_artifact(artifacts) == artifacts[1]


@pytest.mark.parametrize(
    "artifact",
    [
        _artifact(1, "sha", "2026-01-01T00:00:00Z", expired=True),
        _artifact(1, "sha", "2026-01-01T00:00:00Z", size_in_bytes=100),
        _artifact(1, "", "2026-01-01T00:00:00Z"),
        _artifact(1, "sha", "2026-01-01T00:00:00Z", name="other"),
        _artifact(1, "sha", "2026-01-01T00:00:00Z", head_branch="some-pr-branch"),
    ],
)
def test_select_ignores_invalid_artifacts(artifact: db_schema.SchemaArtifact) -> None:
    assert db_schema.select_newest_compatible_artifact([artifact]) is None


class _FakeArtifactPage:
    def __init__(self, artifacts: list[dict[str, Any]], *, has_next: bool) -> None:
        self._artifacts = artifacts
        self.links = {"next": {"url": "next"}} if has_next else {}

    def raise_for_status(self) -> None:
        pass

    def json(self) -> dict[str, Any]:
        return {"artifacts": self._artifacts}


class _FakeArtifactPagesSession:
    def __init__(self, pages: list[list[dict[str, Any]]]) -> None:
        self._pages = pages
        self.get_count = 0

    def get(self, url: str, *, params: dict[str, Any], headers: dict[str, str], timeout: int) -> _FakeArtifactPage:
        self.get_count += 1
        page = params["page"]
        return _FakeArtifactPage(
            self._pages[page - 1] if page <= len(self._pages) else [],
            has_next=page < len(self._pages),
        )


def _raw_artifact(artifact_id: int, head_branch: str, *, head_repository_id: int | None = 1) -> dict[str, Any]:
    return {
        "id": artifact_id,
        "name": db_schema.SCHEMA_ARTIFACT_NAME,
        "expired": False,
        "size_in_bytes": db_schema.MIN_SCHEMA_ARTIFACT_BYTES + 1,
        "archive_download_url": f"https://api.github.com/artifacts/{artifact_id}/zip",
        "created_at": "2026-01-01T00:00:00Z",
        "workflow_run": {
            "head_sha": "sha",
            "head_branch": head_branch,
            "repository_id": 1,
            "head_repository_id": head_repository_id,
        },
    }


@pytest.mark.parametrize("head_repository_id", [2, None])
def test_find_skips_artifacts_from_another_repository(head_repository_id: int | None) -> None:
    session = _FakeArtifactPagesSession(
        [[_raw_artifact(2, "master", head_repository_id=head_repository_id), _raw_artifact(1, "master")]]
    )

    artifact = db_schema.find_newest_compatible_artifact(token="t", session=session, base_branch="master")  # type: ignore[arg-type]

    assert artifact is not None and artifact.id == 1


def test_find_stops_at_first_page_with_compatible_candidate() -> None:
    session = _FakeArtifactPagesSession([[_raw_artifact(1, "master")], [_raw_artifact(2, "master")]])

    artifact = db_schema.find_newest_compatible_artifact(token="t", session=session, base_branch="master")  # type: ignore[arg-type]

    assert session.get_count == 1
    assert artifact is not None and artifact.id == 1


def test_find_caps_pages_when_no_candidate_matches() -> None:
    pages = [[_raw_artifact(page, "some-pr-branch")] for page in range(1, 31)]
    session = _FakeArtifactPagesSession(pages)

    assert db_schema.find_newest_compatible_artifact(token="t", session=session, base_branch="master") is None  # type: ignore[arg-type]
    assert session.get_count == db_schema.MAX_ARTIFACT_PAGES


def test_select_honors_custom_base_branch() -> None:
    master_artifact = _artifact(1, "sha-a", "2026-01-01T00:00:00Z", head_branch="master")
    release_artifact = _artifact(2, "sha-b", "2026-01-02T00:00:00Z", head_branch="release-26.1")

    selected = db_schema.select_newest_compatible_artifact(
        [master_artifact, release_artifact],
        base_branch="release-26.1",
    )

    assert selected == release_artifact


def test_find_emits_diagnostics_on_no_compatible_artifact(capsys: pytest.CaptureFixture[str]) -> None:
    session = _FakeArtifactPagesSession([[_raw_artifact(10, "some-pr"), _raw_artifact(11, "some-other-pr")]])

    assert db_schema.find_newest_compatible_artifact(token="t", session=session, base_branch="master") is None  # type: ignore[arg-type]

    stderr = capsys.readouterr().err
    assert "Fetched 2 migrated-schema artifact(s)" in stderr
    assert "After name/expiry/size/branch filters: 0 candidate(s)" in stderr


class _ScriptedStatusServer(HTTPServer):
    statuses: list[int]
    request_count: int


class _ScriptedStatusHandler(BaseHTTPRequestHandler):
    server: _ScriptedStatusServer

    def do_GET(self) -> None:
        index = min(self.server.request_count, len(self.server.statuses) - 1)
        self.server.request_count += 1
        self.send_response(self.server.statuses[index])
        self.send_header("Content-Length", "2")
        self.end_headers()
        self.wfile.write(b"{}")

    def log_message(self, format: str, *args: Any) -> None:
        pass


@pytest.fixture
def scripted_status_server() -> Iterator[_ScriptedStatusServer]:
    server = _ScriptedStatusServer(("127.0.0.1", 0), _ScriptedStatusHandler)
    server.statuses = [200]
    server.request_count = 0
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    yield server
    server.shutdown()
    server.server_close()


@pytest.mark.parametrize(
    "statuses,expected_status,expected_requests",
    [
        ([500, 502, 200], 200, 3),
        ([503], 503, db_schema.GITHUB_RETRY_TOTAL + 1),
        ([404, 200], 404, 1),
    ],
    ids=["transient_5xx_recovers", "persistent_5xx_returns_last_response", "4xx_not_retried"],
)
def test_github_session_retries_only_server_errors(
    scripted_status_server: _ScriptedStatusServer,
    monkeypatch: pytest.MonkeyPatch,
    statuses: list[int],
    expected_status: int,
    expected_requests: int,
) -> None:
    monkeypatch.setattr(db_schema, "GITHUB_RETRY_BACKOFF_FACTOR", 0)
    scripted_status_server.statuses = statuses
    session = db_schema._github_session()
    session.mount("http://", session.get_adapter("https://"))

    response = session.get(f"http://127.0.0.1:{scripted_status_server.server_port}/", timeout=5)

    assert response.status_code == expected_status
    assert scripted_status_server.request_count == expected_requests


def test_download_fails_when_no_compatible_artifact(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(db_schema, "github_token", lambda: "token")
    monkeypatch.setattr(db_schema, "find_newest_compatible_artifact", lambda **kwargs: None)

    result = runner.invoke(db_schema.db_download_schema, [])

    assert result.exit_code != 0


def test_effective_base_branch_prefers_env(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setenv("POSTHOG_SCHEMA_RESTORE_BASE_BRANCH", "release-26.1")
    assert db_schema._effective_base_branch(None) == "release-26.1"
    assert db_schema._effective_base_branch("override") == "override"

    monkeypatch.delenv("POSTHOG_SCHEMA_RESTORE_BASE_BRANCH", raising=False)
    assert db_schema._effective_base_branch(None) == db_schema.DEFAULT_BASE_BRANCH


@pytest.mark.parametrize(
    "mode,is_empty,expected_inspections",
    [
        ("off", True, []),
        ("auto", False, ["posthog"]),
    ],
)
def test_restore_schema_if_fresh_skips_without_download(
    mode: db_schema.ArtifactMode,
    is_empty: bool,
    expected_inspections: list[str],
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    inspections: list[str] = []

    def fake_is_database_empty(target_db: str) -> bool:
        inspections.append(target_db)
        return is_empty

    monkeypatch.setattr(db_schema, "is_database_empty", fake_is_database_empty)
    monkeypatch.setattr(
        db_schema,
        "download_latest_compatible_schema",
        lambda **kwargs: pytest.fail("download should not run"),
    )

    assert db_schema.restore_schema_if_fresh(target_db="posthog", mode=mode) is False
    assert inspections == expected_inspections


def test_restore_schema_if_fresh_recreates_empty_db(monkeypatch: pytest.MonkeyPatch) -> None:
    restore_calls: list[dict[str, Any]] = []
    monkeypatch.setattr(db_schema, "is_database_empty", lambda target_db: True)
    monkeypatch.setattr(db_schema, "download_latest_compatible_schema", lambda **kwargs: None)
    monkeypatch.setattr(db_schema, "restore_schema_dump", lambda **kwargs: restore_calls.append(kwargs))

    assert db_schema.restore_schema_if_fresh(target_db="posthog", mode="auto") is True
    assert restore_calls == [{"target_db": "posthog", "recreate": True}]


def test_restore_schema_if_fresh_skips_when_db_populated_after_download(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """If another process writes to the DB during the artifact download, the
    post-download re-check must abort before DROP/CREATE."""
    empty_responses = iter([True, False])
    monkeypatch.setattr(db_schema, "is_database_empty", lambda target_db: next(empty_responses))
    monkeypatch.setattr(db_schema, "download_latest_compatible_schema", lambda **kwargs: None)
    monkeypatch.setattr(
        db_schema,
        "restore_schema_dump",
        lambda **kwargs: pytest.fail("restore must not run when DB stopped being empty"),
    )

    assert db_schema.restore_schema_if_fresh(target_db="posthog", mode="auto") is False


def test_run_psql_with_gzip_input_uses_transactional_error_stopping(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    schema_path = tmp_path / "schema.sql.gz"
    _write_schema(schema_path)
    commands: list[list[str]] = []

    class FakeStdin:
        def __init__(self) -> None:
            self.data = b""

        def write(self, chunk: bytes) -> int:
            self.data += chunk
            return len(chunk)

        def close(self) -> None:
            pass

    class SuccessfulProcess:
        def __init__(self) -> None:
            self.stdin = FakeStdin()

        def wait(self) -> int:
            return 0

        def kill(self) -> None:
            pytest.fail("process should not be killed")

    def fake_popen(command: list[str], **kwargs: object) -> SuccessfulProcess:
        commands.append(command)
        return SuccessfulProcess()

    monkeypatch.setattr(db_schema.subprocess, "Popen", fake_popen)

    db_schema._run_psql_with_gzip_input(schema_path, "posthog")

    assert len(commands) == 1
    assert "-v" in commands[0]
    assert "ON_ERROR_STOP=1" in commands[0]
    assert "--single-transaction" in commands[0]


def test_restore_schema_dump_recreate_drops_and_creates(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    schema_path = tmp_path / "schema.sql.gz"
    _write_schema(schema_path)
    commands: list[list[str]] = []
    restored: list[tuple[Path, str]] = []

    monkeypatch.setattr(db_schema, "_run", lambda command, env=None: commands.append(command))
    monkeypatch.setattr(
        db_schema,
        "_run_psql_with_gzip_input",
        lambda gzip_path, target_db: restored.append((gzip_path, target_db)),
    )

    db_schema.restore_schema_dump(target_db="test_posthog", recreate=True, schema_path=schema_path)

    assert any("DROP DATABASE IF EXISTS test_posthog;" in command for call in commands for command in call)
    assert any("CREATE DATABASE test_posthog;" in command for call in commands for command in call)
    assert restored == [(schema_path, "test_posthog")]
    assert not [command for command in commands if command[:2] == ["python", "manage.py"]]


def test_restore_schema_dump_forgets_product_app_migrations(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    # The dump comes from a database that routes product apps elsewhere, so it records their
    # migrations as applied without ever creating their tables. Left in place, the next product
    # migration is applied for real here and fails on the missing table. Nothing else may go: a
    # forgotten row outside those apps makes the caller's migrate re-run DDL the dump already holds.
    schema_path = tmp_path / "schema.sql.gz"
    _write_schema(schema_path)
    commands: list[list[str]] = []

    monkeypatch.setattr(db_schema, "_run", lambda command, env=None: commands.append(command))
    monkeypatch.setattr(db_schema, "_run_psql_with_gzip_input", lambda gzip_path, target_db: None)
    monkeypatch.setattr(db_schema, "_product_routed_app_labels", lambda: ["stamphog", "visual_review"])

    db_schema.restore_schema_dump(target_db="test_posthog", recreate=False, schema_path=schema_path)

    deletes = [command[-1] for command in commands if "DELETE FROM django_migrations" in command[-1]]
    assert deletes == ["DELETE FROM django_migrations WHERE app IN ('stamphog', 'visual_review');"]
    assert not [command for command in commands if command[:3] == ["python", "manage.py", "migrate"]]


def test_restore_schema_dump_recreate_cleans_up_after_failure(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    schema_path = tmp_path / "schema.sql.gz"
    _write_schema(schema_path)
    commands: list[list[str]] = []

    monkeypatch.setattr(db_schema, "_run", lambda command, env=None: commands.append(command))
    monkeypatch.setattr(
        db_schema,
        "_run_psql_with_gzip_input",
        lambda gzip_path, target_db: (_ for _ in ()).throw(db_schema.SchemaRestoreError("restore failed")),
    )

    with pytest.raises(db_schema.SchemaRestoreError, match="restore failed"):
        db_schema.restore_schema_dump(target_db="test_posthog", recreate=True, schema_path=schema_path)

    admin_sql = [command[-1] for command in commands]
    terminate_sql = (
        "SELECT pg_terminate_backend(pid) FROM pg_stat_activity "
        "WHERE datname = 'test_posthog' AND pid <> pg_backend_pid();"
    )
    assert admin_sql == [
        terminate_sql,
        "DROP DATABASE IF EXISTS test_posthog;",
        "CREATE DATABASE test_posthog;",
        terminate_sql,
        "DROP DATABASE IF EXISTS test_posthog;",
        "CREATE DATABASE test_posthog;",
    ]


def test_restore_schema_dump_without_recreate_does_not_drop(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    schema_path = tmp_path / "schema.sql.gz"
    _write_schema(schema_path)
    commands: list[list[str]] = []

    monkeypatch.setattr(db_schema, "_run", lambda command, env=None: commands.append(command))
    monkeypatch.setattr(db_schema, "_run_psql_with_gzip_input", lambda gzip_path, target_db: None)

    db_schema.restore_schema_dump(target_db="posthog", recreate=False, schema_path=schema_path)

    assert not any("DROP DATABASE" in command for call in commands for command in call)
    assert not any("CREATE DATABASE" in command for call in commands for command in call)


@pytest.mark.parametrize(
    "mode,expected_exit,expected_output",
    [
        ("auto", 0, "falling back to migrations"),
        ("on", 1, "no compatible artifact"),
    ],
)
def test_restore_schema_if_fresh_handles_unavailable_artifact(
    mode: str, expected_exit: int, expected_output: str, monkeypatch: pytest.MonkeyPatch
) -> None:
    monkeypatch.setattr(db_schema, "is_database_empty", lambda target_db: True)
    monkeypatch.setattr(
        db_schema,
        "download_latest_compatible_schema",
        lambda **kwargs: (_ for _ in ()).throw(db_schema.SchemaRestoreUnavailable("no compatible artifact")),
    )

    result = runner.invoke(db_schema.db_restore_schema_if_fresh, [f"--mode={mode}"])

    assert result.exit_code == expected_exit
    assert expected_output in result.output


def test_restore_schema_if_fresh_auto_does_not_fallback_when_cleanup_failed(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setattr(
        db_schema,
        "restore_schema_if_fresh",
        lambda **kwargs: (_ for _ in ()).throw(db_schema.SchemaRestoreCleanupFailed("cleanup failed")),
    )

    result = runner.invoke(db_schema.db_restore_schema_if_fresh, ["--mode=auto"])

    assert result.exit_code == 1
    assert "cleanup failed" in result.output


def test_restore_schema_fresh_recreates_target_db(monkeypatch: pytest.MonkeyPatch) -> None:
    calls: list[dict[str, Any]] = []
    monkeypatch.setenv("TARGET_DB", "posthog_e2e_test")
    monkeypatch.setattr(db_schema, "restore_schema_dump", lambda **kwargs: calls.append(kwargs))

    result = runner.invoke(db_schema.db_restore_schema_fresh)

    assert result.exit_code == 0
    assert calls == [{"target_db": "posthog_e2e_test", "recreate": True}]


@pytest.mark.parametrize(
    "command",
    [
        db_schema.db_download_schema,
        db_schema.db_restore_test_db,
        db_schema.db_restore_schema_fresh,
        db_schema.db_restore_schema_if_fresh,
    ],
    ids=lambda cmd: cmd.name,
)
def test_converted_commands_accept_forwarded_yes_flag(command: Any, monkeypatch: pytest.MonkeyPatch) -> None:
    """hogli composite commands forward --yes to children; converted click
    commands must silently accept it (regression for db:prime-test-db --yes)."""
    monkeypatch.setattr(db_schema, "download_latest_compatible_schema", lambda **kwargs: None)
    monkeypatch.setattr(db_schema, "restore_schema_dump", lambda **kwargs: None)
    monkeypatch.setattr(db_schema, "restore_schema_if_fresh", lambda **kwargs: None)

    for flag in ("--yes", "-y"):
        result = runner.invoke(command, [flag])
        assert result.exit_code == 0, f"{command.name} {flag} failed: {result.output}"