Migration to fix drifted project namespace parent_id and traversal_ids
Summary
After investigating #614002 (closed), we found that the parent_id drift on project namespaces is not isolated to a single project. A scan of the production database revealed multiple projects where namespaces.parent_id on the project namespace does not match projects.namespace_id.
These project namespaces have stale parent_id values (pointing at a previous parent), and their traversal_ids are internally consistent with the wrong parent_id. This means:
- Work item list views are broken for affected projects (the
NamespaceResolverfull path lookup fails silently) - The
WorkItems::TraversalIdsHealingCronWorkercannot fix this because it trustsnamespaces.traversal_idsas the source of truth
Detection query
The affected rows can be identified with:
SELECT p.id AS project_id,
p.namespace_id AS expected_parent,
n.id AS project_namespace_id,
n.parent_id AS actual_parent
FROM projects p
JOIN namespaces n ON n.id = p.project_namespace_id
WHERE n.parent_id IS DISTINCT FROM p.namespace_id;Proposed fix
Create a batched background migration that:
- Finds all project namespaces where
parent_id != projects.namespace_id - Updates
namespaces.parent_idto matchprojects.namespace_id - Recomputes
traversal_idsfor the affected namespaces (this should happen automatically via theafter_update :sync_traversal_idscallback whenparent_idchanges, but the migration should verify)
Considerations
- The
Namespace::TraversalHierarchy.sync_traversal_ids!method acquires locks on the root of the hierarchy. The migration should handle lock timeouts gracefully. - Project namespaces have no children, so
traversal_idsrecomputation is scoped to the single row. - The migration should be idempotent and safe to re-run.
References
- Root cause issue: #614002 (closed)
ensure_project_namespace_in_syncgap: #616993 (closed)
Edited by 🤖 GitLab Bot 🤖