Skip to content

fix(db): correct channel contact merged_id foreign key#1290

Merged
clark-cant merged 1 commit into
nextlevelbuilder:devfrom
nguyenha935:fix/channel-contact-merged-fk
Jun 27, 2026
Merged

fix(db): correct channel contact merged_id foreign key#1290
clark-cant merged 1 commit into
nextlevelbuilder:devfrom
nguyenha935:fix/channel-contact-merged-fk

Conversation

@nguyenha935

Copy link
Copy Markdown
Contributor

Summary

  • Fix channel_contacts.merged_id FK to reference tenant_users(id).
  • merged_id is written by MergeContacts(... tenantUserID) and resolved via channel_contacts.merged_id = tenant_users.id.
  • The previous self-reference FK from migration 000083 is incompatible with the application data model and valid historical data.
  • Add migration 000086 for already-migrated beta databases and update FK audit checks.

Surface parity

  • Gateway/server: affected via PostgreSQL migration.
  • Web UI/API contract: N/A, no request/response shape changes.
  • CLI/runtime: N/A, no command or protocol changes.
  • SQLite: N/A, SQLite schema does not enforce this FK; sqlite build still verified.

Tests

  • Docker builder stage build succeeded with ENABLE_EMBEDUI=false.
  • /usr/local/go/bin/go test ./internal/store/... ./internal/upgrade/...
  • /usr/local/go/bin/go build -tags sqliteonly ./...
  • /usr/local/go/bin/go vet ./...
  • Restored a pre-fix DB dump into a temporary database and verified migration 000086 changes merged_id FK from channel_contacts(id) to tenant_users(id).

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦸‍♂️ Approved — Clean FK fix.

Summary: Corrects channel_contacts.merged_id FK to reference tenant_users(id) instead of the erroneous self-reference from migration 000083. Matches actual application data model (MergeContacts writes tenantUserID into merged_id).

Review:

  • Migration up/down: ✅ correct — drops old constraint, adds proper FK with ON DELETE SET NULL
  • Schema version bump: ✅ 85 → 86
  • Audit SQL: ✅ updated to check against tenant_users + added tenant drift check
  • CI: ✅ go, web, release-versioning all green
  • Scope: ✅ small, focused (34+/5−, 4 files)
  • Testing: ✅ author verified with pre-fix DB dump

Ship it.

@clark-cant clark-cant merged commit b72959b into nextlevelbuilder:dev Jun 27, 2026
3 checks passed
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