Add Rails/AddColumnIndex rubocop rule

The `column` method in ActiveRecord::ConnectionAdapters::TableDefinition
supports adding the `index:` option. The documentation says:

> Instantiates a new column for the table. See connection.add_column for
> available options.
>
> Additional options are:
>
> :index - Create an index for the column. Can be either true or an
> options hash.

So basically the `connection.add_column` method silently ignores the
`index:` option, and whenever we intended to create an index this way,
we didn't.

We're creating a new migration where we properly add the indexes that
weren't added when we intended to.

Thanks to the rubocop-rails team, who added this cop in version 2.11.0
and helped us notice this bug.
This commit is contained in:
Javi Martín
2021-08-11 05:28:35 +02:00
parent 5b6dc9d7ff
commit 5abd0466e2
12 changed files with 47 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
class AddConfidenceScoreToComments < ActiveRecord::Migration[4.2]
def change
add_column :comments, :confidence_score, :integer, index: true
add_column :comments, :confidence_score, :integer
end
end

View File

@@ -1,6 +1,6 @@
class AddGenreAndDobToUsers < ActiveRecord::Migration[4.2]
def change
add_column :users, :genre, :string, index: true, limit: 10
add_column :users, :date_of_birth, :datetime, index: true
add_column :users, :genre, :string, limit: 10
add_column :users, :date_of_birth, :datetime
end
end

View File

@@ -1,7 +1,7 @@
class DesnormalizeBallotLine < ActiveRecord::Migration[4.2]
def change
add_column :budget_ballot_lines, :budget_id, :integer, index: true
add_column :budget_ballot_lines, :group_id, :integer, index: true
add_column :budget_ballot_lines, :heading_id, :integer, index: true
add_column :budget_ballot_lines, :budget_id, :integer
add_column :budget_ballot_lines, :group_id, :integer
add_column :budget_ballot_lines, :heading_id, :integer
end
end

View File

@@ -1,6 +1,6 @@
class DenormalizeInvestments < ActiveRecord::Migration[4.2]
def change
add_column :budget_investments, :budget_id, :integer, index: true
add_column :budget_investments, :group_id, :integer, index: true
add_column :budget_investments, :budget_id, :integer
add_column :budget_investments, :group_id, :integer
end
end

View File

@@ -1,5 +1,5 @@
class AddSelectedToBudgetInvestment < ActiveRecord::Migration[4.2]
def change
add_column :budget_investments, :selected, :bool, default: false, index: true
add_column :budget_investments, :selected, :bool, default: false
end
end

View File

@@ -1,5 +1,5 @@
class AddGeozoneRestrictedToPolls < ActiveRecord::Migration[4.2]
def change
add_column :polls, :geozone_restricted, :boolean, default: false, index: true
add_column :polls, :geozone_restricted, :boolean, default: false
end
end

View File

@@ -1,6 +1,6 @@
class AddOfficerIdToFailedCensusCalls < ActiveRecord::Migration[4.2]
def change
add_column :failed_census_calls, :poll_officer_id, :integer, index: true
add_column :failed_census_calls, :poll_officer_id, :integer
add_foreign_key :failed_census_calls, :poll_officers
end
end

View File

@@ -1,5 +1,5 @@
class AddIncompatibleToBudgetInvestments < ActiveRecord::Migration[4.2]
def change
add_column :budget_investments, :incompatible, :bool, default: false, index: true
add_column :budget_investments, :incompatible, :bool, default: false
end
end

View File

@@ -1,5 +1,5 @@
class AddSelectedToProposal < ActiveRecord::Migration[4.2]
def change
add_column :proposals, :selected, :bool, default: false, index: true
add_column :proposals, :selected, :bool, default: false
end
end

View File

@@ -0,0 +1,17 @@
class AddMissingIndexes < ActiveRecord::Migration[5.2]
def change
add_index :comments, :confidence_score
add_index :users, :gender
add_index :users, :date_of_birth
add_index :budget_ballot_lines, :budget_id
add_index :budget_ballot_lines, :group_id
add_index :budget_ballot_lines, :heading_id
add_index :budget_investments, :budget_id
add_index :budget_investments, :group_id
add_index :budget_investments, :selected
add_index :polls, :geozone_restricted
add_index :failed_census_calls, :poll_officer_id
add_index :budget_investments, :incompatible
add_index :proposals, :selected
end
end