diff options
author | Peter Leitzen <pleitzen@gitlab.com> | 2019-09-11 19:23:42 +0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2019-09-11 19:23:42 +0300 |
commit | 50c647af51ae733abb96b84a5169d40b3c8c59be (patch) | |
tree | 13e059a9af8ddad9d1c5d8e312be17629367c9cb /lib | |
parent | 0abc902576a755355b1daf75e19d1f37c6ffb5ff (diff) |
Add rake task `db:obsolete_ignored_columns`
Show a list of obsolete `ignored_columns`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/database/obsolete_ignored_columns.rb | 38 | ||||
-rw-r--r-- | lib/tasks/db_obsolete_ignored_columns.rake | 21 |
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/gitlab/database/obsolete_ignored_columns.rb b/lib/gitlab/database/obsolete_ignored_columns.rb new file mode 100644 index 00000000000..6266b6a4b65 --- /dev/null +++ b/lib/gitlab/database/obsolete_ignored_columns.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Gitlab + module Database + # Checks which `ignored_columns` can be safely removed by scanning + # the current schema for all `ApplicationRecord` descendants. + class ObsoleteIgnoredColumns + def initialize(base = ApplicationRecord) + @base = base + end + + def execute + @base.descendants.map do |klass| + next if klass.abstract_class? + + safe_to_remove = ignored_columns_safe_to_remove_for(klass) + next if safe_to_remove.empty? + + [klass.name, safe_to_remove] + end.compact.sort_by(&:first) + end + + private + + def ignored_columns_safe_to_remove_for(klass) + ignored = klass.ignored_columns.map(&:to_s) + + return [] if ignored.empty? + + schema = klass.connection.schema_cache.columns_hash(klass.table_name) + existing = schema.values.map(&:name) + + used = ignored & existing + ignored - used + end + end + end +end diff --git a/lib/tasks/db_obsolete_ignored_columns.rake b/lib/tasks/db_obsolete_ignored_columns.rake new file mode 100644 index 00000000000..184e407f28c --- /dev/null +++ b/lib/tasks/db_obsolete_ignored_columns.rake @@ -0,0 +1,21 @@ +desc 'Show a list of obsolete `ignored_columns`' +task 'db:obsolete_ignored_columns' => :environment do + list = Gitlab::Database::ObsoleteIgnoredColumns.new.execute + + if list.empty? + puts 'No obsolete `ignored_columns` found.' + else + puts 'The following `ignored_columns` are obsolete and can be removed:' + + list.each do |name, ignored_columns| + puts "- #{name}: #{ignored_columns.join(', ')}" + end + + puts <<~TEXT + + WARNING: Removing columns is tricky because running GitLab processes may still be using the columns. + + See also https://docs.gitlab.com/ee/development/what_requires_downtime.html#dropping-columns + TEXT + end +end |