Fix: partial unique indexes so soft-deleted records can be recreated - #491
Open
bamdadd wants to merge 4 commits into
Open
Fix: partial unique indexes so soft-deleted records can be recreated#491bamdadd wants to merge 4 commits into
bamdadd wants to merge 4 commits into
Conversation
Several unique indexes were created without a deleted_at predicate on tables that use GORM soft deletes (contacts, whatsapp_accounts, templates, sso_providers). Because a soft delete only sets deleted_at, the deleted row still participates in the unique index, so recreating a record with the same key fails with a duplicate-key violation (idx_whatsapp_accounts_org_phone, etc.) — e.g. deleting a WhatsApp account and re-adding the same number. Recreate these as partial unique indexes (WHERE deleted_at IS NULL), matching the pattern already used for team_members, user_organizations and ivr_flows. Old indexes are dropped first so existing installs migrate. The GORM-tag index idx_wa_org_name is replaced by a partial raw index for the same reason.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Deleting a record and re-creating one with the same unique key fails with a duplicate-key error. Example: delete a WhatsApp account in the UI, try to add the same phone number again →
Cause
These models use GORM soft deletes (
BaseModel.DeletedAt), but several unique indexes ingetIndexes()were created without aWHERE deleted_at IS NULLpredicate. A soft delete only setsdeleted_at, so the deleted row still participates in the unique index and blocks recreation.Affected (all soft-deleted tables):
idx_contacts_org_phone(contacts)idx_whatsapp_accounts_org_phone+idx_wa_org_name(whatsapp_accounts)idx_templates_account_name_lang(templates)idx_sso_providers_org_provider(sso_providers)The pattern is already applied correctly for
team_members,user_organizationsandivr_flows— this just makes it consistent.Fix
Recreate each as a partial unique index (
WHERE deleted_at IS NULL). The old index isDROPped first so existing installs migrate, and the new index uses a distinct_activename withIF NOT EXISTSso it isn't rebuilt on every boot.idx_wa_org_namewas created via a GORM struct tag, so the tag is removed and replaced with an equivalent partial raw index.No data changes; indexes only.