debian-mirror-gitlab/config/initializers/active_record_data_types.rb

54 lines
1.6 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
# ActiveRecord custom data type for storing datetimes with timezone information.
2019-12-04 20:38:33 +05:30
# See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/11229
2017-09-10 17:25:29 +05:30
2019-10-12 21:52:04 +05:30
require 'active_record/connection_adapters/postgresql_adapter'
2017-09-10 17:25:29 +05:30
2019-10-12 21:52:04 +05:30
module ActiveRecord::ConnectionAdapters::PostgreSQL::OID
# Add the class `DateTimeWithTimeZone` so we can map `timestamptz` to it.
class DateTimeWithTimeZone < DateTime
def type
:datetime_with_timezone
2017-09-10 17:25:29 +05:30
end
end
2019-10-12 21:52:04 +05:30
end
2017-09-10 17:25:29 +05:30
2019-10-12 21:52:04 +05:30
module RegisterDateTimeWithTimeZone
# Run original `initialize_type_map` and then register `timestamptz` as a
# `DateTimeWithTimeZone`.
#
# Apparently it does not matter that the original `initialize_type_map`
# aliases `timestamptz` to `timestamp`.
#
# When schema dumping, `timestamptz` columns will be output as
# `t.datetime_with_timezone`.
def initialize_type_map(mapping = type_map)
super mapping
2020-03-13 15:44:24 +05:30
register_class_with_precision(
mapping,
'timestamptz',
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::DateTimeWithTimeZone
)
2017-09-10 17:25:29 +05:30
end
2019-10-12 21:52:04 +05:30
end
2017-09-10 17:25:29 +05:30
2019-10-12 21:52:04 +05:30
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
prepend RegisterDateTimeWithTimeZone
2017-09-10 17:25:29 +05:30
2019-10-12 21:52:04 +05:30
# Add column type `datetime_with_timezone` so we can do this in
# migrations:
#
# add_column(:users, :datetime_with_timezone)
#
NATIVE_DATABASE_TYPES[:datetime_with_timezone] = { name: 'timestamptz' }
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
# Ensure `datetime_with_timezone` columns are correctly written to schema.rb
if (ActiveRecord::Base.connection.active? rescue false)
ActiveRecord::Base.connection.send :reload_type_map
end
2020-03-13 15:44:24 +05:30
ActiveRecord::Base.time_zone_aware_types += [:datetime_with_timezone]