Add primary email to emails upon confirmation
What does this MR do and why?
This MR adds primary email to emails upon email confirmation. This allows to remove complex logic from after_commit
callback in User
model. (This will be fully done in #344134 (closed), after background migration of this MR run successfully and we clean up after it.)
It removes the validation disallowing creation of an Email with the same value as primary email. Now it's possible as long as it's the primary email of the same user.
This MR adds background data migration which adds the primary email of confirmed users to Emails, unless that email is already in Emails table. There are around 8 million confirmed users.
It's a step towards completing https://gitlab.com/gitlab-org/gitlab/-/issues/218788. The removal of Devise confirmable
might still be worth to do, but it seems to be not trivial - let's start with this step.
Database review
Query in app/models/email.rb
:
explain SELECT 1 AS one FROM "users" WHERE "users"."email" = 'theuser@example.com' AND "users"."id" != 1234 LIMIT 1;
Plan with execution:
Limit (cost=0.56..3.58 rows=1 width=4) (actual time=16.495..16.496 rows=0 loops=1)
Buffers: shared read=4
I/O Timings: read=16.299 write=0.000
-> Index Scan using index_users_on_email on public.users (cost=0.56..3.58 rows=1 width=4) (actual time=16.492..16.493 rows=0 loops=1)
Index Cond: ((users.email)::text = 'theuser@example.com'::text)
Filter: (users.id <> 1234)
Rows Removed by Filter: 0
Buffers: shared read=4
I/O Timings: read=16.299 write=0.000
Summary:
Time: 18.682 ms
- planning: 2.148 ms
- execution: 16.534 ms
- I/O read: 16.299 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 0 from the buffer pool
- reads: 4 (~32.00 KiB) from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.