Skip to content

Conversation

@ulemons
Copy link
Contributor

@ulemons ulemons commented Jan 29, 2026

Remove Foreign Key Constraints and Add Orphan Cleanup Tracking

Overview

This migration removes foreign key constraints from aggregation tables and introduces a tracking mechanism for orphan cleanup operations.

Changes

Foreign Key Removal

  • memberSegmentsAgg: Removed foreign key constraint memberSegmentsAgg_memberId_fkey
  • organizationSegmentsAgg: Removed foreign key constraint organizationSegmentsAgg_organizationId_fkey

Rationale: Removing these constraints allows for more flexible data management and prevents cascade deletion issues while maintaining data integrity through application-level controls.

New Table: orphanCleanupRuns

Introduces a new tracking table to monitor and audit orphan record cleanup operations.

Fields:

  • id (UUID): Primary key
  • tableName (VARCHAR): Target table name
  • startedAt / completedAt (TIMESTAMPTZ): Execution timestamps
  • status (VARCHAR): Operation status ('running', 'completed', 'failed')
  • orphansFound / orphansDeleted (INTEGER): Cleanup metrics
  • executionTimeMs (INTEGER): Performance tracking
  • errorMessage (TEXT): Error details if failed

Note

Medium Risk
Schema changes remove referential integrity enforcement on aggregate tables and introduce a new tracking table; risk is primarily around allowing orphaned aggregate rows and requiring application/job logic to handle cleanup correctly.

Overview
Database migration drops the foreign key constraints on memberSegmentsAgg.memberId and organizationSegmentsAgg.organizationId, decoupling these aggregate tables from their parent records.

It also adds a new orphanCleanupRuns table to record/audit orphan cleanup jobs (status, timing, counts, and error message).

Written by Cursor Bugbot for commit 668c2a6. This will update automatically on new commits. Configure here.

@ulemons ulemons self-assigned this Jan 29, 2026
@ulemons ulemons changed the title feat: add migration feat: add migration (CM-904) Jan 30, 2026
@ulemons ulemons requested a review from skwowet January 30, 2026 13:01
@ulemons ulemons marked this pull request as ready for review January 30, 2026 13:01
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