diff --git a/docsgpt/deploy/commands.py b/docsgpt/deploy/commands.py index 94a144f6..ca7be581 100644 --- a/docsgpt/deploy/commands.py +++ b/docsgpt/deploy/commands.py @@ -805,12 +805,14 @@ def _check_postgres(uri: Optional[str]) -> Check: with psycopg.connect(uri, connect_timeout=5) as connection, connection.cursor() as cursor: cursor.execute("select current_setting('server_version')") version = cursor.fetchone()[0] - cursor.execute("select to_regclass('public.alembic_version')") + # Unqualified, like alembic itself: env.py sets no version_table_schema, so the table + # lives wherever search_path puts it. Asserting public would call a migrated database empty. + cursor.execute("select to_regclass('alembic_version')") applied = cursor.fetchone()[0] is not None current = None if applied: - # public, like the to_regclass check above: search_path could resolve another one. - cursor.execute("select version_num from public.alembic_version") + # The same relation the check above resolved, by the same rules. + cursor.execute("select version_num from alembic_version") row = cursor.fetchone() current = row[0] if row else None except (psycopg.Error, OSError, ValueError) as exc: diff --git a/tests/deploy/test_doctor.py b/tests/deploy/test_doctor.py index 4acca1d5..4cc4a343 100644 --- a/tests/deploy/test_doctor.py +++ b/tests/deploy/test_doctor.py @@ -261,12 +261,17 @@ class TestPostgresCheck: assert check.level == "ok" assert "16.2" in check.detail and "0031_x" in check.detail - def test_the_revision_comes_from_the_schema_that_was_checked(self, monkeypatch): - """to_regclass looks in public, so the second query must not resolve through search_path.""" + def test_both_queries_resolve_the_same_table(self, monkeypatch): + """Alembic sets no version_table_schema, so the table follows search_path; asserting a + schema in one query and not the other is how doctor called a migrated database empty.""" cursor = _postgres_answering(monkeypatch) commands._check_postgres("postgresql://localhost/d") - revision_query = [statement for statement in cursor.statements if "version_num" in statement] - assert revision_query and all("public.alembic_version" in statement for statement in revision_query) + looked_up = [statement for statement in cursor.statements if "to_regclass" in statement] + read = [statement for statement in cursor.statements if "version_num" in statement] + assert looked_up and read + assert all("public." not in statement for statement in looked_up + read), ( + "neither query may pin a schema alembic never promised" + ) def test_a_database_with_no_schema_yet(self, monkeypatch): """The commonest first-run state: the database exists, nothing has been migrated into it."""