Welcome to mirror list, hosted at ThFree Co, Russian Federation.

1_active_record_data_types.rb « initializers « config - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c0671f2ca3a4f61e0a93815f156ce3e2b40b773 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

# ActiveRecord custom data type for storing datetimes with timezone information.
# See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/11229

require 'active_record/connection_adapters/postgresql_adapter'

ActiveRecord::Type.register(:ind_jsonb, Gitlab::Database::Type::IndifferentJsonb)
ActiveRecord::Type.register(:sym_jsonb, Gitlab::Database::Type::SymbolizedJsonb)

module ActiveRecord::ConnectionAdapters::PostgreSQL::OID # rubocop:disable Style/ClassAndModuleChildren
  # Add the class `DateTimeWithTimeZone` so we can map `timestamptz` to it.
  class DateTimeWithTimeZone < DateTime
    def type
      :datetime_with_timezone
    end
  end
end

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`.
  class << self
    def initialize_type_map(mapping = type_map)
      super mapping

      register_class_with_precision(
        mapping,
        'timestamptz',
        ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::DateTimeWithTimeZone
      )
    end
  end
end

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter # rubocop:disable Style/ClassAndModuleChildren
  prepend RegisterDateTimeWithTimeZone

  # 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' }
end

def connection_active?
  ActiveRecord::Base.connection.active? # rubocop:disable Database/MultipleDatabases
rescue StandardError
  false
end

# Ensure `datetime_with_timezone` columns are correctly written to schema.rb

# rubocop:disable Database/MultipleDatabases
ActiveRecord::Base.connection.send(:reload_type_map) if connection_active?

ActiveRecord::Base.time_zone_aware_types += [:datetime_with_timezone]
# rubocop:enable Database/MultipleDatabases