1

I am trying to do some DB experiments in which I reset the DB a few times as part of Rails runner script. Here is a simple example:

`rake db:reset`
puts User.count
`rake db:reset`

This fails with:

PG::ObjectInUse: ERROR:  database "my_db" is being accessed by other users
DETAIL:  There is 1 other session using the database.

I understand why this happens. The User.count opens a connection so the reset cannot happen. I have tried a few things to close said connection, including calling close and reset_active_connections!, to no avail. Any idea how I can achieve this? Some other pursuits were not fruitful as well, such as trying to close the connection by passing the process ID to psql.


1 Answer 1

2

The right call is:

ApplicationRecord.connection_pool.connections.map &:disconnect!

I simply couldn't find the right method the first time, had to read the AR code in more detail to find it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.