Save taggings data to ci_build_tags
This is a ticket for the work of ci_build_tags
After #467196 (closed) and #467197 (closed) when a new job or runner is tagged, we must insert that data into their specific tables too along with the inserts into taggings.
This feature should be under a feature flag.
Example for jobs:
job = Ci::Build.last
job.tag_list = ['ruby', 'docker']
job.save!
This should trigger the following queries:
INSERT INTO "tags" ("name") VALUES ('ruby'), ('docker') ON CONFLICT ("name") DO NOTHING RETURNING "id";
INSERT INTO "taggings" ("tag_id", "taggable_type", "context", "created_at", "taggable_id") VALUES (5, 'CommitStatus', 'tags', '2024-06-12 12:42:11.681466', 208740) RETURNING "id";
INSERT INTO "taggings" ("tag_id", "taggable_type", "context", "created_at", "taggable_id") VALUES (6, 'CommitStatus', 'tags', '2024-06-12 12:42:11.684550', 208740) RETURNING "id";
INSERT INTO "p_ci_builds_tags" ("tag_id", "build_id", "partition_id", "project_id") VALUES (5, 208740, 101, 296), (6, 208740, 101, 296) RETURNING "id"; -- bulk insert by default
Similarly, reading job.tag_list should generate this query when reading from taggings:
[11] pry(main)> job.tag_list
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 208740 AND "taggings"."taggable_type" = 'CommitStatus' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
=> ["docker", "ruby"]
But tag_list should implement reading from the new table too under a feature flag and generate this query:
[11] pry(main)> job.tag_list
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" INNER JOIN "p_ci_builds_tags" ON "tags"."id" = "p_ci_builds_tags"."tag_id" WHERE "p_ci_builds_tags"."build_id" = 208740;
=> ["docker", "ruby"]
https://gitlab.com/gitlab-org/gitlab/-/blob/ebf1aa86b28adf1b91497a41f1ef3d9d8d4f36f7/app/models/concerns/ci/metadatable.rb#L60-117 implements a similar patter from reading/writing the same data to multiple tables.
Edited by Tianwen Chen