diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-05 01:22:57 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-05 01:22:57 +0000 |
commit | 7fa016711e540f9b6dda45aa1f47867737c52940 (patch) | |
tree | 545fa4ba39a17df75301c6c90d7aef1e77cba98d /db/post_migrate/20220204110725_backfill_cycle_analytics_aggregations.rb | |
parent | ee3c8d00bd8c17201da0b116a9b3b276804b855f (diff) | |
download | gitlab-ce-7fa016711e540f9b6dda45aa1f47867737c52940.tar.gz |
Add latest changes from gitlab-org/gitlab@15-11-stable-ee
Diffstat (limited to 'db/post_migrate/20220204110725_backfill_cycle_analytics_aggregations.rb')
-rw-r--r-- | db/post_migrate/20220204110725_backfill_cycle_analytics_aggregations.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/db/post_migrate/20220204110725_backfill_cycle_analytics_aggregations.rb b/db/post_migrate/20220204110725_backfill_cycle_analytics_aggregations.rb new file mode 100644 index 00000000000..933ad747c5c --- /dev/null +++ b/db/post_migrate/20220204110725_backfill_cycle_analytics_aggregations.rb @@ -0,0 +1,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 |