From 40f10431d908feffb6196e649a0def64b6eb5e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20Duch=C3=AAne-Savard?= <86625150+mduchenesavard@users.noreply.github.com> Date: Mon, 10 Jan 2022 10:09:48 -0500 Subject: [PATCH] PLTF-826 - Added Cop `Migration/UseChangeTableBulk` (#7) * add bulk_change_table fix cop name default.yml fix rubocop offenses * change name to avoid collision & change default change name in changelog --- CHANGELOG.md | 1 + config/default.yml | 6 ++ .../cop/migration/use_change_table_bulk.rb | 30 +++++++++ .../migration/use_change_table_bulk_spec.rb | 63 +++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 lib/rubocop/cop/migration/use_change_table_bulk.rb create mode 100644 spec/rubocop/cop/migration/use_change_table_bulk_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index c670742..5827363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ # main +* Added Cop `Migration/UseChangeTableBulk` ([#7](https://github.com/petalmd/rubocop-petal/pull/7)) * Added cop `Grape/HelpersIncludeModule` ([#1](https://github.com/petalmd/rubocop-petal/pull/1)) # v0.1.0 diff --git a/config/default.yml b/config/default.yml index 6345ebb..c1605e3 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1,3 +1,9 @@ +Migration/UseChangeTableBulk: + Description: 'Enforces the use of option `bulk: true` with `change_table`' + Enabled: true + Include: + - db/migrate/** + Grape/HelpersIncludeModule: Description: 'Prevent using helpers with block to include module' Enabled: true diff --git a/lib/rubocop/cop/migration/use_change_table_bulk.rb b/lib/rubocop/cop/migration/use_change_table_bulk.rb new file mode 100644 index 0000000..eeca68d --- /dev/null +++ b/lib/rubocop/cop/migration/use_change_table_bulk.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Migration + # Use `bulk: true` with `change_table`. + # # bad + # + # change_table :my_table + # + # # good + # + # change_table :my_table, bulk: true + class UseChangeTableBulk < Base + MSG = 'Use `change_table` with `bulk: true`.' + RESTRICT_ON_SEND = %i[change_table].freeze + + def_node_matcher :use_bulk?, <<~PATTERN + (send nil? :change_table _ (hash <(pair (sym :bulk) true) ...>) ...) + PATTERN + + def on_send(node) + return if use_bulk?(node) + + add_offense(node) + end + end + end + end +end diff --git a/spec/rubocop/cop/migration/use_change_table_bulk_spec.rb b/spec/rubocop/cop/migration/use_change_table_bulk_spec.rb new file mode 100644 index 0000000..52d8f31 --- /dev/null +++ b/spec/rubocop/cop/migration/use_change_table_bulk_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::Migration::UseChangeTableBulk, :config do + context 'without change_table' do + it 'does not register an offense' do + expect_no_offenses <<~RUBY + class MyMigration < ActiveRecord::Migration[5.2] + def up + my_variable = :test + some_function() + MyClass.stuff() + end + end + RUBY + end + end + + context 'with change_table' do + context 'when using bulk: true' do + it 'does not register an offense' do + expect_no_offenses <<~RUBY + class MyMigration < ActiveRecord::Migration[5.2] + def up + change_table :users, some_other_param: 'stuff', bulk: true, another_param: :some_sym do + do_stuff() + end + end + end + RUBY + end + end + + context 'when using bulk: false' do + it 'registers an offense' do + expect_offense <<~RUBY + class MyMigration < ActiveRecord::Migration[5.2] + def up + change_table :users, some_other_param: 'stuff', bulk: false, another_param: :some_sym do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `change_table` with `bulk: true`. + do_stuff() + end + end + end + RUBY + end + end + + context 'when bulk is omitted' do + it 'registers an offense' do + expect_offense <<~RUBY + class MyMigration < ActiveRecord::Migration[5.2] + def up + change_table :users, some_other_param: 'stuff', another_param: :some_sym do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `change_table` with `bulk: true`. + do_stuff() + end + end + end + RUBY + end + end + end +end