Commit c45879bb authored by rpereira2's avatar rpereira2

Use concurrent helpers in migration to change type

Use the concurrent helpers recommended by our migration guide to change
type of historical_data.date to timestamptz.
parent 62895180
# frozen_string_literal: true
class ChangeHistoricalDataDateType < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
# historical_data only contains 1059 rows in gitlab.com. A new row is added
# once a day (at 12) by a cronjob (HistoricalDataWorker).
# Self-managed GitLab instances will probably have even less data, since gitlab.com
# is one of the oldest GitLab instances.
disable_ddl_transaction!
# We're using `Time.zone.tzinfo.name` here because the sidekiq-cron gem
# evaluates crons against the timezone configured in Rails.
def up
execute(
<<SQL
ALTER TABLE historical_data ALTER COLUMN date TYPE timestamptz USING ((date + '12:00'::time) AT TIME ZONE '#{Time.zone.tzinfo.name}');
create or replace function change_historical_date_to_timetamptz(date)
returns timestamptz
language sql
as
$$
SELECT ($1 + '12:00'::time) AT TIME ZONE '#{Time.zone&.tzinfo&.name || "Etc/UTC"}'
$$
SQL
)
change_column_type_concurrently(:historical_data, :date, :timestamptz, type_cast_function: "change_historical_date_to_timetamptz")
execute("DROP FUNCTION IF EXISTS change_historical_date_to_timetamptz")
end
def down
change_column :historical_data, :date, :date
undo_change_column_type_concurrently(:historical_data, :date)
end
end
# frozen_string_literal: true
class ChangeHistoricalDataDateTypeCleanup < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
cleanup_concurrent_column_type_change(:historical_data, :date)
end
def down
undo_cleanup_concurrent_column_type_change(:historical_data, :date, :date)
end
end
7a1719eb6975d731f89ce7e269868956d220e8f63dfd1739371ae2ddfd0e2473
\ No newline at end of file
......@@ -12699,10 +12699,11 @@ CREATE TABLE group_wiki_repositories (
CREATE TABLE historical_data (
id integer NOT NULL,
date timestamp with time zone NOT NULL,
active_user_count integer,
created_at timestamp without time zone,
updated_at timestamp without time zone
updated_at timestamp without time zone,
date timestamp with time zone,
CONSTRAINT check_8f3547750e CHECK ((date IS NOT NULL))
);
CREATE SEQUENCE historical_data_id_seq
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment