Add starring support for AI catalog items (part 1 : schema)

What does this MR do and why?

Introduces the backend foundation for starring AI catalog items. This is the first in a series of MRs implementing quality signals for the AI catalog, as discussed in gitlab-org#20225 (comment 3112262413).

This MR adds:

  • ai_catalog_item_stars join table storing (user_id, ai_catalog_item_id) pairs with a unique constraint
  • star_count integer column on ai_catalog_items (default 0, not null, with a CHECK (star_count >= 0) constraint)
  • Ai::Catalog::ItemStar model with after_create/after_destroy callbacks that update star_count in real time, mirroring the pattern used by UsersStarProject. Inactive users are excluded from count updates.
  • Item#star(user), Item#unstar(user), and Item#starred_by?(user) as the primary interface

No API surface is included. A follow-up MR will add a GraphQL query field and mutation to expose starring to the frontend (WIP MR: !228667 (merged)).

Database queries

Ai::Catalog::ItemStar — star count update

Called from after_create/after_destroy callbacks on Ai::Catalog::ItemStar when a star is created or destroyed.

Query (increment):

UPDATE ai_catalog_items SET star_count = star_count + 1 WHERE id = $1

Query (decrement):

UPDATE ai_catalog_items SET star_count = star_count - 1 WHERE id = $1

Execution plan:

Update on ai_catalog_items
  ->  Index Scan using ai_catalog_items_pkey on ai_catalog_items
        Index Cond: (id = $1)

Single-row PK lookup. No full table scans.

This pattern mirrors UsersStarProject, which uses the same +1/-1 approach via update_counters. A periodic recalculation worker will be added in a follow-up to handle drift from cascade deletes (e.g. when a user is deleted).

References

Screenshots or screen recordings

No UI changes.

How to set up and validate locally

  1. Run migrations: bin/rails db:migrate

  2. In the Rails console:

    item = Ai::Catalog::Item.first
    user = User.first
    item.star(user)          # creates star, increments star_count
    item.reload.star_count   # => 1
    item.starred_by?(user)   # => true
    item.unstar(user)        # destroys star, decrements star_count
    item.reload.star_count   # => 0

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.

Edited by Mattias Michaux

Merge request reports

Loading
Loading