Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Migration/StandaloneAddReference #54

Merged
merged 8 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# main

* Added cop `Migration/StandaloneAddReference` ([#54](https://github.com/petalmd/rubocop-petal/pull/54))
* Update `Migration/ChangeTableReferences` on send alias and message to handle removing references. ([#55](https://github.com/petalmd/rubocop-petal/pull/55))

# v1.1.2 (2023-05-30)
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Migration/SchemaStatementsMethods:
Include:
- db/migrate/**

Migration/StandaloneAddReference:
Description: 'Prevent using `add_reference/belongs_to` outside of a change_table.'
Enabled: true
Include:
- db/migrate/**

RSpec/AuthenticatedAs:
Description: 'Suggest to use authenticated_as instead of legacy api_key.'
Enabled: true
Expand Down
32 changes: 32 additions & 0 deletions lib/rubocop/cop/migration/standalone_add_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Migration
# Prevent using `add_reference` and `remove_reference` outside of
# a `change_table` block. `add_reference` create multiples `ALTER TABLE`
# statements. Using `change_table` with `bulk: true` is more efficient.
#
# # bad
# add_reference :products, :user, foreign_key: true
#
# # good
# change_table :products, bulk: true do |t|
# t.bigint :user_id, null: false
# t.index :user_id
# t.foreign_key :users, column: :user_id
# end
class StandaloneAddReference < Base
MSG = 'Modifying references must be done in a change_table block.'

RESTRICT_ON_SEND = %i[add_reference belongs_to remove_reference remove_belongs_to].freeze

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce que peut-être ici on doit ajouter le add_belongs_to et supprimer le belongs_to parce que seulement le belongs_to ne déclenche pas le cop si on ajoute le add_belongs_to.

@Bhacaz

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!


def on_send(node)
reference_method = node.source_range.with(end_pos: node.child_nodes.first.source_range.begin_pos - 1)

add_offense(reference_method)
end
end
end
end
end
36 changes: 36 additions & 0 deletions spec/rubocop/cop/migration/standalone_add_reference_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Migration::StandaloneAddReference, :config do
it 'registers an offense when modifing a references outside a change_table' do
expect_offense(<<~RUBY)
add_reference :products, :user
^^^^^^^^^^^^^ Modifying references must be done in a change_table block.
RUBY

expect_offense(<<~RUBY)
add_reference :products, :user, index: true
^^^^^^^^^^^^^ Modifying references must be done in a change_table block.
RUBY

expect_offense(<<~RUBY)
belongs_to :products, :user
^^^^^^^^^^ Modifying references must be done in a change_table block.
RUBY

expect_offense(<<~RUBY)
remove_reference :products, :user
^^^^^^^^^^^^^^^^ Modifying references must be done in a change_table block.
RUBY

expect_offense(<<~RUBY)
remove_belongs_to :products, :user
^^^^^^^^^^^^^^^^^ Modifying references must be done in a change_table block.
RUBY
end

it 'does not register an offense when not calling modifying references methods' do
expect_no_offenses(<<~RUBY)
add_index :users, :user_id
RUBY
end
end