Skip to content

Schedule the creation of a unique partial index on lfs_objects_projects

What does this MR do and why?

We want to have database constraints that enforce this model validation: https://gitlab.com/gitlab-org/gitlab/-/blob/162ff7cea82fa314f6941bbec917a745247caf5c/app/models/lfs_objects_project.rb#L11

class LfsObjectsProject < ApplicationRecord
# ...
  validates :lfs_object_id, uniqueness: { scope: [:project_id, :repository_type], message: "already exists in repository" }
# ...
end

In a previous MR a migration to schedule an index like:

CREATE UNIQUE INDEX CONCURRENTLY "idx_lfs_objects_projects_on_project_id_lfs_object_id_repo_type" ON "lfs_objects_projects" ("project_id", "lfs_object_id", "repository_type")

This MR schedules the creation of a similar unique index that will handle cases where repository_type is null:

CREATE UNIQUE INDEX CONCURRENTLY "lfs_objects_projects_on_project_id_lfs_object_id_null_repo_type" ON "lfs_objects_projects" ("project_id", "lfs_object_id") WHERE repository_type IS NULL

References

Please include cross links to any resources that are relevant to this MR. This will give reviewers and future readers helpful context to give an efficient review of the changes introduced.

MR acceptance checklist

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

How to set up and validate locally

Try something similar to the steps described in: https://docs.gitlab.com/ee/development/database/adding_database_indexes.html#verify-indexes-created-asynchronously

For example

  1. Enable async index operations (Feature.enable(:database_async_index_operations), Feature.enable(:database_async_index_creation) and Feature.enable(:database_reindexing))
  2. Run migrations bundle exec rails db:migrate
  3. Check that async operation has been scheduled in the postgres_async_indexes table (SELECT definition from postgres_async_indexes)
  4. Rollback the migration and check that the async operation is no long scheduled
  5. Run the migration again and execute the async operation (bundle exec rails gitlab:db:execute_async_index_operations:all)
  6. Check that the unique index has been added to lfs_objects_projects

Related to #467119 (closed)

Edited by Jerry Seto

Merge request reports

Loading