Create Ai::ActiveContext::Code tables

What does this MR do and why?

Adds two tables for ActiveContext: Ai::ActiveContext::Code::EnabledNamspace and Ai::ActiveContext::Code::Repository.

enabled_namespaces is a reference to namespace and exists only for namespaces that have Duo licenses. An enabled_namespace has many repositories. A repository represents (and references) a project that has to be indexed.

These records belong to a connection. If a new connection is created, the old enabled_namespaces and repositories records will be cleaned up.

Both tables are partitioned:

  • enabled_namespaces is partitioned by namespace_id which will be a root namespace id
  • repositories is partitioned by project_id which is non-nullable

The following references exist:

Screenshot_2025-06-12_at_13.42.52

  • enabled_namespaces is on gitlab_main_cell and sharded by namespaces
  • repositories is on gitlab_main_cell_local
  • connection (existing table) is on gitlab_main_cell_local

Schema

EnabledNamespace

Partitioned table "public.p_ai_active_context_code_enabled_namespaces"
    Column     |           Type           | Collation | Nullable |                                 Default
---------------+--------------------------+-----------+----------+-------------------------------------------------------------------------
 id            | bigint                   |           | not null | nextval('p_ai_active_context_code_enabled_namespaces_id_seq'::regclass)
 namespace_id  | bigint                   |           | not null |
 connection_id | bigint                   |           | not null |
 metadata      | jsonb                    |           | not null | '{}'::jsonb
 state         | smallint                 |           | not null | 0
 created_at    | timestamp with time zone |           | not null |
 updated_at    | timestamp with time zone |           | not null |
Partition key: RANGE (namespace_id)
Indexes:
    "p_ai_active_context_code_enabled_namespaces_pkey" PRIMARY KEY, btree (id, namespace_id)
    "idx_ai_active_context_code_enabled_namespaces_namespace_id" btree (namespace_id)
    "idx_unique_ai_code_repository_connection_namespace_id" UNIQUE, btree (connection_id, namespace_id)
Foreign-key constraints:
    "fk_rails_42b1b86224" FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE
Triggers:
    p_ai_active_context_code_enabled_namespaces_loose_fk_trigger AFTER DELETE ON p_ai_active_context_code_enabled_namespaces REFERENCING OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE FUNCTION insert_into_loose_foreign_keys_deleted_records()
Number of partitions: 7 (Use \d+ to list them.)

Repository

Partitioned table "public.p_ai_active_context_code_repositories"
        Column        |           Type           | Collation | Nullable |                              Default
----------------------+--------------------------+-----------+----------+-------------------------------------------------------------------
 id                   | bigint                   |           | not null | nextval('p_ai_active_context_code_repositories_id_seq'::regclass)
 project_id           | bigint                   |           | not null |
 connection_id        | bigint                   |           |          |
 enabled_namespace_id | bigint                   |           |          |
 metadata             | jsonb                    |           | not null | '{}'::jsonb
 last_commit          | text                     |           |          |
 state                | smallint                 |           | not null | 0
 indexed_at           | timestamp with time zone |           |          |
 created_at           | timestamp with time zone |           | not null |
 updated_at           | timestamp with time zone |           | not null |
Partition key: RANGE (project_id)
Indexes:
    "p_ai_active_context_code_repositories_pkey" PRIMARY KEY, btree (id, project_id)
    "idx_ai_code_repository_project_id_state" btree (project_id, state)
    "idx_p_ai_active_context_code_repositories_enabled_namespace_id" btree (enabled_namespace_id)
    "idx_unique_ai_code_repository_connection_project_id" UNIQUE, btree (connection_id, project_id)
Check constraints:
    "check_b253d453c7" CHECK (char_length(last_commit) <= 64)
Foreign-key constraints:
    "fk_rails_b3d72d06cf" FOREIGN KEY (connection_id) REFERENCES ai_active_context_connections(id) ON DELETE SET NULL
Number of partitions: 7 (Use \d+ to list them.)

References

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

Edited by Madelein van Niekerk

Merge request reports

Loading