我正在尝试使用Rails 7来创建测试数据库:
bin/rails db:test:prepare RAILS_ENV=test
我已经非常清楚地在database.yml
中设置了测试环境:
default: &default
adapter: postgresql
encoding: utf8
port: 5432
pool: 5
host: localhost
database: my_db_dev
username: user
password: pwd
development:
<<: *default
test:
<<: *default
database: my_db_test
production:
<<: *default
但命令试图删除我的开发数据库:
base) ➜ git:(main) ✗ bin/rails db:test:prepare RAILS_ENV=test
rails aborted!
ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: database "my_db_dev" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
Caused by:
PG::ObjectInUse: ERROR: database "my_db_dev" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)
关于环境设置,我遗漏了什么?我甚至运行了:
bin/rails db:environment:set RAILS_ENV=test
但似乎没什么效果。
3条答案
按热度按时间yzckvree1#
ENV['DATABASE_URL']
将与database.yml文件中的任何设置合并并覆盖其中的任何设置。这通常在Heroku这样的生产环境中使用,以避免签入凭据。
参见:
6tqwzwtp2#
你可以试试
据我所知
db:test:prepare
是no longer available since Rails 4.1.0.rc1+。ibps3vxo3#
尝试
有时,我们需要在运行任何命令之前指定环境