Skip to content

Fix pagination of resource label events

Heinrich Lee Yu requested to merge fix-pagination-of-label-events into master

What does this MR do and why?

Fixes a bug where resource label events were not grouped for pagination. This can result to situations where the label events can be split across multiple pages.

So instead of having a system note that says "Added label 1, label 2, label 3, label 4", you'll get "Added label 1, label 2" (from the first page) and "Added label 3, label 4" (from the next page).

Also, on work item notes, which I think uses Vue / Apollo, this leads to a JS error because both of those notes will have the same discussion ID and we use that as the global ID.

Database

Query diff:

SELECT
  "notes".*
FROM
  (
    (
      SELECT
        'notes' AS table_name,
-       "notes"."discussion_id",
        MIN(id) AS id,
-       MIN(created_at) AS created_at
+	MIN(created_at) AS created_at,
+       ARRAY_AGG(id) AS ids 
      FROM
        (
          (
            SELECT
              "notes".*
            FROM
              "notes"
            WHERE
              "notes"."noteable_id" = 204888603
              AND "notes"."noteable_type" = 'MergeRequest'
          )
          UNION ALL
            (
              SELECT
                "notes".*
              FROM
                "notes"
              WHERE
                "notes"."system" = FALSE
                AND "notes"."project_id" IN (2, 2)
                AND "notes"."noteable_type" = 'Commit'
                AND "notes"."commit_id" = '0926737bb43d6f148100bc1662854c18ec423ef9'
            )
        ) notes
      GROUP BY
        "notes"."discussion_id"
    )
    UNION ALL
      (
        SELECT
          'resource_label_events',
-         'NULL',
-         "resource_label_events"."id",
-         "resource_label_events"."created_at"
+         MIN(id),
+         "resource_label_events"."created_at",
+         ARRAY_AGG(id)
        FROM
          "resource_label_events"
        WHERE
          "resource_label_events"."merge_request_id" = 204888603
+       GROUP BY
+         "resource_label_events"."created_at",
+         "resource_label_events"."user_id"
      )
    UNION ALL
      (
        SELECT
          'resource_state_events',
-         'NULL',
          "resource_state_events"."id",
-         "resource_state_events"."created_at"
+         "resource_state_events"."created_at",
+         ARRAY_FILL(id, ARRAY [1])
        FROM
          "resource_state_events"
        WHERE
          "resource_state_events"."merge_request_id" = 204888603
      )
    UNION ALL
      (
        SELECT
          'resource_milestone_events',
-         'NULL',
          "resource_milestone_events"."id",
-         "resource_milestone_events"."created_at"
+ 	  "resource_milestone_events"."created_at",
+         ARRAY_FILL(id, ARRAY [1])
        FROM
          "resource_milestone_events"
        WHERE
          "resource_milestone_events"."merge_request_id" = 204888603
      )
    UNION ALL
      (
        SELECT
          'resource_iteration_events',
-         'NULL',
          "resource_iteration_events"."id",
-         "resource_iteration_events"."created_at"
+         "resource_iteration_events"."created_at",
+         ARRAY_FILL(id, ARRAY [1])
        FROM
          "resource_iteration_events"
        WHERE
          "resource_iteration_events"."merge_request_id" = 204888603
      )
  ) notes
ORDER BY
  "notes"."created_at" ASC,
  "notes"."id" ASC

Query plan: https://console.postgres.ai/shared/bb225f98-1787-4ba4-9f45-2981908591d3

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Andy Schoenen

Merge request reports