Skip to content

Add index to email field on users table if does not already exist

What does this MR do?

This MR tries to bring structure.sql to be closer to what we actually have on production DB, i.e. on production we actually have both indexes: index_users_on_email ON users USING btree (email) and index_users_on_lower_email ON users USING btree (lower((email)::text)) but the later one is not reflected in strcuture.sql

for reference:

FYI: I just noticed the indexes index_on_users_lower_username and index_on_users_lower_email exist on production, but not in db/structure.sql. Only index_on_users_name_lower does. We should write a migration to add it if it does not exist.

!33883 (comment 440922556)

Output

  • Up

== 20201104111152 AddIndexOnUsersTableLoweredEmail: migrating =================
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:users, "LOWER(email)", {:unique=>true, :name=>"index_users_on_lower_email", :algorithm=>:concurrently})
   -> 0.0135s
-- execute("SET statement_timeout TO 0")
   -> 0.0002s
-- add_index(:users, "LOWER(email)", {:unique=>true, :name=>"index_users_on_lower_email", :algorithm=>:concurrently})
   -> 0.0132s
-- execute("RESET ALL")
   -> 0.0003s
== 20201104111152 AddIndexOnUsersTableLoweredEmail: migrated (0.0278s) ========
  • Down

== 20201104111152 AddIndexOnUsersTableLoweredEmail: reverting =================
-- transaction_open?()
   -> 0.0000s
-- indexes(:users)
   -> 0.0114s
-- execute("SET statement_timeout TO 0")
   -> 0.0003s
-- remove_index(:users, {:algorithm=>:concurrently, :name=>"index_users_on_lower_email"})
   -> 0.0057s
-- execute("RESET ALL")
   -> 0.0002s
== 20201104111152 AddIndexOnUsersTableLoweredEmail: reverted (0.0182s) ========

Duration

  • ~2 mins on #databse-lab:
exec CREATE UNIQUE INDEX CONCURRENTLY index_users_on_lower_email ON users USING btree (lower((email)::text));
Session: joe-buht62o350j13v0696og
The query has been executed. Duration: 1.706 min (edited) 

Index Details

\di+ index_users_on_lower_email
Session: joe-buht62o350j13v0696og
Command output:
List of relations
 Schema |            Name            | Type  | Owner  | Table |  Size  | Description 
--------+----------------------------+-------+--------+-------+--------+-------------
 public | index_users_on_lower_email | index | gitlab | users | 303 MB | 
(1 row)

re #214466 (closed) !33883 (comment 441509284)

Screenshots (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Alexandru Croitor

Merge request reports