djezzzl / database_consistency

The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.
MIT License
1.05k stars 46 forks source link

ActiveRecord 7.0.5 internal error #200

Closed PuntoDiGoccia closed 1 year ago

PuntoDiGoccia commented 1 year ago

I'm testing database_consistency (great project BTW) first time on my app . The report is being generated but starts with:

Hey, some checks fail with an error, please open an issue on github at https://github.com/djezzzl/database_consistency

Here's the dumped file:

<===begin===>
/usr/local/bundle/gems/activerecord-7.0.5/lib/active_record/reflection.rb:441:in `rescue in compute_class': Rails couldn't find a valid model for AssetModel association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass. (NameError)

          raise NameError, msg
          ^^^^^
    from /usr/local/bundle/gems/activerecord-7.0.5/lib/active_record/reflection.rb:432:in `compute_class'
    from /usr/local/bundle/gems/activerecord-7.0.5/lib/active_record/reflection.rb:382:in `klass'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/association_checkers/foreign_key_checker.rb:28:in `same_database?'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/association_checkers/foreign_key_checker.rb:24:in `preconditions'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/base_checker.rb:23:in `report'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/base_checker.rb:34:in `report_if_enabled?'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:23:in `block (3 levels) in check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:21:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:21:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:21:in `block (2 levels) in check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:20:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:20:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:20:in `block in check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:17:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:17:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:17:in `check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:30:in `reports'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:15:in `block in reports'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:14:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:14:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:14:in `reports'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency.rb:102:in `run'
    from /usr/local/bundle/gems/database_consistency-1.7.9/bin/database_consistency:78:in `<top (required)>'
    from /usr/local/bundle/bin/database_consistency:25:in `load'
    from /usr/local/bundle/bin/database_consistency:25:in `<top (required)>'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli/exec.rb:58:in `load'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli/exec.rb:58:in `kernel_load'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli/exec.rb:23:in `run'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli.rb:492:in `exec'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli.rb:34:in `dispatch'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli.rb:28:in `start'
    from /usr/local/bundle/gems/bundler-2.4.14/exe/bundle:37:in `block in <top (required)>'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
    from /usr/local/bundle/gems/bundler-2.4.14/exe/bundle:29:in `<top (required)>'
    from /usr/local/bundle/bin/bundle:25:in `load'
    from /usr/local/bundle/bin/bundle:25:in `<main>'
/usr/local/bundle/gems/activerecord-7.0.5/lib/active_record/inheritance.rb:259:in `compute_type': uninitialized constant Private::Asset::AssetModel (NameError)

            raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
            ^^^^^
    from /usr/local/bundle/gems/activerecord-7.0.5/lib/active_record/reflection.rb:433:in `compute_class'
    from /usr/local/bundle/gems/activerecord-7.0.5/lib/active_record/reflection.rb:382:in `klass'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/association_checkers/foreign_key_checker.rb:28:in `same_database?'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/association_checkers/foreign_key_checker.rb:24:in `preconditions'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/base_checker.rb:23:in `report'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/checkers/base_checker.rb:34:in `report_if_enabled?'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:23:in `block (3 levels) in check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:21:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:21:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:21:in `block (2 levels) in check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:20:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:20:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:20:in `block in check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:17:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:17:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/associations_processor.rb:17:in `check'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:30:in `reports'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:15:in `block in reports'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:14:in `each'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:14:in `flat_map'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency/processors/base_processor.rb:14:in `reports'
    from /usr/local/bundle/gems/database_consistency-1.7.9/lib/database_consistency.rb:102:in `run'
    from /usr/local/bundle/gems/database_consistency-1.7.9/bin/database_consistency:78:in `<top (required)>'
    from /usr/local/bundle/bin/database_consistency:25:in `load'
    from /usr/local/bundle/bin/database_consistency:25:in `<top (required)>'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli/exec.rb:58:in `load'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli/exec.rb:58:in `kernel_load'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli/exec.rb:23:in `run'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli.rb:492:in `exec'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli.rb:34:in `dispatch'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/cli.rb:28:in `start'
    from /usr/local/bundle/gems/bundler-2.4.14/exe/bundle:37:in `block in <top (required)>'
    from /usr/local/bundle/gems/bundler-2.4.14/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
    from /usr/local/bundle/gems/bundler-2.4.14/exe/bundle:29:in `<top (required)>'
    from /usr/local/bundle/bin/bundle:25:in `load'
    from /usr/local/bundle/bin/bundle:25:in `<main>'
<===end===>

We have a multi-DB setup, some entities are saved in one, other in another.

Keep it up, thanks

djezzzl commented 1 year ago

Hi @PuntoDiGoccia,

Thank you for using the gem and your kind words.

You seem to have two " broken " associations; for example, they miss "class_name" options.

Unfortunately, it's hard for me to say which associations are broken.

Probably, one is related to "Private::Asset::AssetModel." Both errors are associated with a single association. Unfortunately, the stack trace doesn't give much information about how to find that broken association.

I will implement a checker for that.

Looking at the stack trace, you will see that the issue happens in ActiveRecord (not in DatabaseConsistency).

Please let me know if this information is anyhow helpful to you.

djezzzl commented 1 year ago

Hi @PuntoDiGoccia,

I've just merged: https://github.com/djezzzl/database_consistency/pull/205

It does two things:

It was released with version 1.7.13. Please try it, and let me know if that helped you with your issue.

Have a great day!

PuntoDiGoccia commented 1 year ago

Hi @PuntoDiGoccia,

I've just merged: #205

It does two things:

  • adding a new checker that will help you to find invalid associations that point to undefined models
  • stop raising an error for ForeignKeyTypeChecker

It was released with version 1.7.13. Please try it, and let me know if that helped you with your issue.

Have a great day!

Hi @djezzzl , yep, that helped get a better understanding of the issue, thanks!