Denormalize latest_version

What does this MR do and why?

The latestVersion field is suboptimal in that the preloading loads all versions for an item in order to select 1 !197081 (comment 2616258254).

This MR denormalises latest_version for items, so we select it more performantly.

We'll be able to make wide use of latest_released_version for other purposes besides just exposing in GraphQL. The utility of this can be more than an optimisation for GraphQL #558282.

How to set up and validate locally

Enable the global_ai_catalog feature flag:

Feature.enable(:global_ai_catalog)

You may need to delete any existing AI Catalog data if you have created some locally to remove any existing records that do not have a .latest_version record set, which is now expected within this MR.

There is no data in these tables on staging and production, and the WIP feature flag remains disabled for staging and production.

Ai::Catalog::ItemConsumer.destroy_all
Ai::Catalog::Item.destroy_all

Create some AI Catalog data, execute a 3 of the following mutations in GraphiQL Explorer http://gdk.test:3000/-/graphql-explorer:

 mutation {
   aiCatalogAgentCreate(input: {
     name: "name"
     description: "A pirate doing a sum"
     projectId: "gid://gitlab/Project/1000000"
     public: true
     systemPrompt: "System prompt"
     userPrompt: "User prompt"
   }) {
    errors
    item {
      id
    }
  }
}

Tail your development log:

tail -f log/development.log

Query for the data:

{
  aiCatalogItems {
    nodes {
      id
      latestVersion {
        id
      }
    }
  }
}

On this branch, your logs will show 2 queries are executed, with ai_catalog_item_versions being batch loaded:

SELECT "ai_catalog_items".* FROM "ai_catalog_items" WHERE "ai_catalog_items"."deleted_at" IS NULL AND "ai_catalog_items"."organization_id" = 1 ORDER BY "ai_catalog_items"."id" DESC LIMIT 101 

SELECT "ai_catalog_item_versions".* FROM "ai_catalog_item_versions" WHERE "ai_catalog_item_versions"."id" IN (48, 47, 46) 

On master, your logs will also show 2 queries, but instead of selecting specific ai_catalog_item_versions. records by their ID (WHERE ai_catalog_item_versions.id), they are selected by the associated Item (WHERE ai_catalog_item_versions.ai_catalog_item_id) and so all of the item's versions are being loaded.

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 #554673 (closed)

Edited by Luke Duncalfe

Merge request reports

Loading