Maintenance::BackfillMboProfileActiveOnTreeNodeTask

Source code
# frozen_string_literal: true

# Backfills active on TreeNodeToMboProfile join records.
#
# After the active column was added (default: true), all existing joins start active.
# This task propagates the legacy profile-level state to the joins by deactivating the
# joins of profiles that are currently inactive, so per-treenode state matches the
# behaviour in place before the column was introduced.
#
# Primary joins are intentionally left active: TreeNodeToMboProfile#active_must_be_true_if_primary
# forbids a primary association from being inactive, so deactivating them would create rows that
# no subsequent save could persist. The legacy global "inactive" state predates that per-treenode
# invariant; on the primary tree node the association stays active (and usable), while every
# non-primary association is deactivated.
#
# One-shot transition backfill: run ONCE immediately after the deploy that adds the active column,
# before normal per-tree-node activity begins. It reads the legacy mbo_profiles.active column, which
# mutations no longer write and which is therefore only trustworthy during that initial window. Re-running
# it later is idempotent against untouched data (only inactive profiles' still-active, non-primary joins
# are deactivated), but do NOT re-run it after go-live: a profile that was legacy-inactive but has since
# been reactivated on a node via updateTreeNode would have that fresh change undone, since the frozen
# legacy column no longer reflects the join-level source of truth.
class Maintenance::BackfillMboProfileActiveOnTreeNodeTask < MaintenanceTasks::Task
  collection_batch_size(1_000)

  def collection
    MboProfile.where(active: false)
  end

  def process(mbo_profile)
    TreeNodeToMboProfile.where(mbo_profile_id: mbo_profile.id, active: true, primary: false)
                        .update_all(active: false) # rubocop:disable Rails/SkipsModelValidations
  end
end

Previous Runs

Succeeded
#92

Processed 2 out of 2 items (100%).

Ran for less than 5 seconds, finished .

Metadata:
user_id
-1