summaryrefslogtreecommitdiff
path: root/db/migrate/20201022094846_update_historical_data_recorded_at.rb
blob: bd1889149bb0c52b1e1f1714a832020956180ede (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
# frozen_string_literal: true

class UpdateHistoricalDataRecordedAt < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    update_value = Arel.sql("COALESCE(created_at, date + '12:00'::time AT TIME ZONE '#{Time.zone&.tzinfo&.name || "Etc/UTC"}')")

    update_column_in_batches(:historical_data, :recorded_at, update_value) do |table, query|
      query.where(table[:recorded_at].eq(nil))
    end

    add_not_null_constraint :historical_data, :recorded_at

    change_column_null :historical_data, :date, true
  end

  def down
    change_column_null :historical_data, :date, false

    remove_not_null_constraint :historical_data, :recorded_at

    update_column_in_batches(:historical_data, :recorded_at, nil) do |table, query|
      query.where(table[:recorded_at].not_eq(nil))
    end
  end
end