Class: RuboCop::Cop::DevDoc::Migration::AvoidJsonColumn

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

Overview

Prefer ‘jsonb` over `json` for column types.

## Rationale ‘jsonb` stores data in a binary format that supports indexing, querying with operators (`->`, `->>`, `@>`), and is generally faster to read. Use `json` only if you need to preserve key order or exact formatting of the original JSON string — which is rare in practice.


t.json :metadata
add_column :users, :metadata, :json

✔️
t.jsonb :metadata
add_column :users, :metadata, :jsonb

Examples:

# bad
t.json :metadata
add_column :users, :metadata, :json

# good
t.jsonb :metadata
add_column :users, :metadata, :jsonb

Constant Summary collapse

MSG =
'Use `jsonb` instead of `json`. `jsonb` supports indexing and is faster to read.'.freeze
RESTRICT_ON_SEND =
%i[json add_column].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  if node.method_name == :add_column
    check_add_column(node)
  else
    add_offense(node.loc.selector) { |c| c.replace(node.loc.selector, 'jsonb') }
  end
end