You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CI workflows should use bin/rails db:test:prepare instead of bin/rails db:setup to prevent unintended seed data loading, especially when seed data relies on environment-specific credentials not present in test environments.
Steps to reproduce
In many Rails projects, seed data may utilize Rails' credentials feature to access sensitive information. The typical setup involves a single credentials.yml.enc file that might not include separate entries for different environments like test, development, and production.
When setting up a new Rails project, with a CI environment with GitHub Actions, the workflow includes a step with bin/rails db:setup test test:system which attempts to set up the test database and inadvertently runs the seed data. Here is an example error that can occur when db:setup is executed in a test environment where the necessary credentials might not be set:
Run bin/rails db:setup
Created database 'community_app_test'
rails aborted!
NoMethodError: undefined method `[]' for nil (NoMethodError)
password: Rails.application.credentials.admin[:password]
^^^^^^^^^^^
/home/runner/work/community_app/community_app/db/seeds.rb:32:in `<main>'
Expected behavior
The CI process should only prepare the test database without attempting to load seed data. Seed data often contains production-like setups that are not relevant to automated test environments and can lead to failures if they depend on credentials not present in the test environment.
Actual behavior
Using bin/rails db:setup in CI setups unintentionally initiates seed data loading. When seeds depend on environment-specific credentials that are not available in the test environment, it leads to failures such as NoMethodError for undefined methods when accessing missing credentials.
Suggested Fix
To avoid such errors and ensure a clean test environment setup, the CI workflow command should be changed to:
bin/rails db:test:prepare which is designed specifically to handle test database preparation without loading seed data.
Alternatively, bin/rails db:create db:schema:load could be used to manually replicate the steps db:test:prepare would typically handle, ensuring the test database is ready without any seed data interference.
PS: I can implement this change by modifying the CI configuration to replace bin/rails db:setup with bin/rails db:test:prepare, ensuring that seed data does not interfere with the test environment.
CI workflows should use
bin/rails db:test:prepare
instead ofbin/rails db:setup
to prevent unintended seed data loading, especially when seed data relies on environment-specific credentials not present in test environments.Steps to reproduce
In many Rails projects, seed data may utilize Rails' credentials feature to access sensitive information. The typical setup involves a single
credentials.yml.enc
file that might not include separate entries for different environments liketest
,development
, andproduction
.When setting up a new Rails project, with a CI environment with GitHub Actions, the workflow includes a step with
bin/rails db:setup test test:system
which attempts to set up the test database and inadvertently runs the seed data. Here is an example error that can occur whendb:setup
is executed in a test environmentwhere the necessary credentials might not be set
:Expected behavior
The CI process should only
prepare the test database without attempting to load seed data
. Seed data often contains production-like setups that are not relevant to automated test environments and can lead to failures if they depend on credentials not present in the test environment.Actual behavior
Using
bin/rails db:setup
in CI setups unintentionally initiates seed data loading. When seeds depend on environment-specific credentials that are not available in the test environment, it leads to failures such asNoMethodError
for undefined methods when accessing missing credentials.Suggested Fix
To avoid such errors and ensure a clean test environment setup, the CI workflow command should be changed to:
bin/rails db:test:prepare
which is designed specifically to handle test database preparation without loading seed data.bin/rails db:create db:schema:load
could be used to manually replicate the stepsdb:test:prepare
would typically handle, ensuring the test database is ready without any seed data interference.PS: I can implement this change by modifying the CI configuration to replace
bin/rails db:setup
withbin/rails db:test:prepare
, ensuring that seed data does not interfere with the test environment.System configuration
Rails version: 7.x
Ruby version: 3.x
Related to:
#50508
#44815
The text was updated successfully, but these errors were encountered: