PG::UndefinedTable: ERROR: relation "trends" does not exist

Problem:
Trends are broken, because the database table does not exist:

/usr/sbin/foreman-rake foreman_statistics:trends:counter
rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "trends" does not exist
LINE 8:  WHERE a.attrelid = '"trends"'::regclass
                            ^
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:19:in `exec'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:19:in `block (2 levels) in query'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:18:in `block in query'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract_adapter.rb:696:in `block (2 levels) in log'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract_adapter.rb:695:in `block in log'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract_adapter.rb:687:in `log'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:17:in `query'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql_adapter.rb:820:in `column_definitions'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract/schema_statements.rb:116:in `columns'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:112:in `block in columns'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:111:in `fetch'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:111:in `columns'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:120:in `block in columns_hash'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:119:in `fetch'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:119:in `columns_hash'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:553:in `load_schema!'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/attributes.rb:250:in `load_schema!'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:539:in `block in load_schema'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:536:in `synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:536:in `load_schema'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:392:in `columns_hash'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/inheritance.rb:84:in `descends_from_active_record?'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/inheritance.rb:90:in `finder_needs_type_condition?'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/core.rb:498:in `relation'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/scoping/named.rb:45:in `default_scoped'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/scoping/named.rb:32:in `all'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/scoping/named.rb:176:in `block in scope'
/usr/share/foreman/vendor/ruby/2.7.0/gems/foreman_statistics-2.1.0/app/services/foreman_statistics/trend_importer.rb:13:in `check_values'
/usr/share/foreman/vendor/ruby/2.7.0/gems/foreman_statistics-2.1.0/app/services/foreman_statistics/trend_importer.rb:5:in `update!'
/usr/share/foreman/vendor/ruby/2.7.0/gems/foreman_statistics-2.1.0/lib/tasks/foreman_statistics_tasks.rake:8:in `block (3 levels) in <top (required)>'
/usr/share/foreman/vendor/ruby/2.7.0/gems/rake-13.1.0/exe/rake:27:in `<top (required)>'

Caused by:
PG::UndefinedTable: ERROR:  relation "trends" does not exist
LINE 8:  WHERE a.attrelid = '"trends"'::regclass
                            ^
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:19:in `exec'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:19:in `block (2 levels) in query'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:18:in `block in query'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract_adapter.rb:696:in `block (2 levels) in log'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract_adapter.rb:695:in `block in log'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activesupport-6.1.7.7/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract_adapter.rb:687:in `log'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:17:in `query'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/postgresql_adapter.rb:820:in `column_definitions'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/abstract/schema_statements.rb:116:in `columns'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:112:in `block in columns'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:111:in `fetch'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:111:in `columns'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:120:in `block in columns_hash'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:119:in `fetch'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/connection_adapters/schema_cache.rb:119:in `columns_hash'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:553:in `load_schema!'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/attributes.rb:250:in `load_schema!'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:539:in `block in load_schema'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:536:in `synchronize'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:536:in `load_schema'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/model_schema.rb:392:in `columns_hash'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/inheritance.rb:84:in `descends_from_active_record?'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/inheritance.rb:90:in `finder_needs_type_condition?'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/core.rb:498:in `relation'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/scoping/named.rb:45:in `default_scoped'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/scoping/named.rb:32:in `all'
/usr/share/foreman/vendor/ruby/2.7.0/gems/activerecord-6.1.7.7/lib/active_record/scoping/named.rb:176:in `block in scope'
/usr/share/foreman/vendor/ruby/2.7.0/gems/foreman_statistics-2.1.0/app/services/foreman_statistics/trend_importer.rb:13:in `check_values'
/usr/share/foreman/vendor/ruby/2.7.0/gems/foreman_statistics-2.1.0/app/services/foreman_statistics/trend_importer.rb:5:in `update!'
/usr/share/foreman/vendor/ruby/2.7.0/gems/foreman_statistics-2.1.0/lib/tasks/foreman_statistics_tasks.rake:8:in `block (3 levels) in <top (required)>'
/usr/share/foreman/vendor/ruby/2.7.0/gems/rake-13.1.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => foreman_statistics:trends:counter
(See full trace by running task with --trace)

Expected outcome:
I’ve tried to seed the tables, but it does not recreate the tables:

$ /usr/sbin/foreman-rake db:seed
$ /usr/sbin/foreman-rake db:migrate

Foreman and Proxy versions:
3.10.0

Foreman and Proxy plugin versions:
foreman-3.10.0-1.el8.noarch
foreman-cli-3.10.0-1.el8.noarch
foreman-debug-3.10.0-1.el8.noarch
foreman-dynflow-sidekiq-3.10.0-1.el8.noarch
foreman-obsolete-packages-1.6-1.el8.noarch
foreman-ovirt-3.10.0-1.el8.noarch
foreman-postgresql-3.10.0-1.el8.noarch
foreman-proxy-3.10.0-1.el8.noarch
foreman-redis-3.10.0-1.el8.noarch
foreman-selinux-3.10.0-1.el8.noarch
foreman-service-3.10.0-1.el8.noarch
rubygem-foreman_discovery-24.0.1-1.fm3_10.el8.noarch
rubygem-foreman_hooks-0.3.17-3.fm3_9.el8.noarch
rubygem-foreman_puppet-6.2.0-1.fm3_10.el8.noarch
rubygem-foreman_statistics-2.1.0-2.fm3_10.el8.noarch
rubygem-foreman_templates-9.4.0-2.fm3_10.el8.noarch
rubygem-hammer_cli_foreman-3.10.0-1.el8.noarch
rubygem-puppetdb_foreman-6.0.2-1.fm3_9.el8.noarch

Distribution and version:
Rocky 8.9

Other relevant data:
none

Hi,
Trends have been removed from the Foreman [0]. This helper method [1] suggests removing the foreman_statistics plugin to clean up old trends data.

Try to run:

foreman-rake purge:trends

[0] Fixes #35347 - purge old foreman_docker references by ianballou · Pull Request #9343 · theforeman/foreman · GitHub
[1] foreman/lib/cleanup_helper.rb at develop · theforeman/foreman · GitHub

Trends are still available as a separate plugin:

No need to purge trends from the DB in my case, because the tables don’t exist :grin: (which is the problem I’d like to solve).

Solution: