Home > Back-end >  How can I make Rails create my test database?
How can I make Rails create my test database?

Time:01-03

I'm trying to get Rails 7 to create my test database:

bin/rails db:test:prepare RAILS_ENV=test

I have very clearly set up my test environment in 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

But the command is trying to delete my development database:

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)

What am I missing about the environment setting? I've even run:

bin/rails db:environment:set RAILS_ENV=test

but that doesn't appear to have done anything.

CodePudding user response:

You can try

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

As I know db:test:prepare is no longer available since Rails 4.1.0.rc1 .

CodePudding user response:

ENV['DATABASE_URL'] is merged with and overrides any settings from the database.yml file.

This is often used in production environments like Heroku to avoid checking in credentials.

See:

  • Related