summaryrefslogtreecommitdiff
path: root/app/models/concerns/ignorable_columns.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-01 06:06:11 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-01 06:06:11 +0000
commit864475536355651a9f7caa5b1606aa5640424ec3 (patch)
tree1dc80c96ddf3f9049c4a163b4c49f052a9b1a4ad /app/models/concerns/ignorable_columns.rb
parent7ddd5846999029916b2b6d8560b5b0f02ec0f6ea (diff)
downloadgitlab-ce-864475536355651a9f7caa5b1606aa5640424ec3.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/concerns/ignorable_columns.rb')
-rw-r--r--app/models/concerns/ignorable_columns.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/models/concerns/ignorable_columns.rb b/app/models/concerns/ignorable_columns.rb
new file mode 100644
index 00000000000..744a1f0b5f3
--- /dev/null
+++ b/app/models/concerns/ignorable_columns.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+module IgnorableColumns
+ extend ActiveSupport::Concern
+
+ ColumnIgnore = Struct.new(:remove_after, :remove_with) do
+ def safe_to_remove?
+ Date.today > remove_after
+ end
+
+ def to_s
+ "(#{remove_after}, #{remove_with})"
+ end
+ end
+
+ class_methods do
+ # Ignore database columns in a model
+ #
+ # Indicate the earliest date and release we can stop ignoring the column with +remove_after+ (a date string) and +remove_with+ (a release)
+ def ignore_columns(*columns, remove_after:, remove_with:)
+ raise ArgumentError, 'Please indicate when we can stop ignoring columns with remove_after (date string YYYY-MM-DD), example: ignore_columns(:name, remove_after: \'2019-12-01\', remove_with: \'12.6\')' unless remove_after =~ Gitlab::Regex.utc_date_regex
+ raise ArgumentError, 'Please indicate in which release we can stop ignoring columns with remove_with, example: ignore_columns(:name, remove_after: \'2019-12-01\', remove_with: \'12.6\')' unless remove_with
+
+ self.ignored_columns += columns.flatten # rubocop:disable Cop/IgnoredColumns
+
+ columns.flatten.each do |column|
+ self.ignored_columns_details[column.to_sym] = ColumnIgnore.new(Date.parse(remove_after), remove_with)
+ end
+ end
+
+ alias_method :ignore_column, :ignore_columns
+
+ def ignored_columns_details
+ unless defined?(@ignored_columns_details)
+ IGNORE_COLUMN_MUTEX.synchronize do
+ @ignored_columns_details ||= superclass.try(:ignored_columns_details)&.dup || {}
+ end
+ end
+
+ @ignored_columns_details
+ end
+
+ IGNORE_COLUMN_MUTEX = Mutex.new
+ end
+end