Loading
Migrate deprecated assignable permissions
What does this MR do and why?
Update the GranularScopePermissions database helper to accept a hash of permission names to update and add a migration to migrate deprecated permission names.
The migrated permissions were deprecated in these MR's:
References
Issue: #586412 (closed)
Database
Batching scope query
The batching scope uses the jsonb ?| operator to find rows containing any of the old permission names in a single predicate:
Postgres.ai: https://console.postgres.ai/shared/d3befdea-32f4-4bff-a28d-670d8c0c5601
Raw SQL
SELECT id FROM granular_scopes
WHERE permissions ?| array[
'read_user_ssh_key',
'create_user_ssh_key',
'delete_user_ssh_key',
'read_user_gpg_key',
'create_user_gpg_key',
'delete_user_gpg_key',
'revoke_user_gpg_key',
'read_user_email',
'create_user_email',
'delete_user_email',
'read_user_counts',
'create_user_support_pin',
'read_user_support_pin',
'read_user_activity',
'read_user_association',
'update_user_avatar',
'read_user_follower',
'read_user_following',
'read_user_preference',
'update_user_preference',
'read_user_project_deploy_key',
'read_user_status',
'update_user_status',
'read_project_export',
'create_project_export',
'download_project_export',
'read_project_import',
'create_project_import',
'read_project_relation_export',
'create_project_relation_export',
'download_project_relation_export',
'read_project_relation_import',
'create_project_relation_import',
'read_project_alias',
'create_project_alias',
'delete_project_alias',
'download_group_export',
'start_group_export',
'create_group_import',
'read_starred_project',
'read_merge_request_approval_state',
'read_merge_request_label_event',
'read_issue_label_event',
'read_epic_label_event',
'update_webhook_url_variable',
'delete_webhook_url_variable',
'update_webhook_custom_header',
'delete_webhook_custom_header',
'test_webhook',
'resend_webhook_event',
'increment_usage_data_metric',
'read_relation_export',
'create_relation_export',
'download_relation_export',
'read_relation_import',
'create_relation_import',
'read_bulk_import',
'create_bulk_import',
'cancel_bulk_import',
'read_bulk_import_entity',
'read_bulk_import_entity_failure',
'create_bitbucket_import',
'create_bitbucket_server_import',
'create_github_import',
'cancel_github_import',
'create_github_gist_import'
]
AND id >= 0
ORDER BY id
LIMIT 1000;Query plan
Limit (cost=0.29..1313.99 rows=1000 width=8) (actual time=2.278..111.238 rows=1000 loops=1)
Buffers: shared hit=1462 read=279 dirtied=14
WAL: records=16 fpi=14 bytes=113472
-> Index Scan using granular_scopes_pkey on public.granular_scopes (cost=0.29..5822.62 rows=4432 width=8) (actual time=2.276..111.105 rows=1000 loops=1)
Index Cond: (granular_scopes.id >= 0)
Filter: (granular_scopes.permissions ?| '{read_user_ssh_key,create_user_ssh_key,delete_user_ssh_key,read_user_gpg_key,create_user_gpg_key,delete_user_gpg_key,revoke_user_gpg_key,read_user_email,create_user_email,delete_user_email,read_user_counts,create_user_support_pin,read_user_support_pin,read_user_activity,read_user_association,update_user_avatar,read_user_follower,read_user_following,read_user_preference,update_user_preference,read_user_project_deploy_key,read_user_status,update_user_status,read_project_export,create_project_export,download_project_export,read_project_import,create_project_import,read_project_relation_export,create_project_relation_export,download_project_relation_export,read_project_relation_import,create_project_relation_import,read_project_alias,create_project_alias,delete_project_alias,download_group_export,start_group_export,create_group_import,read_starred_project,read_merge_request_approval_state,read_merge_request_label_event,read_issue_label_event,read_epic_label_event,update_webhook_url_variable,delete_webhook_url_variable,update_webhook_custom_header,delete_webhook_custom_header,test_webhook,resend_webhook_event,increment_usage_data_metric,read_relation_export,create_relation_export,download_relation_export,read_relation_import,create_relation_import,read_bulk_import,create_bulk_import,cancel_bulk_import,read_bulk_import_entity,read_bulk_import_entity_failure,create_bitbucket_import,create_bitbucket_server_import,create_github_import,cancel_github_import,create_github_gist_import}'::text[])
Rows Removed by Filter: 4387
Buffers: shared hit=1462 read=279 dirtied=14
WAL: records=16 fpi=14 bytes=113472
Settings: effective_cache_size = '472585MB', jit = 'off', random_page_cost = '1.5', work_mem = '230MB', seq_page_cost = '4'UPDATE query (executed once per rename entry per sub-batch)
Postgres.ai: https://console.postgres.ai/shared/8af2c85d-f2a2-435e-a623-4693325a564a
Raw SQL
UPDATE granular_scopes
SET permissions = (
SELECT jsonb_agg(DISTINCT elem)
FROM jsonb_array_elements(
(permissions - 'read_user_ssh_key') || '["read_ssh_key"]'::jsonb
) AS elem
)
WHERE id IN (SELECT id FROM granular_scopes WHERE id BETWEEN 0 AND 10000)
AND permissions @> '"read_user_ssh_key"'::jsonb;Query plan
ModifyTable on public.granular_scopes (cost=0.57..11.49 rows=0 width=0) (actual time=0.035..0.036 rows=0 loops=1)
Buffers: shared hit=9
-> Nested Loop (cost=0.57..11.49 rows=1 width=44) (actual time=0.034..0.035 rows=0 loops=1)
Buffers: shared hit=9
-> Index Scan using granular_scopes_pkey on public.granular_scopes granular_scopes_1 (cost=0.29..3.31 rows=1 width=14) (actual time=0.028..0.028 rows=1 loops=1)
Index Cond: ((granular_scopes_1.id >= 0) AND (granular_scopes_1.id <= 10000))
Buffers: shared hit=6
-> Index Scan using granular_scopes_pkey on public.granular_scopes (cost=0.29..3.31 rows=1 width=255) (actual time=0.004..0.004 rows=0 loops=1)
Index Cond: (granular_scopes.id = granular_scopes_1.id)
Filter: (granular_scopes.permissions @> '"read_user_ssh_key"'::jsonb)
Rows Removed by Filter: 1
Buffers: shared hit=3
SubPlan 1
-> Aggregate (cost=4.83..4.84 rows=1 width=32) (actual time=0.000..0.000 rows=0 loops=0)
-> Sort (cost=4.33..4.58 rows=100 width=32) (actual time=0.000..0.000 rows=0 loops=0)
Sort Key: elem.value
-> Function Scan on jsonb_array_elements elem (cost=0.01..1.01 rows=100 width=32) (actual time=0.000..0.000 rows=0 loops=0)
Settings: random_page_cost = '1.5', work_mem = '230MB', seq_page_cost = '4', effective_cache_size = '472585MB', jit = 'off'Notes:
- Both queries use the primary key index (
granular_scopes_pkey) for efficient scanning - The batching scope collapses to a single
?|predicate rather than a chain of@>ORs (per Eugie's suggestion) - The UPDATE is executed once per rename entry (65 entries) per sub-batch, but each UPDATE is scoped to the sub-batch and filtered by the
@>containment operator which skips non-matching rows
Screenshots or screen recordings
| Before | After |
|---|---|
How to set up and validate locally
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.
Edited by Alex Buijs