Skip to content

Commit ec7b3a8

Browse files
committed
Fix MySQL schema dump for timestamp
1 parent b367b78 commit ec7b3a8

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

config/initializers/active_record_data_types.rb

+30-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,37 @@ class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
4545
elsif Gitlab::Database.mysql?
4646
require 'active_record/connection_adapters/mysql2_adapter'
4747

48-
module ActiveRecord
49-
module ConnectionAdapters
50-
class AbstractMysqlAdapter
51-
NATIVE_DATABASE_TYPES[: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)
5260
end
5361
end
5462
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
5581
end

0 commit comments

Comments
 (0)