Fix FK violation when deleting projects with AI catalog items

What does this MR do and why?

Contributes to #574259

Problem

When deleting a project that owns AI catalog items with consumers, a foreign key constraint violation occurred:

  ERROR: update or delete on table "ai_catalog_items" violates
  foreign key constraint "fk_bba1649fa5" on table
  "ai_catalog_item_consumers" (PG::ForeignKeyViolation)

The issue occurs because:

  • The project_id FK on ai_catalog_item_consumers has ON DELETE CASCADE
  • The ai_catalog_item_id FK has ON DELETE RESTRICT
  • When project deletion cascades to delete consumers, it violates the restrict constraint on the catalog item

This blocks deletion of projects that have published public catalog items used by other projects.

Solution

Handle AI catalog data cleanup in Projects::DestroyService:

  1. Explicitly delete all item consumers where the project is the consumer BEFORE the project deletion cascade begins

  2. For catalog items owned by the project:

    • If the item has consumers from other projects: soft delete the item and unset the project association (making it organization-owned only) to preserve functionality for other consumers
    • If the item only has consumers from the same project or no consumers: delete the item normally

This allows projects to be deleted without breaking other customers' workflows that depend on public catalog items.

References

Bug: Enabled AI Catalog items blocks Project de... (#574259)

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #574259

Edited by Vasilii Iakliushin

Merge request reports

Loading