Fix MySQL schema dump for `timestamp`
This commit is contained in:
parent
b367b780eb
commit
ec7b3a8d5c
|
@ -45,11 +45,37 @@ if Gitlab::Database.postgresql?
|
|||
elsif Gitlab::Database.mysql?
|
||||
require 'active_record/connection_adapters/mysql2_adapter'
|
||||
|
||||
module ActiveRecord
|
||||
module ConnectionAdapters
|
||||
class AbstractMysqlAdapter
|
||||
NATIVE_DATABASE_TYPES[:datetime_with_timezone] = { name: 'timestamp' }
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue