module Birdspotting::SchemaStatements

Public Instance Methods

add_column(table_name, column_name, type, options = {}) click to toggle source
Calls superclass method
# File lib/birdspotting/schema_statements.rb, line 3
def add_column(table_name, column_name, type, options = {})
  add_column_position_check(options)
  encoding_check(column_name, type, options)

  super
end
remove_column(table_name, column_name, type = nil, options = {}) click to toggle source
Calls superclass method
# File lib/birdspotting/schema_statements.rb, line 16
def remove_column(table_name, column_name, type = nil, options = {})
  remove_column_check(column_name, table_name, options)

  super(table_name, column_name, type, options)
end
rename_column(*args, **kwargs) click to toggle source
Calls superclass method
# File lib/birdspotting/schema_statements.rb, line 10
def rename_column(*args, **kwargs)
  rename_column_check(kwargs)

  super(*args)
end

Private Instance Methods

add_column_position_check(options) click to toggle source
# File lib/birdspotting/schema_statements.rb, line 31
def add_column_position_check(options)
  return if bypass_check?(options)
  return unless Birdspotting.configuration.add_column_position_check?
  return unless options[:after].nil? && options[:first].nil?

  raise Birdspotting::ColumnPositionMissingError,
        "The :after or :first option is required when adding columns"
end
bypass_check?(options = {}) click to toggle source
# File lib/birdspotting/schema_statements.rb, line 84
def bypass_check?(options = {})
  checkable_version? ||
    options.delete(:bypass_schema_statements_check) ||
    ENV.key?(Birdspotting.configuration.check_bypass_env_var)
end
checkable_version?() click to toggle source
# File lib/birdspotting/schema_statements.rb, line 90
def checkable_version?
  version &&
    Birdspotting.configuration.start_check_at_version &&
    version <= Birdspotting.configuration.start_check_at_version
end
encoding_check(column_name, type, options) click to toggle source
# File lib/birdspotting/schema_statements.rb, line 40
def encoding_check(column_name, type, options)
  return if bypass_check?(options)
  return unless Birdspotting.configuration.encoding_check?
  return unless %i[text string].include?(type)

  warn sprintf(
    Birdspotting.configuration.encoding_check_message,
    type: type,
    column_name: column_name
  )
end
model_for(table_name) click to toggle source
# File lib/birdspotting/schema_statements.rb, line 75
def model_for(table_name)
  model = ActiveRecord::Base.descendants.find { |t| t.table_name == table_name.to_s }
  model || begin
             table_name.to_s.classify.constantize
           rescue StandardError
             nil
           end
end
remove_column_check(column_name, table_name, options) click to toggle source
# File lib/birdspotting/schema_statements.rb, line 52
def remove_column_check(column_name, table_name, options)
  return if bypass_check?(options)
  return unless Birdspotting.configuration.remove_column_check?

  model = model_for(table_name)

  unless model
    raise Birdspotting::ModelNotFoundError,
          "No model for `#{table_name}` table could be found. " \
          "Is the associated model preloaded?" \
          "Call the model at the beginning of the migration to ensure it is loaded." \
          "Or use the :bypass_schema_statements_check option " \
          "if you're sure of what you are doing."
  end

  if model.columns.map(&:name).include?(column_name.to_s) # rubocop:disable Style/GuardClause
    raise Birdspotting::RemoveColumnForbiddenError,
          "`#{column_name}` column should be added to ignored_columns in `#{model.name}` model" \
          " before being removed. Use #{Birdspotting.configuration.check_bypass_env_var}" \
          " env variable if you're sure of what you are doing."
  end
end
rename_column_check(options) click to toggle source
# File lib/birdspotting/schema_statements.rb, line 24
def rename_column_check(options)
  return if bypass_check?(options)
  return unless Birdspotting.configuration.rename_column_check?

  raise Birdspotting::RenameColumnForbiddenError, Birdspotting.configuration.rename_column_message
end