Skip to content

Adds EnumInheritance as option for STI

Eduardo Bonet requested to merge sti-through-enum into master

What does this MR do and why?

Adds EnumInheritance as option for Single Table Inheritance

Stock ActiveRecord STI works by storing the class name in the database, which is heavy and unnecessary. EnumInheritance introduces a way to use STI through model defined enums.

I used Packages::Package as an example of using this concern: !127264 (merged)

But GitLab does not like STI

Current documentation (https://docs.gitlab.com/ee/development/database/single_table_inheritance.html) states that STI should be used due to the same problems as polymorphic associations. This is misleading, as STI is not the same as PA. Because PA is an association, it requires two columns (id and type). STI requires only a single column (class), which is understandably a problem, but a problem that this MR addresses. Any table that has some sort of type column is already effectively an STI, just without the tooling to make usage easier.

Alternatives

I also create an alternative that does not use STI: !125902 (closed) . It uses inheritance on the model and a default_scope to define fetching. It works, but has some issues:

  • Usage of default_scope
  • When fetching from the base class, elements are returned with the base class. We need to use becomes to convert.
  • More boilerplate in general

Merge request reports