Skip to content

Make SQL migrations the single source of the Postgres search schema - #3

Merged
tae2089 merged 1 commit into
mainfrom
refactor/postgres-schema-single-source
Jul 11, 2026
Merged

Make SQL migrations the single source of the Postgres search schema#3
tae2089 merged 1 commit into
mainfrom
refactor/postgres-schema-single-source

Conversation

@tae2089

@tae2089 tae2089 commented Jul 10, 2026

Copy link
Copy Markdown
Owner

문제

PostgresBackend.Migrate가 마이그레이션 000001이 이미 만드는 tsvector 컬럼·트리거·GIN 인덱스를 Go 인라인 SQL로 중복 생성했고, namespace DEFAULT ''(SQL은 'default') 드리프트가 있었음. 게다가 pg_trgm 확장·trgm 인덱스는 Migrate에만 있었는데 프로덕션은 Migrate를 호출하지 않음 → 퍼지 검색이 프로덕션서 인덱스 없이(seq scan) 돌았음.

변경

  • pg_trgm + trgm 인덱스를 새 마이그레이션 000006으로 이동. CREATE EXTENSIONDO 블록으로 감싸 권한 없을 때 graceful degrade(확장 있을 때만 인덱스 생성) — startup 하드실패 방지. RequiredSchemaVersion 5→6.
  • PostgresBackend.Migrate를 마이그레이션 실행으로 축소 → 스키마 정의 단일화, 드리프트 불가능.
  • Postgres 검색 테스트를 실제 마이그레이션(스키마 리셋 + RunMigrations)으로 전환 (AutoMigrate + 백엔드 DDL 제거). down-migration step 수·required_version 로그 단언을 v6로 조정.

검증

live Postgres(격리 DB)에서 확인:

  • 마이그레이션이 pg_trgm 확장 + trgm 인덱스 2개 생성 (이전엔 프로덕션서 안 생기던 것)
  • search_documents.namespace DEFAULT 'default' (드리프트 해소)
  • default 스위트 36 패키지 그린, postgres-tagged(search·cmd/ccg) 그린

🤖 Generated with Claude Code

PostgresBackend.Migrate hand-wrote the tsvector column, trigger, and GIN
index that migration 000001 already creates, with a drift (namespace
DEFAULT '' vs 'default'), and it created the pg_trgm extension + trigram
indexes that no production path ever called — so fuzzy search ran without
its indexes in production.

- Move pg_trgm + the trigram indexes into a new migration (000006). A DO
  block attempts CREATE EXTENSION and degrades gracefully (indexes only
  created when the extension is present) so a missing privilege does not
  fail startup. Bump RequiredSchemaVersion to 6.
- Reduce PostgresBackend.Migrate to running the versioned migrations, so
  there is one schema definition and it cannot drift.
- Point the Postgres search tests at the real migrations (reset schema +
  RunMigrations) instead of AutoMigrate + backend DDL, and adjust the
  down-migration step counts and required-version log assertions for v6.

Verified against a live Postgres: migrations create pg_trgm, both trigram
indexes, and namespace DEFAULT 'default'.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@tae2089
tae2089 merged commit bb6d8b5 into main Jul 11, 2026
1 check failed
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.

1 participant