Skip to content

fix(db): repair search_path clobbered by migration 308 - #448

Merged
thinkclay merged 1 commit into
mainfrom
fix/442-search-path-clobber
Jul 30, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/442-search-path-clobber

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Fixes #442. Part of #440.

What broke

308_function_search_path.sql blanket-ran ALTER FUNCTION ... SET search_path = <own schema> over every function it owned, discarding the value each function was declared with. 37 functions were rewritten. Five of them resolve extension objects through search_path and broke outright:

Function Uses Symptom
core.get_jobs_with_coords ST_X/ST_Y, ::geometry /workers/map job layer — the reported error
core.get_organizations_with_coords ST_X/ST_Y, ::geometry, citext /workers/map org layer
core.validate_address_in_site ST_Contains, ::geometry project address validation
core.check_site_overlaps ST_Area/ST_Intersection/ST_Overlaps project site geo validation
onet.search_occupations SIMILARITY (pg_trgm) career-assessment occupation search
ERROR:  42704 type "geometry" does not exist
LINE 9:     ST_X(j.geo::geometry) AS longitude,

The other 32 fully-qualify their references and were unaffected — I checked each rather than assuming.

What this does

341 restores a working search_path on all five. It lists both public and extensions, because 309 tries to move postgis/citext/pg_trgm into extensions but that ALTER needs extension ownership and silently no-ops where migrations don't own them (it no-op'd locally). Listing both means these functions work whether or not 309 took effect.

308 now only fills in a search_path where one is MISSING. It can no longer overwrite a declared value, so this cannot recur on a fresh db reset or in a newly provisioned environment.

341 ends with a verification block that executes all five functions and RAISEs rather than committing a silent regression.

Two things that block caught

  1. onet.search_occupations had a second, independent break. It declares RETURNS TABLE(onetsoc_code text, title text, ...) while onet.occupation_data stores those as character(10) and varchar(150). plpgsql RETURN QUERY demands an exact type match, so it raised structure of query does not match function result type — and the uncast columns were also breaking SIMILARITY() overload resolution. Recreated here with casts. CREATE OR REPLACE, no DROP, so GRANTs survive.
  2. Two of the five predate 308. validate_address_in_site and check_site_overlaps were declared SET search_path = core back in 105, so they have never worked. Not 308's fault; fixed here regardless.

Security posture

308 was chasing Supabase advisor #172 (mutable search_path). That is preserved — every search_path here stays explicit and immutable. Adding public/extensions introduces no object-shadowing risk for these SECURITY DEFINER functions because neither schema grants CREATE to anon or authenticated (verified: only pg_database_owner holds it), so no untrusted role can plant a shadowing object.

Verification

  • Full pnpm supa db reset applies the entire 177-migration chain clean, with 341's verification block passing
  • 341 is idempotent — second run reports 0 search_path changes
  • public.get_jobs_with_coords() → 11 pins; public.get_organizations_with_coords() → 9 pins
  • onet.search_occupations('carpenter', 3) → Carpenters 0.750, Helpers--Carpenters 0.500, Cabinetmakers and Bench Carpenters 0.281 (verified against an inserted row, since O*NET data comes from the separate supa:seed:onet script)
  • core.validate_address_in_site and core.check_site_overlaps execute without error

On CI: all six workflows are already failing on main, pre-existing. The api suite is 113 passed / 54 failed locally; none of the 54 reference any function this PR touches, and the failures are 400/401/500 auth-class with no 42704 — the known ES256/auth.uid() breakage. This change is two .sql files that nothing imports, so no JS/TS build or test input is affected.

Follow-up, deliberately not in scope

10 functions from migrations 314–325 have no search_path at all — they were created after 308 ran, so they were never covered in either form. Pre-existing advisor gap, filed separately.

308 blanket-ran `ALTER FUNCTION ... SET search_path = <own schema>` over every
function it owned, discarding the declared value on 37 functions. Five of them
resolve extension objects through search_path and broke outright:

  core.get_jobs_with_coords            ST_X/ST_Y, ::geometry  -> /workers/map
  core.get_organizations_with_coords   ST_X/ST_Y, ::geometry, citext
  core.validate_address_in_site        ST_Contains, ::geometry
  core.check_site_overlaps             ST_Area/Intersection/Overlaps
  onet.search_occupations              SIMILARITY (pg_trgm)

  ERROR: 42704 type "geometry" does not exist

The other 32 fully-qualify their references and were unaffected.

341 restores a working search_path on all five. It lists both `public` and
`extensions` because 309 tries to move postgis/citext/pg_trgm into `extensions`
but silently no-ops where migrations do not own the extensions -- listing both
means these work either way. Every search_path stays explicit and immutable, so
308's advisor #172 posture is kept; neither schema grants CREATE to
anon/authenticated, so widening resolution introduces no shadowing risk for
these SECURITY DEFINER functions.

341 ends with a verification block that executes all five and raises rather
than committing a silent regression. That block caught a second latent break:
onet.search_occupations declares RETURNS TABLE(onetsoc_code text, title text)
while the columns are character(10) and varchar(150), so RETURN QUERY raised
"structure of query does not match function result type" -- and the uncast
columns were also breaking SIMILARITY() overload resolution. It is recreated
here with the casts. That one predates 308, as do validate_address_in_site and
check_site_overlaps, which 105 declared `SET search_path = core`.

308 itself now only fills in a search_path where one is MISSING, so it can no
longer overwrite a declared value on a fresh reset or in a new environment.

Verified: full `supabase db reset` applies the chain clean; 341 is idempotent;
get_jobs_with_coords returns 11 pins, get_organizations_with_coords 9,
search_occupations ranks correctly.

Refs #442, #440

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit d4d8ce8 into main Jul 30, 2026
2 of 10 checks passed
@thinkclay
thinkclay deleted the fix/442-search-path-clobber branch July 30, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(db): migration 308 clobbered search_path, breaking 5 PostGIS/pg_trgm functions

1 participant