Data Model & Database Interactions
The Discussions feature in GitLab is built on a flexible data model that leverages the existing GitLab database and API. This model allows for a wide range of discussion types, including:
- Discussions: Standard threads that can be associated with issues, merge requests, epics, and project wikis.
- Notes: A special type of discussion that can be used for collaboration and feedback directly within the code.
Core Tables
The following tables are core to the Discussions feature and represent the primary data model:
discussions
: This table stores information about each discussion, including its title, body, and associated resource (issue, merge request, epic, or wiki page).notes
: This table stores information about each note within a discussion. It includes the note body, author, and timestamp.discussion_participants
: This table tracks which users are involved in a discussion, including their role (e.g., author, commenter) and any specific permissions they have.
Data Model Interactions
The data model is accessed through the Rails API, which provides a consistent and efficient way to interact with the database. Here are some common use cases:
Creating a new discussion:
discussion = Discussion.new( title: "New Discussion", body: "This is a new discussion.", resource_type: "Issue", resource_id: 1 ) discussion.save!
Retrieving a discussion:
discussion = Discussion.find(1)
Creating a new note:
note = Note.new( discussion_id: 1, body: "This is a new note.", author_id: 1 ) note.save!
Retrieving notes for a discussion:
notes = Discussion.find(1).notes
Database Migrations
All changes to the data model are handled through database migrations. These migrations are stored in the db/migrate
directory and are responsible for creating, altering, and deleting tables and columns. The gitlab
database is used to store all the data related to GitLab, including discussions.
Further Resources:
- GitLab Discussions Rails codebase: -/tree/main/app/models
- GitLab API Documentation: https://docs.gitlab.com/ee/api/