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

@@ -208,6 +208,9 @@ Performance/Sum:
Rails/ActiveRecordCallbacksOrder:
Enabled: true
Rails/AddColumnIndex:
Enabled: true
Rails/CreateTableWithTimestamps:
Enabled: true
Exclude:

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

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_05_19_115700) do
ActiveRecord::Schema.define(version: 2021_08_11_195800) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
@@ -155,6 +155,9 @@ ActiveRecord::Schema.define(version: 2021_05_19_115700) do
t.integer "heading_id"
t.index ["ballot_id", "investment_id"], name: "index_budget_ballot_lines_on_ballot_id_and_investment_id", unique: true
t.index ["ballot_id"], name: "index_budget_ballot_lines_on_ballot_id"
t.index ["budget_id"], name: "index_budget_ballot_lines_on_budget_id"
t.index ["group_id"], name: "index_budget_ballot_lines_on_group_id"
t.index ["heading_id"], name: "index_budget_ballot_lines_on_heading_id"
t.index ["investment_id"], name: "index_budget_ballot_lines_on_investment_id"
end
@@ -274,8 +277,12 @@ ActiveRecord::Schema.define(version: 2021_05_19_115700) do
t.integer "original_heading_id"
t.index ["administrator_id"], name: "index_budget_investments_on_administrator_id"
t.index ["author_id"], name: "index_budget_investments_on_author_id"
t.index ["budget_id"], name: "index_budget_investments_on_budget_id"
t.index ["community_id"], name: "index_budget_investments_on_community_id"
t.index ["group_id"], name: "index_budget_investments_on_group_id"
t.index ["heading_id"], name: "index_budget_investments_on_heading_id"
t.index ["incompatible"], name: "index_budget_investments_on_incompatible"
t.index ["selected"], name: "index_budget_investments_on_selected"
t.index ["tsv"], name: "index_budget_investments_on_tsv", using: :gin
end
@@ -424,6 +431,7 @@ ActiveRecord::Schema.define(version: 2021_05_19_115700) do
t.index ["cached_votes_total"], name: "index_comments_on_cached_votes_total"
t.index ["cached_votes_up"], name: "index_comments_on_cached_votes_up"
t.index ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type"
t.index ["confidence_score"], name: "index_comments_on_confidence_score"
t.index ["hidden_at"], name: "index_comments_on_hidden_at"
t.index ["user_id"], name: "index_comments_on_user_id"
t.index ["valuation"], name: "index_comments_on_valuation"
@@ -570,6 +578,7 @@ ActiveRecord::Schema.define(version: 2021_05_19_115700) do
t.string "district_code"
t.integer "poll_officer_id"
t.integer "year_of_birth"
t.index ["poll_officer_id"], name: "index_failed_census_calls_on_poll_officer_id"
t.index ["user_id"], name: "index_failed_census_calls_on_user_id"
end
@@ -1203,6 +1212,7 @@ ActiveRecord::Schema.define(version: 2021_05_19_115700) do
t.integer "related_id"
t.tsvector "tsv"
t.index ["budget_id"], name: "index_polls_on_budget_id", unique: true
t.index ["geozone_restricted"], name: "index_polls_on_geozone_restricted"
t.index ["related_type", "related_id"], name: "index_polls_on_related_type_and_related_id"
t.index ["starts_at", "ends_at"], name: "index_polls_on_starts_at_and_ends_at"
end
@@ -1283,6 +1293,7 @@ ActiveRecord::Schema.define(version: 2021_05_19_115700) do
t.index ["geozone_id"], name: "index_proposals_on_geozone_id"
t.index ["hidden_at"], name: "index_proposals_on_hidden_at"
t.index ["hot_score"], name: "index_proposals_on_hot_score"
t.index ["selected"], name: "index_proposals_on_selected"
t.index ["tsv"], name: "index_proposals_on_tsv", using: :gin
end
@@ -1595,7 +1606,9 @@ ActiveRecord::Schema.define(version: 2021_05_19_115700) do
t.boolean "recommended_debates", default: true
t.boolean "recommended_proposals", default: true
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["date_of_birth"], name: "index_users_on_date_of_birth"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["gender"], name: "index_users_on_gender"
t.index ["geozone_id"], name: "index_users_on_geozone_id"
t.index ["hidden_at"], name: "index_users_on_hidden_at"
t.index ["password_changed_at"], name: "index_users_on_password_changed_at"