Skip to content

Add Rubocop to enforce factories for new tables

Nailia Iskhakova requested to merge 444461-add-rubocop-for-factories into master

What does this MR do and why?

Adding Rubocop to enforce factories for new tables.The cop works by scanning code for create_table calls, which are typically found in Rails migration files. For each create_table call, it checks if a corresponding factory file exists in the spec/factories/**/* directory. If a factory file does not exist for a given table, the cop registers an offense and reports an error message.

Related to Add Rubocop to enforce factories for new tables (#444461 - closed)

Why

Ensuring that each table has corresponding Factory would allow to seed database for all possible tables which in turn will help with upgrade testing. See original proposal from Database team gitlab-org/quality/quality-engineering/team-tasks#1919 (comment 1627260665) and how FactoryBot is used for seeding DB for multi-version upgrade - !144631 (merged) / Expand data seeding configuration for multi-ver... (gitlab-org/quality/quality-engineering/team-tasks#2354 - closed)

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

  1. In GDK gitlab folder run the custom Rubocop check:
    bundle exec rubocop --only Migration/EnsureFactoryForTable db
Documentation for good and bad examples
      # Checks for `create_table` calls without a corresponding factory.
      #
      # @example
      #
      #   # bad
      #
      #   create_table :users do |t|
      #     t.string :name
      #     t.timestamps
      #   end
      #   # spec/factories/users.rb does not exist
      #
      # @example
      #
      #   # good
      #
      #   create_table :users do |t|
      #     t.string :name
      #     t.timestamps
      #   end
      #   # spec/factories/users.rb exists

Testing

Edited by Nailia Iskhakova

Merge request reports