fix: doctor looks for alembic_version where alembic puts it

env.py sets no version_table_schema, so the table follows search_path. Pinning
public. in doctor's queries made them agree with each other and disagree with
alembic: on an install using another schema it reported no schema at all and
sent the user to migrate an already-migrated database. Both queries resolve the
table the same way alembic does now.
This commit is contained in:
Alex committed 2026-09-17 12:51:50 +01:00
1 parent bb50d0a91d
commit 48285f4405
2 files changed
+14 -7

No files matched your search

+5 -3
View File
@@ -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:
+9 -4
View File
@@ -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."""