summaryrefslogtreecommitdiff
path: root/db/post_migrate/20210406144743_backfill_total_tuple_count_for_batched_migrations.rb
blob: 5fc5a5b2b6e26760ed8af36e9bb5fa58488ac483 (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
# frozen_string_literal: true

class BackfillTotalTupleCountForBatchedMigrations < ActiveRecord::Migration[6.0]
  DOWNTIME = false

  def up
    return unless should_run?

    Gitlab::Database::BackgroundMigration::BatchedMigration.all.each do |migration|
      total_tuple_count = Gitlab::Database::PgClass.for_table(migration.table_name)&.cardinality_estimate

      migration.update(total_tuple_count: total_tuple_count)
    end
  end

  def down
    return unless should_run?

    Gitlab::Database::BackgroundMigration::BatchedMigration.update_all(total_tuple_count: nil)
  end

  private

  def should_run?
    Gitlab.dev_or_test_env? || Gitlab.com?
  end
end