Skip to content

Commit 037ddb6

Browse files
bokelleyclaude
andcommitted
fix(reporting-storage): check for reporting tables in the current schema only
`to_regclass('name')` resolves through the whole search_path. With `search_path=tenant,public` and a ledger already in `public`, every unqualified existence check in the package could find `public`'s table while working on `tenant`: - `applied_migrations()` read `public`'s `adcp_schema_migrations`, so `python -m adcp migrate` printed "up to date" and left `tenant` unmigrated, and `postgres_storage`'s `check_schema` passed (#1838). - Migration 0002 (`reporting_ledger_account_generations.sql`) and 0006 (`reporting_status_checkpoint_offerings.sql`) took another schema's managed-delivery and status tables as present and ran their `ALTER TABLE` on the unqualified name. `tenant`'s upgrade then altered `public`'s tables, or failed with DuplicateColumn when `public` was already migrated. - The reconciliation DDL took another schema's feed as installed and skipped this schema's feed backfill. - `_period_close_generations_on` took another schema's materializer candidates as present and read them. Qualify each check with `quote_ident(current_schema())`, the pattern the rest of the package already uses, and read the recorded migrations from the current schema's table. A static test refuses any new unqualified `to_regclass('...')` in the package. Real PostgreSQL tests run `migrate` and `create_schema()` for a schema beside a migrated one on the same search_path. They check that the schema is built in place and that the other schema's catalog is unchanged. Closes #1838 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2e79ea9 commit 037ddb6

6 files changed

Lines changed: 143 additions & 13 deletions

File tree

‎packages/adcp-reporting-storage/src/adcp_reporting_storage/ledger/pg.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3301,7 +3301,8 @@ async def _period_close_generations_on(
33013301
# every catalog object on the lease path. Call only after the account lock.
33023302
present = await (
33033303
await connection.execute(
3304-
"SELECT to_regclass('reporting_materializer_candidates') IS NOT NULL"
3304+
"SELECT to_regclass(quote_ident(current_schema())"
3305+
" || '.reporting_materializer_candidates') IS NOT NULL"
33053306
)
33063307
).fetchone()
33073308
if not present[0]:

‎packages/adcp-reporting-storage/src/adcp_reporting_storage/ledger/reporting_ledger_account_generations.sql‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ BEGIN
9595
-- (reconciliation records, materializer discovery and status checkpoints).
9696
-- Production generations serve account-level generations only; a
9797
-- subscription generation is refused by their CHECK.
98-
IF to_regclass('reporting_reconciliation_records') IS NOT NULL THEN
98+
IF to_regclass(quote_ident(current_schema()) || '.reporting_reconciliation_records') IS NOT NULL THEN
9999
ALTER TABLE reporting_reconciliation_records
100100
ADD COLUMN offering_id TEXT COLLATE "C" NOT NULL DEFAULT '',
101101
ADD CONSTRAINT reporting_reconciliation_records_generation_fkey
@@ -104,7 +104,7 @@ BEGIN
104104
REFERENCES reporting_configurations(account_id, consumer_id, delivery_config_id,
105105
delivery_config_version, offering_id);
106106
END IF;
107-
IF to_regclass('reporting_materializer_discovery') IS NOT NULL THEN
107+
IF to_regclass(quote_ident(current_schema()) || '.reporting_materializer_discovery') IS NOT NULL THEN
108108
ALTER TABLE reporting_materializer_discovery
109109
ADD COLUMN offering_id TEXT COLLATE "C" NOT NULL DEFAULT '',
110110
ADD CONSTRAINT reporting_materializer_discovery_generation_fkey
@@ -113,7 +113,7 @@ BEGIN
113113
REFERENCES reporting_configurations(account_id, consumer_id, delivery_config_id,
114114
delivery_config_version, offering_id);
115115
END IF;
116-
IF to_regclass('reporting_status_scope_checkpoints') IS NOT NULL THEN
116+
IF to_regclass(quote_ident(current_schema()) || '.reporting_status_scope_checkpoints') IS NOT NULL THEN
117117
ALTER TABLE reporting_status_scope_checkpoints
118118
ADD COLUMN offering_id TEXT COLLATE "C" NOT NULL DEFAULT '',
119119
ADD CONSTRAINT reporting_status_scope_checkpoints_generation_fkey
@@ -122,7 +122,7 @@ BEGIN
122122
REFERENCES reporting_configurations(account_id, consumer_id, delivery_config_id,
123123
delivery_config_version, offering_id);
124124
END IF;
125-
IF to_regclass('reporting_production_generations') IS NOT NULL THEN
125+
IF to_regclass(quote_ident(current_schema()) || '.reporting_production_generations') IS NOT NULL THEN
126126
ALTER TABLE reporting_production_generations
127127
ADD COLUMN offering_id TEXT COLLATE "C" NOT NULL DEFAULT ''
128128
CONSTRAINT reporting_production_generations_account_generation

‎packages/adcp-reporting-storage/src/adcp_reporting_storage/ledger/reporting_ledger_reconciliation.sql‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,10 @@ BEGIN
829829

830830
-- The optional feed has a dense head for each authenticated principal. It
831831
-- never allocates Core ledger sequences or widens Core's record vocabulary.
832-
feed_installed := to_regclass('reporting_reconciliation_changes') IS NOT NULL;
832+
-- Look in this schema only, so another schema's feed on the search_path
833+
-- cannot skip this one's backfill.
834+
feed_installed := to_regclass(
835+
quote_ident(current_schema()) || '.reporting_reconciliation_changes') IS NOT NULL;
833836
CREATE UNIQUE INDEX IF NOT EXISTS reporting_reconciliation_feed_identity
834837
ON reporting_reconciliation_records(account_id, consumer_id, namespace, record_id,
835838
record_kind, change_id, content_sha256);

‎packages/adcp-reporting-storage/src/adcp_reporting_storage/ledger/reporting_status_checkpoint_offerings.sql‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ DECLARE
3232
events TEXT;
3333
BEGIN
3434
PERFORM pg_advisory_xact_lock(hashtext('adcp.reporting.schema'), hashtext(current_schema()));
35-
IF to_regclass('reporting_status_scope_checkpoints') IS NULL THEN
35+
IF to_regclass(quote_ident(current_schema()) || '.reporting_status_scope_checkpoints') IS NULL THEN
3636
RETURN;
3737
END IF;
3838
SELECT ARRAY(
@@ -224,14 +224,14 @@ BEGIN
224224
RETURN NEW;
225225
END
226226
$event_guard$;
227-
IF to_regclass('reporting_status_notification_events') IS NOT NULL AND NOT EXISTS (
227+
IF to_regclass(quote_ident(current_schema()) || '.reporting_status_notification_events') IS NOT NULL AND NOT EXISTS (
228228
SELECT 1 FROM pg_trigger WHERE tgrelid='reporting_status_notification_events'::regclass
229229
AND tgname='reporting_status_event_guard' AND NOT tgisinternal) THEN
230230
CREATE TRIGGER reporting_status_event_guard BEFORE INSERT
231231
ON reporting_status_notification_events FOR EACH ROW EXECUTE FUNCTION reporting_status_event_guard();
232232
END IF;
233233

234-
IF to_regclass('reporting_projection_accounts') IS NULL THEN
234+
IF to_regclass(quote_ident(current_schema()) || '.reporting_projection_accounts') IS NULL THEN
235235
RETURN;
236236
END IF;
237237
-- The projection's status events: the same checkpoint check, after

‎packages/adcp-reporting-storage/src/adcp_reporting_storage/migrations/__init__.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,26 @@ def migrations() -> tuple[Migration, ...]:
145145

146146

147147
async def applied_migrations(connection: Any) -> dict[str, datetime]:
148-
"""``version`` -> ``applied_at`` for every recorded migration (empty when none)."""
149-
cursor = await connection.execute("SELECT to_regclass('adcp_schema_migrations') IS NOT NULL")
148+
"""``version`` -> ``applied_at`` for every recorded migration (empty when none).
149+
150+
Reads the ``current_schema()`` table only: an unqualified name resolves
151+
through the whole ``search_path``, so another schema's record would
152+
otherwise report this one as migrated.
153+
"""
154+
from psycopg import sql
155+
156+
cursor = await connection.execute(
157+
"SELECT current_schema(),"
158+
" to_regclass(quote_ident(current_schema()) || '.adcp_schema_migrations') IS NOT NULL"
159+
)
150160
row = await cursor.fetchone()
151-
if not row or not row[0]:
161+
if not row or not row[1]:
152162
return {}
153-
cursor = await connection.execute("SELECT version, applied_at FROM adcp_schema_migrations")
163+
cursor = await connection.execute(
164+
sql.SQL("SELECT version, applied_at FROM {}").format(
165+
sql.Identifier(row[0], "adcp_schema_migrations")
166+
)
167+
)
154168
return {str(version): applied_at for version, applied_at in await cursor.fetchall()}
155169

156170

‎tests/conformance/reporting/test_reporting_storage_migrations_pg.py‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
from contextlib import asynccontextmanager
6+
57
import pytest
68

79
from adcp.reporting._errors import ReliableReportingConfigurationError
@@ -146,3 +148,113 @@ async def test_a_managed_service_runs_on_postgres_storage() -> None:
146148
assert await engine.list_subscription_principals() == [PrincipalRef(None, AGENCY)]
147149
finally:
148150
await service.close()
151+
152+
153+
def test_no_storage_existence_check_resolves_through_the_search_path() -> None:
154+
"""#1838: ``to_regclass('name')`` finds a table in any schema on the path.
155+
156+
An existence check that does would read, skip or alter another schema's
157+
tables; qualify it with ``quote_ident(current_schema())``.
158+
"""
159+
import re
160+
from pathlib import Path
161+
162+
import adcp_reporting_storage
163+
164+
root = Path(adcp_reporting_storage.__file__).parent
165+
unqualified = re.compile(r"to_regclass\(\s*'")
166+
found = [
167+
f"{path.relative_to(root)}:{number}"
168+
for path in sorted(root.rglob("*"))
169+
if path.suffix in {".py", ".sql"}
170+
for number, line in enumerate(path.read_text().splitlines(), 1)
171+
if unqualified.search(line)
172+
]
173+
assert found == []
174+
175+
176+
async def _schema(pool) -> str:
177+
async with pool.connection() as connection:
178+
return (await (await connection.execute("SELECT current_schema()")).fetchone())[0]
179+
180+
181+
async def _catalog(pool, schema: str) -> list[tuple]:
182+
"""Columns, constraints and recorded migrations of ``schema``."""
183+
async with pool.connection() as connection:
184+
columns = await (
185+
await connection.execute(
186+
"SELECT table_name, column_name, data_type FROM information_schema.columns"
187+
" WHERE table_schema = %s ORDER BY 1, 2",
188+
(schema,),
189+
)
190+
).fetchall()
191+
constraints = await (
192+
await connection.execute(
193+
"SELECT c.conrelid::regclass::text, c.conname, pg_get_constraintdef(c.oid)"
194+
" FROM pg_constraint c JOIN pg_namespace n ON n.oid = c.connamespace"
195+
" WHERE n.nspname = %s ORDER BY 1, 2",
196+
(schema,),
197+
)
198+
).fetchall()
199+
recorded = await (
200+
await connection.execute("SELECT version FROM adcp_schema_migrations ORDER BY 1")
201+
).fetchall()
202+
return [*columns, *constraints, *recorded]
203+
204+
205+
@asynccontextmanager
206+
async def _search_path(*schemas: str):
207+
import os
208+
209+
from psycopg_pool import AsyncConnectionPool
210+
211+
async with AsyncConnectionPool(
212+
os.environ["ADCP_PG_TEST_URL"],
213+
kwargs={"options": f"-csearch_path={','.join(schemas)}", "autocommit": True},
214+
min_size=1,
215+
max_size=2,
216+
open=False,
217+
) as pool:
218+
await pool.wait(timeout=10)
219+
yield pool
220+
221+
222+
@pytest.mark.asyncio
223+
async def test_migrate_upgrades_its_own_schema_beside_a_migrated_one() -> None:
224+
"""#1838: a migrated schema later on the search_path is not this schema's record."""
225+
from adcp_reporting_storage.migrations import migrate, migrations, pending_migrations
226+
227+
every = [item.version for item in migrations()]
228+
async with (
229+
isolated_reporting_pool(autocommit=True) as other,
230+
isolated_reporting_pool(autocommit=True) as own,
231+
):
232+
await migrate(other)
233+
other_schema, own_schema = await _schema(other), await _schema(own)
234+
before = await _catalog(other, other_schema)
235+
async with _search_path(own_schema, other_schema) as shared:
236+
assert [item.version for item in await pending_migrations(shared)] == every
237+
assert [item.version for item in await migrate(shared)] == every
238+
assert await pending_migrations(shared) == []
239+
assert await pending_migrations(own) == []
240+
assert await _catalog(own, own_schema) == before # the same schema, built in place
241+
assert await _catalog(other, other_schema) == before
242+
243+
244+
@pytest.mark.asyncio
245+
async def test_create_schema_builds_its_own_schema_beside_a_migrated_one() -> None:
246+
from adcp_reporting_storage.ledger.pg import PgReportingLedgerStore
247+
from adcp_reporting_storage.migrations import migrate, pending_migrations
248+
249+
async with (
250+
isolated_reporting_pool(autocommit=True) as other,
251+
isolated_reporting_pool(autocommit=True) as own,
252+
):
253+
await migrate(other)
254+
other_schema, own_schema = await _schema(other), await _schema(own)
255+
before = await _catalog(other, other_schema)
256+
async with _search_path(own_schema, other_schema) as shared:
257+
await PgReportingLedgerStore(pool=shared).create_schema()
258+
# create_schema() records its own 0001-0003; the rest stay for ``migrate``.
259+
assert [item.version for item in await pending_migrations(own)] == ["0004", "0005", "0006"]
260+
assert await _catalog(other, other_schema) == before

0 commit comments

Comments
 (0)