Skip to content

Make User#user_type not nullable

Summary

Currently "human" user type has value of NULL which works poorly in query conditions and indexes. E.g. instead of user_type IN (0,5,6) we have to write user_type IS NULL OR user_type IN (5,6) which is not covered by index (user_type).

Proposal

Make value 0 as "human" user type too so it can be indexed and mentioned in IN clauses.

  1. Create indexes with user_type = 0 in addition to user_type IS NULL where needed.
  2. Make the app aware about 0 value as human value too.
  3. Make column default to 0 instead of NULL
  4. Migrate all rows with user_type = NULL to 0
  5. Prohibit nil as enum value at Ruby level. Leave only 0 as user type for humans.
  6. Enforce NOT NULL constraint on user_type field.
  7. Update indexes, remove the IS null condition. (post migrate)
Edited by Pavel Shutsin