Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 29, 2025

This PR adds a comprehensive solution for consolidating duplicate labels in the system while preserving all track, show, and series associations.

Problem

The system can accumulate duplicate labels (same name within the same radio) due to:

  • Data imports from external sources
  • Case-sensitivity issues before the downcase_name callback
  • Manual data entry inconsistencies

When duplicate labels exist, track associations are split across multiple label records, making data management difficult and potentially causing issues with label-based queries.

Solution

Main Script: script/consolidate_duplicate_labels.rb

Safely consolidates duplicate labels by:

  1. Finding labels with identical names within each radio scope
  2. Keeping the oldest label (by created_at timestamp)
  3. Moving all associations from duplicate labels to the kept label:
    • TrackLabel relationships (track ↔ label associations)
    • ScheduledShowLabel relationships (show ↔ label associations)
    • ShowSeriesLabel relationships (series ↔ label associations)
  4. Deleting the duplicate labels

Safety Features:

  • Transaction-wrapped operations with automatic rollback on errors
  • Duplicate association detection to prevent constraint violations
  • Dry-run mode (DRY_RUN=true) for testing before execution
  • Detailed logging and progress tracking

Helper Script: script/check_duplicate_labels.rb

Read-only script to identify duplicate labels and show association counts before consolidation.

Model Enhancement

Added missing show_series_labels and show_series associations to the Label model to ensure complete relationship handling.

Usage

# Check for duplicates first
rails runner script/check_duplicate_labels.rb

# Test consolidation (no changes made)
DRY_RUN=true rails runner script/consolidate_duplicate_labels.rb

# Perform actual consolidation
rails runner script/consolidate_duplicate_labels.rb

Testing

Comprehensive test suite covers:

  • Basic duplicate consolidation with association preservation
  • Dry-run mode functionality
  • Cross-radio label handling (duplicates in different radios are preserved)
  • Edge cases like no duplicates found

The solution ensures that all track label data is preserved during consolidation, addressing the core requirement that "if a track belongs to a duplicate label it should be added to the label we will keep to preserve all track label data."


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 29, 2025 17:00
Co-authored-by: mcfiredrill <66243+mcfiredrill@users.noreply.github.com>
Co-authored-by: mcfiredrill <66243+mcfiredrill@users.noreply.github.com>
Copilot AI changed the title [WIP] add a script to consolidate duplicate labels if a track belongs to a duplicate label it should be added to the label we will keep to preserve all track label data Add script to consolidate duplicate labels and preserve track associations Aug 29, 2025
Copilot AI requested a review from mcfiredrill August 29, 2025 17:03
@mcfiredrill mcfiredrill marked this pull request as ready for review September 9, 2025 02:44
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