Skip to content

Add full-text search and verify PostgreSQL support - #11

Merged
ShaneCurcuru merged 5 commits into
mainfrom
claude/project-thread-jqn51r
Nov 7, 2026
Merged

ShaneCurcuru merged 5 commits into
mainfrom
claude/project-thread-jqn51r

Conversation

@ShaneCurcuru

@ShaneCurcuru ShaneCurcuru commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Shane · project thread

What this does

This PR implements the search index from the end of step 7 in docs/_prompts/plan/HomeIOT-Master-Plan.md and section 2.5 of data-storage-backup.md. It also checks the experimental PostgreSQL support (decision 2) by running the whole test suite against Postgres.

Search

There is one SearchIndex service (src/domus/services/search.py) with two engines behind it.

  • SQLite: an FTS5 table, search_fts. It uses the unicode61 tokenizer with diacritics removed, and bm25 ranking that weights title matches highest.
  • PostgreSQL: a search_doc table with generated tsvector columns. It uses the simple configuration, so model numbers are not stemmed. A pg_trgm word-similarity match on the title catches typos such as "hallwya".
  • Migration 0003 comes after main's 0002 for canvases and creates whichever of the two fits the engine. The SQL is copied into the migration, so later changes to the service can't break it.
  • GET /api/v1/search?q=&property_id=&limit= returns {query, assets, areas}. Every word has to match as the start of a word. Results go through the same access checks as the list endpoints, so hidden items are left out and guests get the guest schemas.
  • Guests never match private fields. Each document keeps two text fields. The public one holds the name, notes, product, tags, area and location. The private one holds identifiers such as serials and MACs, plus the vendor. Guest queries search only the public field.
  • Front end: a search box in the header, a "Search" nav entry, and a /search page that lists matching areas and devices.

The index is kept in sync by the application, not by database triggers:

  • A session listener, installed in make_session_factory, re-indexes whatever each commit touched. That includes devices created by the Home Assistant import.
  • The one bulk Core delete() (asset tags) marks its asset by hand.
  • A backup import, including loading the sample home, rebuilds the whole index inside its transaction.
  • On startup the app fills an empty index if data already exists.
  • The search tables are not on Base.metadata, so backups never carry them, and the Alembic schema comparison skips them.

PostgreSQL

  • New optional extra domus[postgres] = pg8000>=1.31.5. Shane approved it on the decision card. The licenses are pg8000 BSD-3-Clause, scramp MIT-0 and asn1crypto MIT. Versions 1.31.4 and older have CVE-2025-61385 (SQL injection via pg8000.native.literal), hence the minimum version. Domus doesn't call that function.
  • License check: Shane decided that the generic "BSD License" classifier is acceptable for every package. That is decision 23 in docs/_prompts/01-decisions.md; 21 and 22 were taken by Add Home Assistant device import (v0.1 step 10) #10. scripts/check_licenses.py now maps it to BSD-3-Clause, so pg8000 needs no override of its own.
  • Testing on Postgres: setting DOMUS_TEST_DATABASE_URL runs the whole suite against Postgres, with one fresh database per test. CONTRIBUTING.md has the steps.
  • Fixes found by running on Postgres 16:
    • pg8000 reports constraint violations as ProgrammingError, so a backup import with a missing parent row crashed instead of failing cleanly. The importer now treats SQLSTATE class 23 the same as IntegrityError.
    • The Postgres text parser reads "SN-77441" as "sn" and "-77441", so searching for a serial missed it. Postgres documents are now split into words the same way as queries.
    • The SQLite-pragma test is skipped on Postgres, and the search tests use the right table name for each engine.

How I read the ask

  • I read "add search" as indexing devices and areas. Document titles are not indexed yet. Uploads landed on main in Floor plans, wall photos, and device pins (plan step 9) #8 while this PR was open, and documents have their own sensitivity rules, so indexing them is a follow-up.
  • GET /api/v1/search is a new endpoint, so the published API grows. openapi.json and schema.d.ts are regenerated.
  • The coordinator's note said asyncpg. I used pg8000 because Shane's later decision was sync SQLAlchemy with pg8000.

Left undone

  • No Postgres CI job. It would need a maintainer's OK. CI also syncs with --all-groups but not --all-extras, so its license check doesn't cover the postgres extra. I checked it locally and all 62 distributions pass.
  • The plan docs still say "aiosqlite and asyncpg". I didn't edit them here.
  • The Postgres typo match uses the word_similarity() function rather than pg_trgm's <% operator. SQLAlchemy doesn't escape a % in a custom operator name, and pg8000 would read it as a parameter marker. So the trigram index on the title isn't used. That's fine at home scale.

A gotcha worth knowing

SQLAlchemy's event.contains() gives false positives for a new sessionmaker once an old one has been garbage collected, because its registry is keyed by id(). A quick script showed 2,981 false positives out of 3,000. An "already installed?" guard made search tests fail at random in the full suite, so install() has no guard and is called once per factory.

Checks

These were run after merging main, which now includes #8, #9 and #10.

  • uv run ruff check ., uv run ruff format --check ., uv run pyright (strict): clean
  • uv run pytest on SQLite: 428 passed, 1 skipped (the Postgres-only typo test)
  • The same suite on PostgreSQL 16.15 through pg8000 1.31.6: 428 passed, 1 skipped (the SQLite pragma test). That includes the upload, canvas and Home Assistant import code.
  • Front end: npm run lint, npm run typecheck, npx vitest run (76 passed), prettier: clean

Posted by Claude on behalf of @ShaneCurcuru

🤖 Generated with Claude Code

https://claude.ai/code/session_012UmrcoAD3ijA8mYmuzLyKz

A SearchIndex service with two engines: an FTS5 table on SQLite, and a
tsvector + pg_trgm table on PostgreSQL (data-storage-backup.md 2.5,
master plan step 7). A session listener re-indexes what each commit
touches, backup imports rebuild the index, and the app fills an empty
index at startup. Guests never match serials, MACs, or vendors.

Adds GET /api/v1/search, a header search box, and a search page.

Posted by Claude on behalf of @ShaneCurcuru

Generated-by: Claude Code
@ShaneCurcuru ShaneCurcuru self-assigned this Nov 6, 2026
Add pg8000 (BSD-3-Clause) as the optional `postgres` extra, as Shane
approved, requiring 1.31.5 or later for the CVE-2025-61385 fix. Its
dependencies are scramp (MIT-0) and asn1crypto (MIT).

Setting DOMUS_TEST_DATABASE_URL runs the whole suite against Postgres,
with one fresh database per test. Fixes found that way:

- Backup import: pg8000 reports constraint violations as
  ProgrammingError, so treat SQLSTATE class 23 as bad import data too.
- Search: index Postgres text split the same way as queries, since the
  Postgres parser reads "SN-77441" as "sn" and "-77441"; type the @@
  comparison as boolean; match title typos with word_similarity.
- Tests: skip the SQLite pragma test on Postgres, and use the right
  search table name per engine.

Posted by Claude on behalf of @ShaneCurcuru

Generated-by: Claude Code
@ShaneCurcuru ShaneCurcuru changed the title Add full-text search for devices and areas Add full-text search and verify PostgreSQL support Nov 6, 2026
Main added migration 0002 (canvases and placement), so the search index
migration becomes 0003 and follows it. openapi.json, schema.d.ts, and
uv.lock are regenerated.

Posted by Claude on behalf of @ShaneCurcuru

Generated-by: Claude Code
Shane decided that packages whose metadata says only "BSD License" are
acceptable without a per-package review (decision 21). The alias maps
it to BSD-3-Clause, so the pg8000 override is no longer needed.

Posted by Claude on behalf of @ShaneCurcuru

Generated-by: Claude Code
Main's PR #10 took decisions 21 and 22, so the generic BSD license
decision becomes 23. openapi.json, schema.d.ts, and uv.lock are
regenerated; the search router and nav entry sit alongside the new
Home Assistant and sample-data ones.

Posted by Claude on behalf of @ShaneCurcuru

Generated-by: Claude Code
@ShaneCurcuru
ShaneCurcuru marked this pull request as ready for review November 7, 2026 01:42
@ShaneCurcuru
ShaneCurcuru merged commit 1b6c859 into main Nov 7, 2026
7 checks passed
@ShaneCurcuru
ShaneCurcuru deleted the claude/project-thread-jqn51r branch November 7, 2026 01:42
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.

2 participants