diff options
Diffstat (limited to 'db/migrate/20201022094846_update_historical_data_recorded_at.rb')
-rw-r--r-- | db/migrate/20201022094846_update_historical_data_recorded_at.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/db/migrate/20201022094846_update_historical_data_recorded_at.rb b/db/migrate/20201022094846_update_historical_data_recorded_at.rb new file mode 100644 index 00000000000..bd1889149bb --- /dev/null +++ b/db/migrate/20201022094846_update_historical_data_recorded_at.rb @@ -0,0 +1,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 |