summaryrefslogtreecommitdiff
path: root/config/initializers/active_record_data_types.rb
blob: 0359e14b23218f23adf2fa6f6dbbf7d8828b69d5 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# ActiveRecord custom data type for storing datetimes with timezone information.
# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11229

if Gitlab::Database.postgresql?
  require 'active_record/connection_adapters/postgresql_adapter'

  module ActiveRecord::ConnectionAdapters::PostgreSQL::OID
    # 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`.
    def initialize_type_map(mapping)
      super mapping

      mapping.register_type 'timestamptz' do |_, _, sql_type|
        precision = extract_precision(sql_type)
        ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::DateTimeWithTimeZone.new(precision: precision)
      end
    end
  end

  class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
    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
elsif Gitlab::Database.mysql?
  require 'active_record/connection_adapters/mysql2_adapter'

  module RegisterDateTimeWithTimeZone
    # Run original `initialize_type_map` and then register `timestamp` as a
    # `MysqlDateTimeWithTimeZone`.
    #
    # When schema dumping, `timestamp` columns will be output as
    # `t.datetime_with_timezone`.
    def initialize_type_map(mapping)
      super mapping

      mapping.register_type(%r(timestamp)i) do |sql_type|
        precision = extract_precision(sql_type)
        ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlDateTimeWithTimeZone.new(precision: precision)
      end
    end
  end

  class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
    prepend RegisterDateTimeWithTimeZone

    # Add the class `DateTimeWithTimeZone` so we can map `timestamp` to it.
    class MysqlDateTimeWithTimeZone < MysqlDateTime
      def type
        :datetime_with_timezone
      end
    end

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

# 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