Fix missing papertrail versions in GitlabSubscriptions::Duo::BulkAssignService when using upsert_all
Summary
The GitlabSubscriptions::Duo::BulkAssignService uses upsert_all to create addon assignments, but this bypasses ActiveRecord callbacks which are required by papertrail to create version records. This results in orphaned addon assignments without corresponding subscription_user_add_on_assignment_versions records, causing data inconsistencies in ClickHouse historical data.
Problem
As discovered in #498633 (comment 2644308842), we currently have ~9000 orphan addon assignment records without papertrail versions because:
-
GitlabSubscriptions::Duo::BulkAssignServiceusesupsert_allwhich bypasses ActiveRecord callbacks - Papertrail requires ActiveRecord callbacks to create version records
- This leads to missing historical data in ClickHouse for addon assignments
Root Cause
# In GitlabSubscriptions::Duo::BulkAssignService
SubscriptionUserAddOnAssignment.upsert_all(assignments_data, unique_by: [:user_id, :add_on_id])
The upsert_all method doesn't trigger ActiveRecord callbacks, so papertrail doesn't create the corresponding version records.
Proposal
Update GitlabSubscriptions::Duo::BulkAssignService to manually create papertrail version records when using upsert_all. This follows the pattern established in previous fixes:
Implementation Details
- After the
upsert_alloperation, manually create papertrail version records for the inserted/updated assignments - Ensure proper
eventtype is set ('create'for new records,'update'for existing ones) - Preserve correct timestamps and metadata
- Consider using
PaperTrail::Version.create!or similar approach
Acceptance Criteria
-
GitlabSubscriptions::Duo::BulkAssignServicecreates papertrail versions manually afterupsert_all -
New addon assignments created via bulk assign service have corresponding version records -
Historical data is properly synced to ClickHouse -
No new orphaned addon assignments are created -
Tests verify that version records are created correctly
Related Issues
- Parent issue: #498633 (closed)
- Data migration for existing orphans: #552992 (closed)
Additional Context
This is a critical fix needed to ensure data consistency for the historical addon assignment finder functionality. Without proper papertrail versions, the ClickHouse historical data will be incomplete, affecting AI Impact analytics and metrics.