Backfill converted work item status rows to custom_status_id

What does this MR do and why?

Filtering work items by status in the gitlab-org group times out for five statuses: Start, Won't do, New, Complete, Duplicate. These statuses predate the group's conversion to a custom lifecycle.

A work item's status lives in work_item_current_statuses, as either custom_status_id (indexed) or a legacy system_defined_status_id (no usable index). Rows keep the legacy form when they were set before the namespace converted, because conversion created new status definitions but never rewrote existing rows. Filtering by a converted status has to OR both columns, and the planner scans the whole group.

This MR adds a batched background migration, BackfillConvertedWorkItemStatusIds, over work_item_current_statuses (batch column id, default batch size, sub-batch size 100, gitlab_main_org). For each sub-batch, it runs one UPDATE ... FROM namespaces, work_item_custom_statuses: rows with a non-null system_defined_status_id whose root namespace (namespaces.traversal_ids[1]) has a custom status with a matching converted_from_system_defined_status_identifier get custom_status_id set and system_defined_status_id nulled. updated_at is left untouched on purpose, since time-bound status mappings compare against it. Rows in namespaces that never converted are not matched and are left alone — the legacy form is correct and permanent there. The job is idempotent. Statuses are EE-only, so the CE migration class is an empty stub prepended by the EE module.

This MR also tightens the check constraint on work_item_current_statuses from num_nonnulls(custom_status_id, system_defined_status_id) > 0 to = 1, added NOT VALID in a regular migration and validated in a post-deployment migration, so it is in place before the backfill enqueues. Nothing writes both columns (the model setter nulls the other one, and this backfill sets custom and nulls system-defined in the same UPDATE); production has no rows with both set. The old > 0 constraint is dropped in the same post-deployment migration, since "exactly one" implies "at least one". #629284 then only needs to drop the system-defined column.

The companion change — making WorkItems::Lifecycles::BaseService rewrite rows at conversion time, so new legacy rows stop appearing — is a separate follow-up MR, to keep this one a pure migration for database review.

After this backfill, filtering by Start and Won't do needs only the indexed column. It does not fix New, Complete, Duplicate by itself — those also need rows created for items that have none, tracked as a second migration in the epic. Removing the OR branch from the query is a later, flag-gated MR once both migrations are finalised.

Query plans

one sub-batch (100 rows) sizing
Plan sessions/57771/commands/162392 sessions/57771/commands/162393

References

Screenshots or screen recordings

Not applicable. This is a database migration with no UI change.

How to set up and validate locally

  1. Run the specs: spec/migrations/20260916090154_queue_backfill_converted_work_item_status_ids_spec.rb and ee/spec/lib/ee/gitlab/background_migration/backfill_converted_work_item_status_ids_spec.rb.
  2. In rails console, create a group with a converted custom status:
    WorkItems::Statuses::Custom::Status.create!(namespace:, name:, color:, category:, converted_from_system_defined_status_identifier: 1)
    and a WorkItems::Statuses::CurrentStatus row with system_defined_status_id: 1 for an issue in that group.
  3. Run the job for that id range:
    Gitlab::BackgroundMigration::BackfillConvertedWorkItemStatusIds.new(
      start_cursor: [id], end_cursor: [id], batch_table: :work_item_current_statuses,
      batch_column: :id, sub_batch_size: 100, pause_ms: 0, connection: ApplicationRecord.connection
    ).perform
  4. Confirm the row now has custom_status_id set, system_defined_status_id nil, and updated_at unchanged.
Edited by Vedant Jain

Merge request reports

Loading
Loading