Skip to content
Snippets Groups Projects

Validate uniqnuess of member role name

Merged Jarka Košanová requested to merge 438472-validate-cr-name-uniqness into master
1 unresolved thread

What does this MR do and why?

It adds a requirement that a name is unique per namespace:

  • it adds a validation to the model
  • it removes existing duplicates by changing the name of duplicates
  • it adds unique index to the member_roles table

Database queries

  • duplicates removal
  • we have around 10 member role records on Gitlab.com and we expect maximum of hundreds of records on self-managed instances (more likely tons)

MR acceptance checklist

Screenshots or screen recordings

How to set up and validate locally

  1. Before checking out this branch, create few duplicates member roles (duplicated = same name and namespace), eg. in console:
mr1 = MemberRole.create(name: 'member role', base_access_level: 10, read_code: true)
mr2 = MemberRole.create(name: 'member role', base_access_level: 10, read_code: true)
mr3 = MemberRole.create(name: 'member role', base_access_level: 10, read_code: true)
  1. Check out this branch
  2. Run the migration
  3. Check if the duplicated names were updated, eg. by checking in the console: MemberRole.all.map(&:name)
  4. Try to create the duplicated records again, this should not work

Related to #438472 (closed)

Edited by Jarka Košanová

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Contributor
    2 Warnings
    :warning: This MR changes code in ee/, but its Changelog commit is missing the EE: true trailer. Consider adding it to your Changelog commits.
    :warning:

    featureaddition and featureenhancement merge requests normally have a documentation change. Consider adding a documentation update or confirming the documentation plan with the Technical Writer counterpart.

    For more information, see:

    Reviewer roulette

    Category Reviewer Maintainer
    backend @mc_rocha profile link current availability (UTC-5, 6 hours behind author) @alejandro profile link current availability (UTC-5, 6 hours behind author)
    database @huzaifaiftikhar1 profile link current availability (UTC+5.5, 4.5 hours ahead of author) @dfrazao-gitlab profile link current availability (UTC+1, same timezone as author)
    ~"Authorization" Reviewer review is optional for ~"Authorization" @mokhax profile link current availability (UTC-7, 8 hours behind author)

    Please check reviewer's status!

    • available Reviewer is available!
    • unavailable Reviewer is unavailable!

    Please refer to documentation page for guidance on how you can benefit from the Reviewer Roulette, or use the GitLab Review Workload Dashboard to find other available reviewers.

    If needed, you can retry the :repeat: danger-review job that generated this comment.

    Generated by :no_entry_sign: Danger

  • Jarka Košanová added 1618 commits

    added 1618 commits

    Compare with previous version

  • added 1 commit

    • f9a4de0d - Validate uniqnuess of member role name

    Compare with previous version

  • added 1 commit

    • a472e621 - Validate uniqnuess of member role name

    Compare with previous version

  • A deleted user added Data WarehouseImpact Check label
  • added 1 commit

    • 0e130079 - Validate uniqnuess of member role name

    Compare with previous version

  • Contributor

    Database migrations (on the main database)

    Migrations included in this change have been executed on gitlab.com data for testing purposes. For details, please see the migration testing pipeline (limited access).

    Migration Type Total runtime Result DB size change
    20240210104125 - EnsureMemberRolesNamesUniq Post deploy 2.0 s :white_check_mark: +16.00 KiB
    20240221134504 - AddNameUniqueIndexToMemberRoles Post deploy 2.4 s :white_check_mark: +16.00 KiB
    Runtime Histogram for all migrations
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 6
    0.1 seconds - 1 second 1
    1 second - 5 seconds 0
    5 seconds - 15 seconds 0
    15 seconds - 5 minutes 0
    5 minutes + 0

    Migration: 20240210104125 - EnsureMemberRolesNamesUniq

    • Type: Post deploy
    • Duration: 2.0 s
    • Database size change: +16.00 KiB
    Calls Total Time Max Time Mean Time Rows Query
    1 94.1 ms 94.1 ms 94.1 ms 58
    UPDATE member_roles
    SET name = CONCAT(name, $1, id, $2)
    WHERE id IN (
    SELECT mr.id
    FROM member_roles mr
    WHERE EXISTS (
    SELECT mr_duplicates.id
    FROM member_roles mr_duplicates
    WHERE mr_duplicates.name = mr.name AND ( mr_duplicates.namespace_id = mr.namespace_id OR (mr_duplicates.namespace_id IS NULL AND mr.namespace_id IS NULL) ) AND mr_duplicates.id < mr.id
    )
    )
    2 0.0 ms 0.0 ms 0.0 ms 2
    SELECT pg_backend_pid()
    Histogram for EnsureMemberRolesNamesUniq
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 2
    0.1 seconds - 1 second 1
    1 second - 5 seconds 0
    5 seconds - 15 seconds 0
    15 seconds - 5 minutes 0
    5 minutes + 0

    Migration: 20240221134504 - AddNameUniqueIndexToMemberRoles

    • Type: Post deploy
    • Duration: 2.4 s
    • Database size change: +16.00 KiB
    Calls Total Time Max Time Mean Time Rows Query
    1 5.1 ms 5.1 ms 5.1 ms 0
    CREATE UNIQUE INDEX CONCURRENTLY "index_member_roles_on_namespace_id_name_unique" ON "member_roles" ("namespace_id", "name")
    1 2.9 ms 2.9 ms 2.9 ms 0
    DROP INDEX CONCURRENTLY "index_member_roles_on_namespace_id"
    2 0.0 ms 0.0 ms 0.0 ms 2
    SELECT pg_backend_pid()
    Histogram for AddNameUniqueIndexToMemberRoles
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 4
    0.1 seconds - 1 second 0
    1 second - 5 seconds 0
    5 seconds - 15 seconds 0
    15 seconds - 5 minutes 0
    5 minutes + 0

    Other information

    Other migrations pending on GitLab.com
    Migration Type Total runtime Result DB size change
    20240201111294 - AddIndexOnIssuesTableTmpEpicIdColumn Post deploy 2074.0 s :warning: +1.87 GiB
    20240201112204 - AddFkToTmpEpicIdColumnOnIssues Post deploy 3.6 s :warning: +0.00 B
    20240219135417 - ReplaceOldFkCiBuildTraceMetadataToCiJobArtifacts Post deploy 2.8 s :warning: +0.00 B
    20240219142421 - ReplaceOldFkCiJobArtifactStatesToCiJobArtifacts Post deploy 2.2 s :white_check_mark: +0.00 B
    20240222000002 - FinalizeBackfillVsCodeSettingsUuid Post deploy 1.9 s :white_check_mark: +0.00 B
    Clone details
    Clone ID Clone Created At Clone Data Timestamp Expected Removal Time
    database-testing-2898253-12938221-main 2024-02-22T19:01:11Z 2024-02-21T19:41:26Z 2024-02-23 07:42:38 +0000
    database-testing-2898253-12938221-ci 2024-02-22T19:01:11Z 2024-02-22T16:46:05Z 2024-02-23 07:42:38 +0000

    Job artifacts

    Database migrations (on the ci database)

    Migrations included in this change have been executed on gitlab.com data for testing purposes. For details, please see the migration testing pipeline (limited access).

    Migration Type Total runtime Result DB size change
    20240210104125 - EnsureMemberRolesNamesUniq Post deploy 3.0 s :white_check_mark: +0.00 B
    20240221134504 - AddNameUniqueIndexToMemberRoles Post deploy 3.8 s :white_check_mark: +0.00 B
    Runtime Histogram for all migrations
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 5
    0.1 seconds - 1 second 1
    1 second - 5 seconds 0
    5 seconds - 15 seconds 0
    15 seconds - 5 minutes 0
    5 minutes + 0

    Migration: 20240210104125 - EnsureMemberRolesNamesUniq

    • Type: Post deploy
    • Duration: 3.0 s
    • Database size change: +0.00 B
    Calls Total Time Max Time Mean Time Rows Query
    2 0.0 ms 0.0 ms 0.0 ms 2
    SELECT pg_backend_pid()
    Histogram for EnsureMemberRolesNamesUniq
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 2
    0.1 seconds - 1 second 0
    1 second - 5 seconds 0
    5 seconds - 15 seconds 0
    15 seconds - 5 minutes 0
    5 minutes + 0

    Migration: 20240221134504 - AddNameUniqueIndexToMemberRoles

    • Type: Post deploy
    • Duration: 3.8 s
    • Database size change: +0.00 B
    Calls Total Time Max Time Mean Time Rows Query
    1 60.6 ms 60.6 ms 60.6 ms 0
    CREATE UNIQUE INDEX CONCURRENTLY "index_member_roles_on_namespace_id_name_unique" ON "member_roles" ("namespace_id", "name")
    1 41.0 ms 41.0 ms 41.0 ms 0
    DROP INDEX CONCURRENTLY "index_member_roles_on_namespace_id"
    2 0.0 ms 0.0 ms 0.0 ms 2
    SELECT pg_backend_pid()
    Histogram for AddNameUniqueIndexToMemberRoles
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 3
    0.1 seconds - 1 second 1
    1 second - 5 seconds 0
    5 seconds - 15 seconds 0
    15 seconds - 5 minutes 0
    5 minutes + 0

    Other information

    No other migrations pending on GitLab.com

    Clone details
    Clone ID Clone Created At Clone Data Timestamp Expected Removal Time
    database-testing-2898253-12938221-main 2024-02-22T19:01:11Z 2024-02-21T19:41:26Z 2024-02-23 07:42:38 +0000
    database-testing-2898253-12938221-ci 2024-02-22T19:01:11Z 2024-02-22T16:46:05Z 2024-02-23 07:42:38 +0000

    Job artifacts


    Brought to you by gitlab-org/database-team/gitlab-com-database-testing. Epic

  • Jarka Košanová changed the description

    changed the description

  • Jarka Košanová requested review from @mc_rocha and @ck3g

    requested review from @mc_rocha and @ck3g

  • Marcos Rocha approved this merge request

    approved this merge request

  • Marcos Rocha requested review from @rkadam3 and removed review request for @mc_rocha

    requested review from @rkadam3 and removed review request for @mc_rocha

  • Contributor

    E2E Test Result Summary

    allure-report-publisher generated test report!

    e2e-test-on-gdk: :white_check_mark: test report for 48629cb6

    expand test summary
    +------------------------------------------------------------------+
    |                          suites summary                          |
    +-------------+--------+--------+---------+-------+-------+--------+
    |             | passed | failed | skipped | flaky | total | result |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Govern      | 66     | 0      | 0       | 0     | 66    | ✅     |
    | Monitor     | 4      | 0      | 0       | 0     | 4     | ✅     |
    | Create      | 8      | 0      | 3       | 0     | 11    | ✅     |
    | Plan        | 4      | 0      | 0       | 0     | 4     | ✅     |
    | Data Stores | 2      | 0      | 0       | 0     | 2     | ✅     |
    | Package     | 0      | 0      | 1       | 0     | 1     | ➖     |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Total       | 84     | 0      | 4       | 0     | 88    | ✅     |
    +-------------+--------+--------+---------+-------+-------+--------+

    e2e-package-and-test: :white_check_mark: test report for 48629cb6

    expand test summary
    +------------------------------------------------------------------+
    |                          suites summary                          |
    +-------------+--------+--------+---------+-------+-------+--------+
    |             | passed | failed | skipped | flaky | total | result |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Govern      | 272    | 0      | 19      | 0     | 291   | ✅     |
    | Create      | 153    | 0      | 19      | 0     | 172   | ✅     |
    | Monitor     | 8      | 0      | 0       | 0     | 8     | ✅     |
    | Package     | 0      | 0      | 2       | 0     | 2     | ➖     |
    | Plan        | 8      | 0      | 0       | 0     | 8     | ✅     |
    | Data Stores | 4      | 0      | 0       | 0     | 4     | ✅     |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Total       | 445    | 0      | 40      | 0     | 485   | ✅     |
    +-------------+--------+--------+---------+-------+-------+--------+
  • Rajendra Kadam approved this merge request

    approved this merge request

  • Rajendra Kadam removed review request for @rkadam3

    removed review request for @rkadam3

  • Jarka Košanová requested review from @ck3g

    requested review from @ck3g

  • Vitali Tatarintev approved this merge request

    approved this merge request

  • Vitali Tatarintev requested review from @DylanGriffith and removed review request for @ck3g

    requested review from @DylanGriffith and removed review request for @ck3g

  • Dylan Griffith
  • Dylan Griffith
  • Dylan Griffith removed review request for @DylanGriffith

    removed review request for @DylanGriffith

  • Jarka Košanová added 1258 commits

    added 1258 commits

    Compare with previous version

  • Jarka Košanová reset approvals from @ck3g and @rkadam3 by pushing to the branch

    reset approvals from @ck3g and @rkadam3 by pushing to the branch

  • added 1 commit

    • b191bcc4 - Make sure instance level role names are uniq

    Compare with previous version

  • requested review from @DylanGriffith

  • Dylan Griffith removed review request for @DylanGriffith

    removed review request for @DylanGriffith

  • added 1 commit

    • 48629cb6 - Validate member role name uniqnuess only on create

    Compare with previous version

  • mentioned in issue #443780 (closed)

  • requested review from @DylanGriffith

  • Dylan Griffith approved this merge request

    approved this merge request

  • added databaseapproved label and removed databasereviewed label

  • Dylan Griffith resolved all threads

    resolved all threads

  • Dylan Griffith requested review from @alexbuijs and removed review request for @DylanGriffith

    requested review from @alexbuijs and removed review request for @DylanGriffith

  • Alex Buijs approved this merge request

    approved this merge request

  • Alex Buijs removed review request for @alexbuijs

    removed review request for @alexbuijs

  • Alex Buijs requested review from @DylanGriffith

    requested review from @DylanGriffith

  • Jarka Košanová resolved all threads

    resolved all threads

  • Jarka Košanová requested review from @rkadam3

    requested review from @rkadam3

  • Rajendra Kadam approved this merge request

    approved this merge request

  • Rajendra Kadam resolved all threads

    resolved all threads

  • Rajendra Kadam enabled an automatic merge when the pipeline for 457ff24c succeeds

    enabled an automatic merge when the pipeline for 457ff24c succeeds

  • Hello @jarka :wave:

    The database team is looking for ways to improve the database review process and we would love your help!

    If you'd be open to someone on the database team reaching out to you for a chat, or if you'd like to leave some feedback asynchronously, just post a reply to this comment mentioning:

    @gitlab-org/database-team

    And someone will be by shortly!

    Thanks for your help! :heart:

    This message was generated automatically. You're welcome to improve it.

  • mentioned in commit b0f11581

  • added workflowstaging label and removed workflowcanary label

  • Jarka Košanová mentioned in merge request !146293 (merged)

    mentioned in merge request !146293 (merged)

  • 25517 25517
    25518 25518 CREATE INDEX index_member_approval_on_reviewed_by_id ON member_approvals USING btree (reviewed_by_id);
    25519 25519
    25520 CREATE UNIQUE INDEX index_member_roles_on_name_unique ON member_roles USING btree (name) WHERE (namespace_id IS NULL);
    • Even after scripts/regenerate-schema I see git-diff in db/structure.sql file:

      diff --git db/structure.sql db/structure.sql
      index ad951b7af4ae..4ff89e76becf 100644
      --- db/structure.sql
      +++ db/structure.sql
      @@ -25647,12 +25647,12 @@ CREATE INDEX index_member_approval_on_requested_by_id ON member_approvals USING
      
       CREATE INDEX index_member_approval_on_reviewed_by_id ON member_approvals USING btree (reviewed_by_id);
      
      -CREATE UNIQUE INDEX index_member_roles_on_name_unique ON member_roles USING btree (name) WHERE (namespace_id IS NULL);
      -
       CREATE INDEX index_member_approvals_on_member_role_id ON member_approvals USING btree (member_role_id);
      
       CREATE INDEX index_member_approvals_on_user_id ON member_approvals USING btree (user_id);
      
      +CREATE UNIQUE INDEX index_member_roles_on_name_unique ON member_roles USING btree (name) WHERE (namespace_id IS NULL);
      +
       CREATE INDEX index_member_roles_on_namespace_id ON member_roles USING btree (namespace_id);
      
       CREATE UNIQUE INDEX index_member_roles_on_namespace_id_name_unique ON member_roles USING btree (namespace_id, name) WHERE (namespace_id IS NOT NULL);

      I wondering whether it is something with my local environment or whether we should update that file on the master branch? :thinking:

    • I get the same diff after gdk update. We have to update strcuture.sql, the order should alphabetical by index name.

    • Ah, the problem is actually caused by !144882 (merged), and db:check-migrations did fail for it (though it as a job that is allowed to fail).

      Edited by Krasimir Angelov
    • Please register or sign in to reply
  • mentioned in commit 2bc9dad9

  • Krasimir Angelov mentioned in merge request !146959 (merged)

    mentioned in merge request !146959 (merged)

  • mentioned in commit 903ff37a

  • mentioned in commit b8a0b426

  • Krasimir Angelov mentioned in merge request !144882 (merged)

    mentioned in merge request !144882 (merged)

  • mentioned in commit 29c9b06c

  • Jarka Košanová mentioned in merge request !147048 (merged)

    mentioned in merge request !147048 (merged)

  • Jarka Košanová mentioned in merge request !151172 (merged)

    mentioned in merge request !151172 (merged)

  • Please register or sign in to reply
    Loading