Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Seed Data Execution in CI by Replacing db:setup with db:test:prepare #51678

Closed
viktorianer opened this issue Apr 28, 2024 · 3 comments
Closed

Comments

@viktorianer
Copy link
Contributor

viktorianer commented Apr 28, 2024

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.

System configuration

Rails version: 7.x
Ruby version: 3.x

Related to:

#50508
#44815

@byroot
Copy link
Member

byroot commented Apr 30, 2024

Agreed on not running seed on CI. You might need a db:create before db:test:prepare (not 100% sure).

PR welcome.

@viktorianer
Copy link
Contributor Author

viktorianer commented Apr 30, 2024

It will create the database, no problem. I am using this command for longer time in my Rails templates (which are including CI).

I just wanted to ask Rails core team or community, if they would like to use it too, before I create a PR.

@rafaelfranca
Copy link
Member

rafaelfranca commented Apr 30, 2024

Yes. Please open a PR. Closing since this is more like a feature request. But a PR would really be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants