I spent some time going over the Postgres schema of Gitlab. GitLab is an alternative to Github. You can self host GitLab since it is an open source DevOps platform.
My motivation to understand the schema of a big project like Gitlab was to compare it against schemas I am designing and learn some best practices from their schema definition. I can surely say I learnt a lot.
I am aware that best practices are sometimes context dependent so you should not apply them blindly.
The Gitlab schema file structure.sql
[1] is more than 34000 lines of code. Gitlab is a monolithic Ruby on Rails application. The popular way to manage schema migration is using the schema.rb
file. The reason the Gitlab team decided to adopt structure.sql
instead is mentioned in on of their issues [2] in their issue tracker.
Now what keeps us from using those features is the use of
schema.rb
. This can only contain standard migrations (using the Rails DSL), which aim to keep the schema file database system neutral and abstract away from specific SQL. This in turn means we are not able to use extended PostgreSQL features that are reflected in schema. Some examples include triggers, postgres partitioning, materialized views and many other great features.In order to leverage those features, we should consider using a plain SQL schema file (
structure.sql
) instead of a ruby/rails standard schemaschema.rb
.The change would entail switching
config.active_record.schema_format = :sql
and regenerate the schema in SQL. Possibly, some build steps would have to be adjusted, too.
Now, let’s go over the things I learnt from Gitlab Postgres schema.
Below are some of the tweets from people on this article. If you find this article useful please share and tag me @shekhargulati
Continue reading “My Notes on GitLab Postgres Schema Design”