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_starsjoin table storing(user_id, ai_catalog_item_id)pairs with a unique constraintstar_countinteger column onai_catalog_items(default 0, not null, with aCHECK (star_count >= 0)constraint)Ai::Catalog::ItemStarmodel withafter_create/after_destroycallbacks that updatestar_countin real time, mirroring the pattern used byUsersStarProject. Inactive users are excluded from count updates.Item#star(user),Item#unstar(user), andItem#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 = $1Query (decrement):
UPDATE ai_catalog_items SET star_count = star_count - 1 WHERE id = $1Execution 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
- Design discussion: gitlab-org#20225 (comment 3112262413)
- Follow-up (GraphQL query + mutation): TBD
- Pattern reference:
app/models/users_star_project.rb
Screenshots or screen recordings
No UI changes.
How to set up and validate locally
-
Run migrations:
bin/rails db:migrate -
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.