Class: RuboCop::Cop::Migration::Jsonb

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/migration/jsonb.rb

Overview

Prefer ‘jsonb` to `json`.

In PostgreSQL, there is no equality operator for the json column type, which can cause errors for existing ‘SELECT DISTINCT` queries in your application.

Examples:

# bad
add_column :users, :properties, :json

# good
add_column :users, :properties, :jsonb

Constant Summary collapse

MSG =
'Prefer `jsonb` to `json`.'
RESTRICT_ON_SEND =
%i[
  add_column
  change
  change_column
  json
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/migration/jsonb.rb', line 34

def on_send(node)
  json_range = json_range_from_target_send_node(node)
  return unless json_range

  add_offense(json_range) do |corrector|
    corrector.replace(json_range, 'jsonb')
  end
end