Commit b367b780 authored by Michael Kozono's avatar Michael Kozono

Fix PostgreSQL schema dump for `timestamptz`

parent 0a863c8b
...@@ -4,12 +4,43 @@ ...@@ -4,12 +4,43 @@
if Gitlab::Database.postgresql? if Gitlab::Database.postgresql?
require 'active_record/connection_adapters/postgresql_adapter' require 'active_record/connection_adapters/postgresql_adapter'
module ActiveRecord module ActiveRecord::ConnectionAdapters::PostgreSQL::OID
module ConnectionAdapters # Add the class `DateTimeWithTimeZone` so we can map `timestamptz` to it.
class PostgreSQLAdapter class DateTimeWithTimeZone < DateTime
NATIVE_DATABASE_TYPES.merge!(datetime_with_timezone: { name: 'timestamptz' }) 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
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 end
elsif Gitlab::Database.mysql? elsif Gitlab::Database.mysql?
require 'active_record/connection_adapters/mysql2_adapter' require 'active_record/connection_adapters/mysql2_adapter'
...@@ -17,7 +48,7 @@ elsif Gitlab::Database.mysql? ...@@ -17,7 +48,7 @@ elsif Gitlab::Database.mysql?
module ActiveRecord module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
class AbstractMysqlAdapter class AbstractMysqlAdapter
NATIVE_DATABASE_TYPES.merge!(datetime_with_timezone: { name: 'timestamp' }) NATIVE_DATABASE_TYPES[:datetime_with_timezone] = { name: 'timestamp' }
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment