Add organization id to topics
What does this MR do and why?
This is the first MR that adds the sharding key (organization_id) to topics.
- It adds
organization_id
to the topics table and creates all related foreign keys and indexes.
Step | MR |
---|---|
1 | Add organization id to topics - this MR |
2 | Change topics application code to use organization id - TBD |
3 | Copy project topics to organization - TBD |
3 |
Remove the organization_id column default - TBD |
Notes:
- Topics will be assigned to a default organization for now:
organization 1
; - The default organization will be removed from topics, once we change all necessary code;
- As we can create topics for different organizations, they all shall remain under the same default organization for now;
- In production, there are 3 projects not associated with default org and none of them have topics:
gitlabhq_dblab=# SELECT id FROM "projects" WHERE "projects"."organization_id" != 1;
id
----------
59701951
61040785
60992049
(3 rows)
gitlabhq_dblab=# SELECT projects.name project, organizations.name organization FROM projects JOIN organizations ON organizations.id = projects.organization_id WHERE projects.id IN (60992049, 59701951, 61040785);
project | organization
-----------------+-----------------------------
Test Project | jl-test-organization
lc-test-project | lc-test-org
baz | Peter Hegman's Organization
(3 rows)
gitlabhq_dblab=# SELECT * FROM project_topics WHERE project_id IN (60992049, 59701951, 61040785);
id | project_id | topic_id | created_at | updated_at
----+------------+----------+------------+------------
(0 rows)
How to test
- On
master
, add some topics:
Project.all.each do |project|
project.topic_list = project.path
project.save!
end
%w[Ruby Postgres JavaScript Rails Bash].each do |topic|
Projects::Topic.create!(name: topic, title: topic)
end
-
Checkout
463254/1-add-organization-id-to-topics
and migrate the database -
On console, try to create some topics:
# Uses default organization 1
Projects::Topic.create!(name: 'topic-1', title: 'topic-1')
# Same topic can be created with a different organization
Projects::Topic.create!(name: 'topic-1', title: 'topic-1', organization_id: Organizations::Organization.where.not(id: 1).last.id)
# When topics are created through projects, they will be created in the same organization as the project
project = Project.where.not(organization_id: 1).last
topic_name = "TopicFromOrganization-#{project.organization_id}"
project.topic_list = topic_name
project.save!
Projects::Topic.for_organization(project.organization_id).where(name: topic_name)
# => [<Projects::Topic:0x000000017d065c88 id: 19, name: "TopicFromOrganization-1019", ... organization_id: 1019>]
- Create/Edit topics trough admin page:
http://gdk.test:3000/admin/topics
- Explore created topics and projects at
http://gdk.test:3000/explore/projects/topics/
Edited by Leonardo da Rosa