ruby-on-rails 如何让Rails创建我的测试数据库?

hgc7kmma  于 2022-12-30  发布在  Ruby
关注(0)|答案(3)|浏览(192)

我正在尝试使用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

但似乎没什么效果。

yzckvree

yzckvree1#

ENV['DATABASE_URL']将与database.yml文件中的任何设置合并并覆盖其中的任何设置。
这通常在Heroku这样的生产环境中使用,以避免签入凭据。
参见:

  • 3.19配置数据库
6tqwzwtp

6tqwzwtp2#

你可以试试

RAILS_ENV=test rails db:drop db:create db:schema:load

据我所知db:test:prepareno longer available since Rails 4.1.0.rc1+

ibps3vxo

ibps3vxo3#

尝试

RAILS_ENV=test bin/rails db:test:prepare

有时,我们需要在运行任何命令之前指定环境

相关问题