🧹

Railsのバージョンを7.2系に上げたらseedデータを投入できなくなった

2024/09/17に公開

遭遇した事象

Dependabot の自動生成 PR をマージし、Rails のバージョンを7.1系から7.2系に上げたところ、rails db:seed 実行時にエラーが出るようになってしまった。

実際のエラーメッセージは以下のとおり。

$ bundle exec rails db:seed
Running via Spring preloader in process 170
Clean database
rake aborted!
NoMethodError: undefined method `schema_migration' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x000000000084d0 env_name="development" role=:writing> (NoMethodError)

          ::ActiveRecord::Base.connection.schema_migration.table_name
                                         ^^^^^^^^^^^^^^^^^
Did you mean?  schema_creation
/usr/local/bundle/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record/base.rb:19:in `migration_table_name'
/usr/local/bundle/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record/truncation.rb:47:in `migration_storage_names'
/usr/local/bundle/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record/truncation.rb:41:in `tables_to_truncate'
/usr/local/bundle/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record/truncation.rb:25:in `block in clean'
/usr/local/bundle/gems/activerecord-7.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:219:in `disable_referential_integrity'
/usr/local/bundle/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record/truncation.rb:21:in `clean'
/usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaner.rb:65:in `clean_with'
/usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:40:in `block in clean_with'
/usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:40:in `each'
/usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:40:in `clean_with'
/app/webapp/db/seeds.rb:4:in `<main>'
/usr/local/bundle/gems/railties-7.2.1/lib/rails/engine.rb:562:in `load'
/usr/local/bundle/gems/railties-7.2.1/lib/rails/engine.rb:562:in `block in load_seed'
/usr/local/bundle/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:121:in `block in run_callbacks'
/usr/local/bundle/gems/activesupport-7.2.1/lib/active_support/execution_wrapper.rb:91:in `wrap'
/usr/local/bundle/gems/railties-7.2.1/lib/rails/engine.rb:648:in `block (2 levels) in <class:Engine>'
/usr/local/bundle/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `instance_exec'
/usr/local/bundle/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:130:in `block in run_callbacks'
/usr/local/bundle/gems/activesupport-7.2.1/lib/active_support/callbacks.rb:141:in `run_callbacks'
/usr/local/bundle/gems/railties-7.2.1/lib/rails/engine.rb:562:in `load_seed'
/usr/local/bundle/gems/activerecord-7.2.1/lib/active_record/tasks/database_tasks.rb:489:in `load_seed'
/usr/local/bundle/gems/activerecord-7.2.1/lib/active_record/railties/databases.rake:405:in `block (2 levels) in <main>'
<internal:/usr/local/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:86:in `require'
<internal:/usr/local/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:86:in `require'
-e:1:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

エラーメッセージを読み解くと、どうも database_cleaner-active_record という Gem が schema_migration というメソッドに対応していないために発生したエラーらしい。

解決方法

ターミナルで以下のコマンドを実行して database_cleaner-active_record を最新バージョン(database_cleaner-active_record (2.2.0))に変更したのち、再度 rails db:seed を実行すると問題なくseedデータを投入することができた。

bundle update database_cleaner-active_record

Discussion