summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-08-15 20:43:13 +0000
committerStan Hu <stanhu@gmail.com>2017-08-15 20:43:13 +0000
commit7712e34a2a355cea63be0cb5aed38676b7ae967e (patch)
treebfc2521d1e3ea6d00a902fde6a57da95284e2c9c
parent0a61262f450d03d23ea068ec4f8fdcafdcaca37e (diff)
parentd6515aa3e427b8b1944086a10357fd34b1a91515 (diff)
downloadgitlab-ce-7712e34a2a355cea63be0cb5aed38676b7ae967e.tar.gz
Merge branch '36405-fix-mysql-timestamp-columns' into 'master'
Make sure MySQL would not use CURRENT_TIMESTAMP as the default for timestamp columns Closes #36405 See merge request !13560
-rw-r--r--config/initializers/active_record_mysql_timestamp.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/config/initializers/active_record_mysql_timestamp.rb b/config/initializers/active_record_mysql_timestamp.rb
new file mode 100644
index 00000000000..af74c4ff6fb
--- /dev/null
+++ b/config/initializers/active_record_mysql_timestamp.rb
@@ -0,0 +1,30 @@
+# Make sure that MySQL won't try to use CURRENT_TIMESTAMP when the timestamp
+# column is NOT NULL. See https://gitlab.com/gitlab-org/gitlab-ce/issues/36405
+# And also: https://bugs.mysql.com/bug.php?id=75098
+# This patch was based on:
+# https://github.com/rails/rails/blob/15ef55efb591e5379486ccf53dd3e13f416564f6/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb#L34-L36
+
+if Gitlab::Database.mysql?
+ require 'active_record/connection_adapters/abstract/schema_creation'
+
+ module MySQLTimestampFix
+ def add_column_options!(sql, options)
+ # By default, TIMESTAMP columns are NOT NULL, cannot contain NULL values,
+ # and assigning NULL assigns the current timestamp. To permit a TIMESTAMP
+ # column to contain NULL, explicitly declare it with the NULL attribute.
+ # See http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
+ if sql.end_with?('timestamp') && !options[:primary_key]
+ if options[:null] != false
+ sql << ' NULL'
+ elsif options[:column].default.nil?
+ sql << ' DEFAULT 0'
+ end
+ end
+
+ super
+ end
+ end
+
+ ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation
+ .prepend(MySQLTimestampFix)
+end