diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-06-13 12:59:21 +0000 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-06-13 12:59:21 +0000 |
commit | 44b806d0fe3f37e2739bccf6f21bb7ed8caccef3 (patch) | |
tree | 34d9f2a4932c7eca3f17e3ffe18d4d4139adc8f1 /config | |
parent | 1039f101c9044be9d898ee1cf3fa31c67e48f349 (diff) | |
parent | bc00806a4eec785068671f2c995febe01682c2d0 (diff) | |
download | gitlab-ce-44b806d0fe3f37e2739bccf6f21bb7ed8caccef3.tar.gz |
Merge branch '32054-rails-should-use-timestamptz-database-type-for-postgresql' into 'master'
Add database helpers 'add_timestamps_with_timezone' and 'timestamps_with_timezone'
Closes #32054
See merge request !11229
Diffstat (limited to 'config')
-rw-r--r-- | config/initializers/active_record_data_types.rb | 24 | ||||
-rw-r--r-- | config/initializers/active_record_table_definition.rb | 24 |
2 files changed, 48 insertions, 0 deletions
diff --git a/config/initializers/active_record_data_types.rb b/config/initializers/active_record_data_types.rb new file mode 100644 index 00000000000..beb97c6fce0 --- /dev/null +++ b/config/initializers/active_record_data_types.rb @@ -0,0 +1,24 @@ +# ActiveRecord custom data type for storing datetimes with timezone information. +# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11229 + +if Gitlab::Database.postgresql? + require 'active_record/connection_adapters/postgresql_adapter' + + module ActiveRecord + module ConnectionAdapters + class PostgreSQLAdapter + NATIVE_DATABASE_TYPES.merge!(datetime_with_timezone: { name: 'timestamptz' }) + end + end + end +elsif Gitlab::Database.mysql? + require 'active_record/connection_adapters/mysql2_adapter' + + module ActiveRecord + module ConnectionAdapters + class AbstractMysqlAdapter + NATIVE_DATABASE_TYPES.merge!(datetime_with_timezone: { name: 'timestamp' }) + end + end + end +end diff --git a/config/initializers/active_record_table_definition.rb b/config/initializers/active_record_table_definition.rb new file mode 100644 index 00000000000..4f59e35f4da --- /dev/null +++ b/config/initializers/active_record_table_definition.rb @@ -0,0 +1,24 @@ +# ActiveRecord custom method definitions with timezone information. +# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11229 + +require 'active_record/connection_adapters/abstract/schema_definitions' + +# Appends columns `created_at` and `updated_at` to a table. +# +# It is used in table creation like: +# create_table 'users' do |t| +# t.timestamps_with_timezone +# end +module ActiveRecord + module ConnectionAdapters + class TableDefinition + def timestamps_with_timezone(**options) + options[:null] = false if options[:null].nil? + + [:created_at, :updated_at].each do |column_name| + column(column_name, :datetime_with_timezone, options) + end + end + end + end +end |