Commit b7ebf8a9 authored by dineshpanda's avatar dineshpanda Committed by Stan Hu

Remove dependency on IgnorableColumn concern

`IgnorableColumn` module in `app/models/concerns/ignorable_column.rb`
which is used in models to ignore dealing with specified columns.

In Rails 5 with this [pull
request](https://github.com/rails/rails/pull/21720),
`self.ignored_columns` was introduced to do this job.

For example, in app/models/user.rb:

```ruby
ignore_column :external_email
ignore_column :email_provider
```

can be changed to:

```ruby
self.ignored_columns = %i[external_email email_provider]
```
parent 48dc83a6
...@@ -4,7 +4,6 @@ class ApplicationSetting < ApplicationRecord ...@@ -4,7 +4,6 @@ class ApplicationSetting < ApplicationRecord
include CacheableAttributes include CacheableAttributes
include CacheMarkdownField include CacheMarkdownField
include TokenAuthenticatable include TokenAuthenticatable
include IgnorableColumn
include ChronicDurationAttribute include ChronicDurationAttribute
add_authentication_token_field :runners_registration_token, encrypted: -> { Feature.enabled?(:application_settings_tokens_optional_encryption, default_enabled: true) ? :optional : :required } add_authentication_token_field :runners_registration_token, encrypted: -> { Feature.enabled?(:application_settings_tokens_optional_encryption, default_enabled: true) ? :optional : :required }
...@@ -32,12 +31,14 @@ class ApplicationSetting < ApplicationRecord ...@@ -32,12 +31,14 @@ class ApplicationSetting < ApplicationRecord
serialize :repository_storages # rubocop:disable Cop/ActiveRecordSerialize serialize :repository_storages # rubocop:disable Cop/ActiveRecordSerialize
serialize :asset_proxy_whitelist, Array # rubocop:disable Cop/ActiveRecordSerialize serialize :asset_proxy_whitelist, Array # rubocop:disable Cop/ActiveRecordSerialize
ignore_column :koding_url self.ignored_columns = %i[
ignore_column :koding_enabled clientside_sentry_dsn
ignore_column :sentry_enabled clientside_sentry_enabled
ignore_column :sentry_dsn koding_enabled
ignore_column :clientside_sentry_enabled koding_url
ignore_column :clientside_sentry_dsn sentry_dsn
sentry_enabled
]
cache_markdown_field :sign_in_text cache_markdown_field :sign_in_text
cache_markdown_field :help_page_text cache_markdown_field :help_page_text
......
...@@ -11,19 +11,20 @@ module Ci ...@@ -11,19 +11,20 @@ module Ci
include ObjectStorage::BackgroundMove include ObjectStorage::BackgroundMove
include Presentable include Presentable
include Importable include Importable
include IgnorableColumn
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
include Deployable include Deployable
include HasRef include HasRef
BuildArchivedError = Class.new(StandardError) BuildArchivedError = Class.new(StandardError)
ignore_column :commands self.ignored_columns = %i[
ignore_column :artifacts_file artifacts_file
ignore_column :artifacts_metadata artifacts_file_store
ignore_column :artifacts_file_store artifacts_metadata
ignore_column :artifacts_metadata_store artifacts_metadata_store
ignore_column :artifacts_size artifacts_size
commands
]
belongs_to :project, inverse_of: :builds belongs_to :project, inverse_of: :builds
belongs_to :runner belongs_to :runner
......
...@@ -4,7 +4,6 @@ module Ci ...@@ -4,7 +4,6 @@ module Ci
class Runner < ApplicationRecord class Runner < ApplicationRecord
extend Gitlab::Ci::Model extend Gitlab::Ci::Model
include Gitlab::SQL::Pattern include Gitlab::SQL::Pattern
include IgnorableColumn
include RedisCacheable include RedisCacheable
include ChronicDurationAttribute include ChronicDurationAttribute
include FromUnion include FromUnion
...@@ -36,7 +35,7 @@ module Ci ...@@ -36,7 +35,7 @@ module Ci
FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level maximum_timeout_human_readable].freeze FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level maximum_timeout_human_readable].freeze
ignore_column :is_shared self.ignored_columns = %i[is_shared]
has_many :builds has_many :builds
has_many :runner_projects, inverse_of: :runner, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :runner_projects, inverse_of: :runner, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
......
# frozen_string_literal: true
# Module that can be included into a model to make it easier to ignore database
# columns.
#
# Example:
#
# class User < ApplicationRecord
# include IgnorableColumn
#
# ignore_column :updated_at
# end
#
module IgnorableColumn
extend ActiveSupport::Concern
class_methods do
def columns
super.reject { |column| ignored_columns.include?(column.name) }
end
def ignored_columns
@ignored_columns ||= Set.new
end
def ignore_column(*names)
ignored_columns.merge(names.map(&:to_s))
end
end
end
# frozen_string_literal: true # frozen_string_literal: true
class DeployKey < Key class DeployKey < Key
include IgnorableColumn
include FromUnion include FromUnion
has_many :deploy_keys_projects, inverse_of: :deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :deploy_keys_projects, inverse_of: :deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
...@@ -11,7 +10,7 @@ class DeployKey < Key ...@@ -11,7 +10,7 @@ class DeployKey < Key
scope :are_public, -> { where(public: true) } scope :are_public, -> { where(public: true) }
scope :with_projects, -> { includes(deploy_keys_projects: { project: [:route, :namespace] }) } scope :with_projects, -> { includes(deploy_keys_projects: { project: [:route, :namespace] }) }
ignore_column :can_push self.ignored_columns = %i[can_push]
accepts_nested_attributes_for :deploy_keys_projects accepts_nested_attributes_for :deploy_keys_projects
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
class Event < ApplicationRecord class Event < ApplicationRecord
include Sortable include Sortable
include IgnorableColumn
include FromUnion include FromUnion
default_scope { reorder(nil) } default_scope { reorder(nil) }
......
...@@ -4,7 +4,6 @@ class MergeRequestDiff < ApplicationRecord ...@@ -4,7 +4,6 @@ class MergeRequestDiff < ApplicationRecord
include Sortable include Sortable
include Importable include Importable
include ManualInverseAssociation include ManualInverseAssociation
include IgnorableColumn
include EachBatch include EachBatch
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
include ObjectStorage::BackgroundMove include ObjectStorage::BackgroundMove
......
...@@ -14,7 +14,6 @@ class Note < ApplicationRecord ...@@ -14,7 +14,6 @@ class Note < ApplicationRecord
include CacheMarkdownField include CacheMarkdownField
include AfterCommitQueue include AfterCommitQueue
include ResolvableNote include ResolvableNote
include IgnorableColumn
include Editable include Editable
include Gitlab::SQL::Pattern include Gitlab::SQL::Pattern
include ThrottledTouch include ThrottledTouch
...@@ -34,7 +33,7 @@ class Note < ApplicationRecord ...@@ -34,7 +33,7 @@ class Note < ApplicationRecord
end end
end end
ignore_column :original_discussion_id self.ignored_columns = %i[original_discussion_id]
cache_markdown_field :note, pipeline: :note, issuable_state_filter_enabled: true cache_markdown_field :note, pipeline: :note, issuable_state_filter_enabled: true
......
# frozen_string_literal: true # frozen_string_literal: true
class NotificationSetting < ApplicationRecord class NotificationSetting < ApplicationRecord
include IgnorableColumn self.ignored_columns = %i[events]
ignore_column :events
enum level: { global: 3, watch: 2, participating: 1, mention: 4, disabled: 0, custom: 5 } enum level: { global: 3, watch: 2, participating: 1, mention: 4, disabled: 0, custom: 5 }
......
...@@ -13,7 +13,6 @@ class User < ApplicationRecord ...@@ -13,7 +13,6 @@ class User < ApplicationRecord
include Sortable include Sortable
include CaseSensitivity include CaseSensitivity
include TokenAuthenticatable include TokenAuthenticatable
include IgnorableColumn
include FeatureGate include FeatureGate
include CreatedAtFilterable include CreatedAtFilterable
include BulkMemberAccessLoad include BulkMemberAccessLoad
...@@ -24,9 +23,11 @@ class User < ApplicationRecord ...@@ -24,9 +23,11 @@ class User < ApplicationRecord
DEFAULT_NOTIFICATION_LEVEL = :participating DEFAULT_NOTIFICATION_LEVEL = :participating
ignore_column :external_email self.ignored_columns = %i[
ignore_column :email_provider authentication_token
ignore_column :authentication_token email_provider
external_email
]
add_authentication_token_field :incoming_email_token, token_generator: -> { SecureRandom.hex.to_i(16).to_s(36) } add_authentication_token_field :incoming_email_token, token_generator: -> { SecureRandom.hex.to_i(16).to_s(36) }
add_authentication_token_field :feed_token add_authentication_token_field :feed_token
......
...@@ -45,15 +45,12 @@ rule. ...@@ -45,15 +45,12 @@ rule.
The first step is to ignore the column in the application code. This is The first step is to ignore the column in the application code. This is
necessary because Rails caches the columns and re-uses this cache in various necessary because Rails caches the columns and re-uses this cache in various
places. This can be done by including the `IgnorableColumn` module into the places. This can be done by defining the columns to ignore. For example, to ignore
model, followed by defining the columns to ignore. For example, to ignore
`updated_at` in the User model you'd use the following: `updated_at` in the User model you'd use the following:
```ruby ```ruby
class User < ActiveRecord::Base class User < ApplicationRecord
include IgnorableColumn self.ignored_columns = %i[updated_at]
ignore_column :updated_at
end end
``` ```
...@@ -64,8 +61,7 @@ column. Both these changes should be submitted in the same merge request. ...@@ -64,8 +61,7 @@ column. Both these changes should be submitted in the same merge request.
Once the changes from step 1 have been released & deployed you can set up a Once the changes from step 1 have been released & deployed you can set up a
separate merge request that removes the ignore rule. This merge request can separate merge request that removes the ignore rule. This merge request can
simply remove the `ignore_column` line, and the `include IgnorableColumn` line simply remove the `self.ignored_columns` line.
if no other `ignore_column` calls remain.
## Renaming Columns ## Renaming Columns
......
...@@ -9,14 +9,12 @@ module EE ...@@ -9,14 +9,12 @@ module EE
extend ActiveSupport::Concern extend ActiveSupport::Concern
prepended do prepended do
include IgnorableColumn
EMAIL_ADDITIONAL_TEXT_CHARACTER_LIMIT = 10_000 EMAIL_ADDITIONAL_TEXT_CHARACTER_LIMIT = 10_000
INSTANCE_REVIEW_MIN_USERS = 100 INSTANCE_REVIEW_MIN_USERS = 100
belongs_to :file_template_project, class_name: "Project" belongs_to :file_template_project, class_name: "Project"
ignore_column :minimum_mirror_sync_time self.ignored_columns = %i[minimum_mirror_sync_time]
validates :shared_runners_minutes, validates :shared_runners_minutes,
numericality: { greater_than_or_equal_to: 0 } numericality: { greater_than_or_equal_to: 0 }
......
...@@ -19,12 +19,13 @@ module EE ...@@ -19,12 +19,13 @@ module EE
include EE::DeploymentPlatform # rubocop: disable Cop/InjectEnterpriseEditionModule include EE::DeploymentPlatform # rubocop: disable Cop/InjectEnterpriseEditionModule
include EachBatch include EachBatch
include InsightsFeature include InsightsFeature
include IgnorableColumn
include Vulnerable include Vulnerable
ignore_column :mirror_last_update_at, self.ignored_columns = %i[
:mirror_last_successful_update_at, mirror_last_update_at
:next_execution_timestamp mirror_last_successful_update_at
next_execution_timestamp
]
before_save :set_override_pull_mirror_available, unless: -> { ::Gitlab::CurrentSettings.mirror_available } before_save :set_override_pull_mirror_available, unless: -> { ::Gitlab::CurrentSettings.mirror_available }
before_save :set_next_execution_timestamp_to_now, if: ->(project) { project.mirror? && project.mirror_changed? && project.import_state } before_save :set_next_execution_timestamp_to_now, if: ->(project) { project.mirror? && project.mirror_changed? && project.import_state }
......
...@@ -6,8 +6,7 @@ module EE ...@@ -6,8 +6,7 @@ module EE
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
prepended do prepended do
include IgnorableColumn self.ignored_columns = %i[created_at updated_at]
ignore_column :created_at, :updated_at
BACKOFF_PERIOD = 24.seconds BACKOFF_PERIOD = 24.seconds
JITTER = 6.seconds JITTER = 6.seconds
......
...@@ -18,7 +18,7 @@ module EE ...@@ -18,7 +18,7 @@ module EE
prepended do prepended do
EMAIL_OPT_IN_SOURCE_ID_GITLAB_COM = 1 EMAIL_OPT_IN_SOURCE_ID_GITLAB_COM = 1
ignore_column :support_bot self.ignored_columns = %i[support_bot]
# We aren't using the `auditor?` method for the `if` condition here # We aren't using the `auditor?` method for the `if` condition here
# because `auditor?` returns `false` when the `auditor` column is `true` # because `auditor?` returns `false` when the `auditor` column is `true`
......
# frozen_string_literal: true # frozen_string_literal: true
class ProjectRepositoryState < ApplicationRecord class ProjectRepositoryState < ApplicationRecord
include IgnorableColumn
include ShaAttribute include ShaAttribute
ignore_column :last_repository_verification_failed self.ignored_columns = %i[last_repository_verification_failed last_wiki_verification_failed]
ignore_column :last_wiki_verification_failed
sha_attribute :repository_verification_checksum sha_attribute :repository_verification_checksum
sha_attribute :wiki_verification_checksum sha_attribute :wiki_verification_checksum
......
# frozen_string_literal: true
require 'spec_helper'
describe IgnorableColumn do
let :base_class do
Class.new do
def self.columns
# This method does not have access to "double"
[
Struct.new(:name).new('id'),
Struct.new(:name).new('title'),
Struct.new(:name).new('date')
]
end
end
end
let :model do
Class.new(base_class) do
include IgnorableColumn
end
end
describe '.columns' do
it 'returns the columns, excluding the ignored ones' do
model.ignore_column(:title, :date)
expect(model.columns.map(&:name)).to eq(%w(id))
end
end
describe '.ignored_columns' do
it 'returns a Set' do
expect(model.ignored_columns).to be_an_instance_of(Set)
end
it 'returns the names of the ignored columns' do
model.ignore_column(:title, :date)
expect(model.ignored_columns).to eq(Set.new(%w(title date)))
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment