Background migration to move requirement to work items
What does this MR do and why?
Background migration to migrate requirement objects into work items(issues) of requirement type.
This is part of a plan to deprecate old Requirement
objects. For more information check: #323779 (closed)
This migration creates new issues directly on DB which means we have to generate and store latest issue iids in order to keep creation of new issues valid.
related to #342496 (closed)
Scheduling queries
There are only ~12500 records on the requirements table. I am using a 30 batch size for scheduling which should produce ~420 jobs. These should run for each batch scheduled.
SELECT "requirements"."id" FROM "requirements" WHERE "requirements"."issue_id" IS NULL ORDER BY "requirements"."id" ASC LIMIT 1
https://explain.depesz.com/s/Fg9v#html
SELECT "requirements"."id" FROM "requirements" WHERE "requirements"."issue_id" IS NULL AND "requirements"."id" >= 1 ORDER BY "requirements"."id" ASC LIMIT 1 OFFSET 30
https://explain.depesz.com/s/Kh5v
SELECT MIN("requirements"."id"), MAX("requirements"."id") FROM "requirements" WHERE "requirements"."issue_id" IS NULL AND "requirements"."id" >= 1 AND "requirements"."id" < 38
https://explain.depesz.com/s/sHTw#html
Execution queries
All the queries below run inside a transaction for each requirement record:
INSERT INTO issues (iid, issue_type, title, description, state_id, author_id, project_id, created_at, updated_at)
VALUES
(
COALESCE((SELECT MAX(iid) FROM issues WHERE project_id = 278964), 0) + 1,
3,
'A requirement work item',
NULL,
1,
426128,
278964,
'2021-10-18 18:50:12',
'2021-10-18 18:50:12'
) RETURNING id, iid, project_id
https://explain.depesz.com/s/2fLO
duration: 55ms on database lab
UPDATE "requirements" SET "updated_at" = '2021-10-18 18:50:12', "issue_id" = 172 WHERE "requirements"."id" = 221
https://explain.depesz.com/s/RUvU7
UPDATE "internal_ids" SET "last_value" = GREATEST("internal_ids"."last_value", 1) WHERE "internal_ids"."project_id" = 278964 AND "internal_ids"."usage" = 0 RETURNING "last_value"
https://explain.depesz.com/s/HB9L
SELECT MAX("issues"."iid") FROM "issues" WHERE "issues"."project_id" = 278964
https://explain.depesz.com/s/uyAm
INSERT INTO "internal_ids" ("project_id","namespace_id","usage","last_value") VALUES (278964, NULL, 0, 1) ON CONFLICT DO NOTHING RETURNING "id"
https://explain.depesz.com/s/wshG This one only runs if the update on internal id does not return a result