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

Error occurred with Rails/NotNullColumn cop #997

Closed
fidalgo opened this issue May 4, 2023 · 1 comment
Closed

Error occurred with Rails/NotNullColumn cop #997

fidalgo opened this issue May 4, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@fidalgo
Copy link
Contributor

fidalgo commented May 4, 2023

After updating rubocop and friends I start having this error:

An error occurred while Rails/NotNullColumn cop was inspecting db/migrate/20220208103203_add_widget_type_to_widgets.registrations_component.rb:27:8.


Expected behavior

Report errors or corrections

Actual behavior

error occurred while Rails/NotNullColumn cop was inspecting db/migrate/20220208103203_add_widget_type_to_widgets.registrations_component.rb:27:8. of this migration:

# typed: false
# frozen_string_literal: true

# This migration comes from registrations_component (originally 20220207163412)
class AddWidgetTypeToWidgets < ActiveRecord::Migration[6.1]
  def change
    add_enum_column(:registrations_component_widgets, :widget_type, %i[regular onsite])
  end

  private

  def add_enum_column(table_name, column_name, values, default: nil, type_name: nil)
    type_name ||= [table_name, :enum, column_name].join('_')
    default ||= values.first

    reversible do |migration|
      migration.up do
        ActiveRecord::Base.connection.execute <<~SQL
          DO $$ BEGIN
            IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = '#{type_name}') THEN
              CREATE TYPE #{type_name} AS
              ENUM (#{values.map { |val| "'#{val}'" }.join(', ')});
            END IF;
          END $$;
        SQL

        add_column(table_name, column_name, type_name, default: default)
      end

      migration.down do
        remove_column table_name, column_name

        ActiveRecord::Base.connection.execute <<~SQL
          DO $$ BEGIN
            DROP TYPE IF EXISTS #{type_name};
          END $$;
        SQL
      end
    end
  end
end

and the backtrace:

undefined method `value' for s(:lvar, :type_name):RuboCop::AST::Node
/home/fidalgo/.local/share/rtx/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/not_null_column.rb:48:in `block in check_add_column'
/home/fidalgo/.local/share/rtx/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/not_null_column.rb:36:in `add_not_null_column?'
/home/fidalgo/.local/share/rtx/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/not_null_column.rb:47:in `check_add_column'
/home/fidalgo/.local/share/rtx/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/not_null_column.rb:40:in `on_send'
/home/fidalgo/.local/share/rtx/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.50.2/lib/rubocop/cop/commissioner.rb:143:in `public_send'

Steps to reproduce the problem

If you copy that code to a migration and run rubocop on that file the error will pop up.

RuboCop version

1.50.2 (using Parser 3.2.2.1, rubocop-ast 1.28.1, running on ruby 3.2.0) [x86_64-linux]
  - rubocop-graphql 1.1.1
  - rubocop-performance 1.17.1
  - rubocop-rails 2.19.1
  - rubocop-rspec 2.20.0
  - rubocop-sorbet 0.7.0

Also, on another project, with the same migration:

1.48.1 (using Parser 3.2.1.1, rubocop-ast 1.28.0, running on ruby 3.2.1) [x86_64-linux]
  - rubocop-minitest 0.30.0
  - rubocop-performance 1.16.0
  - rubocop-rails 2.18.0
@fidalgo
Copy link
Contributor Author

fidalgo commented May 4, 2023

I have created a test scenario, that will trigger this error fidalgo#1

fidalgo added a commit to fidalgo/rubocop-rails that referenced this issue May 4, 2023
At the NotNullColumn if a method is passed as arguments for `add_column`
an error is trigger.
In this commit we check if the argument is a method and return it's name
as a String.

Fixes rubocop#997
fidalgo added a commit to fidalgo/rubocop-rails that referenced this issue May 4, 2023
At the NotNullColumn cop if a method is passed as arguments for `add_column`
an error is triggerred:
```
NoMethodError:
  undefined method `value' for s(:send, nil, :string):RuboCop::AST::SendNode
```

In this commit we check if the argument is a method and when positive we return it's name
as a String.
@koic koic added the bug Something isn't working label May 4, 2023
fidalgo added a commit to fidalgo/rubocop-rails that referenced this issue May 4, 2023
At the NotNullColumn cop if a method or variable is passed as arguments for `add_column`
an error is triggerred:
```
NoMethodError:
  undefined method `value' for s(:send, nil, :string):RuboCop::AST::SendNode
```

In this commit we an extra check to ensure the type has  the
`value` method implemented, before invoking it.
@koic koic closed this as completed in 7af820e May 4, 2023
koic added a commit that referenced this issue May 4, 2023
[Fix #997] Allow NotNullColumn to work with method calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants