|
4 | 4 | if Gitlab::Database.postgresql?
|
5 | 5 | require 'active_record/connection_adapters/postgresql_adapter'
|
6 | 6 |
|
7 |
| - module ActiveRecord |
8 |
| - module ConnectionAdapters |
9 |
| - class PostgreSQLAdapter |
10 |
| - NATIVE_DATABASE_TYPES.merge!(datetime_with_timezone: { name: 'timestamptz' }) |
| 7 | + module ActiveRecord::ConnectionAdapters::PostgreSQL::OID |
| 8 | + # Add the class `DateTimeWithTimeZone` so we can map `timestamptz` to it. |
| 9 | + class DateTimeWithTimeZone < DateTime |
| 10 | + def type |
| 11 | + :datetime_with_timezone |
11 | 12 | end
|
12 | 13 | end
|
13 | 14 | end
|
| 15 | + |
| 16 | + module RegisterDateTimeWithTimeZone |
| 17 | + # Run original `initialize_type_map` and then register `timestamptz` as a |
| 18 | + # `DateTimeWithTimeZone`. |
| 19 | + # |
| 20 | + # Apparently it does not matter that the original `initialize_type_map` |
| 21 | + # aliases `timestamptz` to `timestamp`. |
| 22 | + # |
| 23 | + # When schema dumping, `timestamptz` columns will be output as |
| 24 | + # `t.datetime_with_timezone`. |
| 25 | + def initialize_type_map(mapping) |
| 26 | + super mapping |
| 27 | + |
| 28 | + mapping.register_type 'timestamptz' do |_, _, sql_type| |
| 29 | + precision = extract_precision(sql_type) |
| 30 | + ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::DateTimeWithTimeZone.new(precision: precision) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter |
| 36 | + prepend RegisterDateTimeWithTimeZone |
| 37 | + |
| 38 | + # Add column type `datetime_with_timezone` so we can do this in |
| 39 | + # migrations: |
| 40 | + # |
| 41 | + # add_column(:users, :datetime_with_timezone) |
| 42 | + # |
| 43 | + NATIVE_DATABASE_TYPES[:datetime_with_timezone] = { name: 'timestamptz' } |
| 44 | + end |
14 | 45 | elsif Gitlab::Database.mysql?
|
15 | 46 | require 'active_record/connection_adapters/mysql2_adapter'
|
16 | 47 |
|
17 |
| - module ActiveRecord |
18 |
| - module ConnectionAdapters |
19 |
| - class AbstractMysqlAdapter |
20 |
| - NATIVE_DATABASE_TYPES.merge!(datetime_with_timezone: { name: 'timestamp' }) |
| 48 | + module RegisterDateTimeWithTimeZone |
| 49 | + # Run original `initialize_type_map` and then register `timestamp` as a |
| 50 | + # `MysqlDateTimeWithTimeZone`. |
| 51 | + # |
| 52 | + # When schema dumping, `timestamp` columns will be output as |
| 53 | + # `t.datetime_with_timezone`. |
| 54 | + def initialize_type_map(mapping) |
| 55 | + super mapping |
| 56 | + |
| 57 | + mapping.register_type(%r(timestamp)i) do |sql_type| |
| 58 | + precision = extract_precision(sql_type) |
| 59 | + ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlDateTimeWithTimeZone.new(precision: precision) |
21 | 60 | end
|
22 | 61 | end
|
23 | 62 | end
|
| 63 | + |
| 64 | + class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter |
| 65 | + prepend RegisterDateTimeWithTimeZone |
| 66 | + |
| 67 | + # Add the class `DateTimeWithTimeZone` so we can map `timestamp` to it. |
| 68 | + class MysqlDateTimeWithTimeZone < MysqlDateTime |
| 69 | + def type |
| 70 | + :datetime_with_timezone |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + # Add column type `datetime_with_timezone` so we can do this in |
| 75 | + # migrations: |
| 76 | + # |
| 77 | + # add_column(:users, :datetime_with_timezone) |
| 78 | + # |
| 79 | + NATIVE_DATABASE_TYPES[:datetime_with_timezone] = { name: 'timestamp' } |
| 80 | + end |
24 | 81 | end
|
0 commit comments