blob: 933ad747c5c8abecf9f6d88b3011e65604469ebc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# frozen_string_literal: true
class BackfillCycleAnalyticsAggregations < Gitlab::Database::Migration[1.0]
BATCH_SIZE = 50
def up
model = define_batchable_model('analytics_cycle_analytics_group_value_streams')
model.each_batch(of: BATCH_SIZE) do |relation|
execute <<~SQL
WITH records_to_be_inserted AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} (
SELECT root_ancestor.id AS group_id
FROM (#{relation.select(:group_id).to_sql}) as value_streams,
LATERAL (
WITH RECURSIVE "base_and_ancestors" AS (
(SELECT "namespaces"."id", "namespaces"."parent_id" FROM "namespaces" WHERE "namespaces"."id" = value_streams.group_id)
UNION
(SELECT "namespaces"."id", "namespaces"."parent_id" FROM "namespaces", "base_and_ancestors" WHERE "namespaces"."id" = "base_and_ancestors"."parent_id")
)
SELECT "namespaces"."id" FROM "base_and_ancestors" as "namespaces" WHERE parent_id IS NULL LIMIT 1
) as root_ancestor
)
INSERT INTO "analytics_cycle_analytics_aggregations"
SELECT * FROM "records_to_be_inserted"
ON CONFLICT DO NOTHING
SQL
end
end
def down
# no-op
end
end
|