bmc_hub/migrations/185_locations_contacts_unique_related.sql
Christian 770f822fc6 feat: Implement bug reporting feature with screenshot support
- Added a new modal for reporting bugs, including fields for describing the issue and attaching optional files.
- Integrated automatic screenshot capture functionality when the bug report modal is opened.
- Created a new API endpoint for submitting bug reports, including validation and rate limiting.
- Added database migration for tracking bug report submissions.
- Updated frontend scripts to handle bug report submissions and display status messages.
- Enhanced contact search functionality with improved error handling and backward compatibility.
- Introduced a new button in the UI for accessing the bug report modal.
2026-05-06 07:01:43 +02:00

27 lines
904 B
SQL

-- Migration 185: Prevent duplicate active location-contact relations
-- Date: 2026-05-05
-- Purpose: Ensure one active relation per (location_id, related_contact_id)
-- Soft-delete duplicate active rows, keep best candidate (primary first, then oldest)
WITH ranked AS (
SELECT
id,
ROW_NUMBER() OVER (
PARTITION BY location_id, related_contact_id
ORDER BY is_primary DESC, created_at ASC, id ASC
) AS rn
FROM locations_contacts
WHERE deleted_at IS NULL
AND related_contact_id IS NOT NULL
)
UPDATE locations_contacts lc
SET deleted_at = NOW()
FROM ranked r
WHERE lc.id = r.id
AND r.rn > 1;
-- Enforce uniqueness for active linked contacts
CREATE UNIQUE INDEX IF NOT EXISTS uq_locations_contacts_location_related_active
ON locations_contacts(location_id, related_contact_id)
WHERE deleted_at IS NULL AND related_contact_id IS NOT NULL;