Heroku测试夜间突发失败:无法配置SQL Sandbox
Hey there, let's work through this issue you're hitting on Heroku with your Elixir tests—especially since it ran fine Friday and works perfectly locally.
First, let's unpack the error you're seeing:
** (RuntimeError) cannot configure sandbox with pool DBConnection.Poolboy. To use the SQL Sandbox, configure your repository pool as: pool: Ecto.Adapters.SQL.Sandbox
This error boils down to a config mismatch: your Ecto Repository is using the DBConnection.Poolboy pool (standard for production) when it should be using Ecto.Adapters.SQL.Sandbox for testing. The SQL Sandbox relies on its own pool setup to handle test isolation, so mixing these two causes a conflict.
Why this happens on Heroku (but not locally)
Looking at your logs, this line stands out:
08:29:53.389 [info] seeding in prod environment
When you run mix test locally, it automatically uses the test environment (pulling config from config/test.exs). But on Heroku, your test command is running in the prod environment instead—where your repo is configured to use Poolboy. That's why you're seeing this error now, even though everything worked before.
Step-by-step fixes
Confirm your test environment config
Openconfig/test.exsand make sure your repository explicitly uses the Sandbox pool:config :your_app, YourApp.Repo, pool: Ecto.Adapters.SQL.Sandbox, # ... other test-specific settings (database name, credentials, etc.)Your
config/prod.exsshould still use Poolboy—this is correct for production, just not for tests.Force the test environment when running tests on Heroku
Instead of running justmix test, explicitly set the environment totestwith this command:MIX_ENV=test mix testIf you're using a Heroku pipeline or a predefined test script, update it to include
MIX_ENV=testto ensure tests always use the right config.Check Heroku's global environment variables
Double-check that you haven't setMIX_ENV=prodglobally for your Heroku app (which would override the test environment even during test runs). You can verify this with:heroku config:get MIX_ENV --app your-app-nameIf it returns
prod, no need to change this globally—just make sure your test command overrides it withMIX_ENV=test.
Quick recap
The root problem is that Heroku is running your tests in the production environment (using Poolboy) instead of the test environment (which should use the Sandbox). By forcing the test environment and confirming your configs are set properly, you should get past this error.
内容的提问来源于stack exchange,提问作者Jack Carlisle

