summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2017-06-21 11:19:19 -0700
committerMichael Kozono <mkozono@gmail.com>2017-06-22 09:21:23 -0700
commitec7b3a8d5cdc88ba4d494da11a3b90257e172652 (patch)
tree0376b3d0851de9a41d3d2009211dbf82113d37c1
parentb367b780eb503c0141e21194e922d0296557e277 (diff)
downloadgitlab-ce-mk-add-datetime-with-timezone-table-definition.tar.gz
Fix MySQL schema dump for `timestamp`mk-add-datetime-with-timezone-table-definition
-rw-r--r--config/initializers/active_record_data_types.rb34
1 files changed, 30 insertions, 4 deletions
diff --git a/config/initializers/active_record_data_types.rb b/config/initializers/active_record_data_types.rb
index 454d5b11902..fef591c397d 100644
--- a/config/initializers/active_record_data_types.rb
+++ b/config/initializers/active_record_data_types.rb
@@ -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