Class: RuboCop::Cop::Ezcater::Migration::BigintForeignKey

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

Overview

Use ‘bigint` instead of `integer` for all foreign keys.

Examples:


# bad
create_table :foos do |t|
  t.integer :bar_id
end

# bad
create_table :foos do |t|
  t.integer :bar_id, limit: 7
end

# bad
add_column :foos, :bar_id, :integer

# bad
add_column :foos, :bar_id, :integer, limit: 7

# bad
add_reference :foos, :bar, type: :integer

# bad
add_reference :foos, :bar, type: :integer, limit: 7

# good
create_table :foos do |t|
  t.bigint :bar_id
end

# good
create_table :foos do |t|
  t.references :bar
end

# good
add_column :foos, :bar_id, :bigint

# good
add_reference :foos, :bar

Constant Summary collapse

MSG =
"Use `bigint` instead of `integer` for foreign keys. This ensures that they are capable of storing all possible values from referenced primary keys which are `bigint` or may eventually be migrated to `bigint`.\n".chomp
RESTRICT_ON_SEND =

Optimization: only call ‘on_send` for the methods in this list

i(
  integer
  references
  belongs_to
  add_column
  add_reference
  add_belongs_to
).freeze
BIGINT_BYTES =
8

Instance Method Summary collapse

Instance Method Details

#add_column_method(node) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rubocop/cop/ezcater/migration/bigint_foreign_key.rb', line 97

def_node_matcher :add_column_method, "# A call to an `add_column` method\n(send nil? :add_column\n  # Table name\n  {(sym _) (str _)}\n\n  # Column name: a symbol or string ending in _id\n  {(sym #ends_with_id?) (str #ends_with_id?)}\n\n  # Column type\n  (sym :integer)\n\n  # Optional hash that includes a limit key\n  (hash <(pair (sym :limit) (int $_)) ...>)?\n)\n"

#add_reference_method(node) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rubocop/cop/ezcater/migration/bigint_foreign_key.rb', line 115

def_node_matcher :add_reference_method, "# A call to a `add_reference` or `add_belongs_to` method\n(send nil? {:add_reference :add_belongs_to}\n  # Table name\n  {(sym _) (str _)}\n\n  # Reference name\n  {(sym _) (str _)}\n\n  # A hash that includes `type: :integer`\n  (hash <(pair (sym :type) (sym :integer)) ...>)\n)\n"

#limit_pair(node) ⇒ Object



130
131
132
# File 'lib/rubocop/cop/ezcater/migration/bigint_foreign_key.rb', line 130

def_node_search :limit_pair, "(pair (sym :limit) (int $_))\n"

#on_send(node) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rubocop/cop/ezcater/migration/bigint_foreign_key.rb', line 134

def on_send(node)
  t_integer_method(node) do |captures|
    check_integer_method(node, captures)
  end

  add_column_method(node) do |captures|
    check_integer_method(node, captures)
  end

  t_references_method(node) do
    check_reference_method(node)
  end

  add_reference_method(node) do
    check_reference_method(node)
  end
end

#t_integer_method(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/ezcater/migration/bigint_foreign_key.rb', line 69

def_node_matcher :t_integer_method, "(send\n  # Any local variable that calls an `integer` method\n  (lvar _) :integer\n\n  # Column name: symbol or string ending in _id\n  {(sym #ends_with_id?) (str #ends_with_id?)}\n\n  # Optional hash that includes a limit key\n  (hash <(pair (sym :limit) (int $_)) ...>)?\n)\n"

#t_references_method(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubocop/cop/ezcater/migration/bigint_foreign_key.rb', line 83

def_node_matcher :t_references_method, "(send\n  # Any local variable that calls a `references` or `belongs_to` method\n  (lvar _) {:references :belongs_to}\n\n  # Reference name\n  {(sym _) (str _)}\n\n  # A hash that includes `type: :integer`\n  (hash <(pair (sym :type) (sym :integer)) ...>)\n)\n"