1

Question

How do I do a simple rails db:drop on fly.io?

Background

I tried

fly ssh console -C "/app/bin/rails db:drop DISABLE_DATABASE_ENVIRONMENT_CHECK=1"

and also shelling in via

fly ssh myapp

then

/app/bin/rails db:drop DISABLE_DATABASE_ENVIRONMENT_CHECK=1 

but both give the same error:

# /app/bin/rails db:drop DISABLE_DATABASE_ENVIRONMENT_CHECK=1 
D, [2022-12-03T07:19:04.077483 #564] DEBUG -- :    (5010.1ms)  DROP DATABASE IF EXISTS "myapp"
PG::ObjectInUse: ERROR:  database "myapp" is being accessed by other users
DETAIL:  There is 1 other session using the database.
Couldn't drop database 'myapp'
rails aborted!
ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR:  database "myapp" is being accessed by other users
DETAIL:  There is 1 other session using the database.

Notes

  • All browsers accessing the site are closed
  • All shell sessions are ended
  • I tried using ps -ef | grep postgres to get the process id in order to kill it, but all I see is /bin/sh: 18: ps: not found, and
  • Related thread.

Ideas

  • It may be possible to force postress to kill all connections to allow the db:drop to succeed (although I haven't worked out how to do that yet)

  • It may be possible to delete all the database tables (suggested here)

stevec
  • 41,291
  • 27
  • 223
  • 311

1 Answers1

0

There's something else accessing your db, probably your web server. You'll need to stop your web server, then you should be able to drop the db.

def avi
  • 506
  • 6
  • 10
  • Thanks, it makes sense, but how do you stop the rails server on fly.io? – stevec Dec 04 '22 at 02:18
  • hmm not really sure. – def avi Dec 05 '22 at 14:38
  • 1
    I actually remember that I tried to do a `db:reset` on heroku once and it wouldn't let me. I ended up opening the console and destroying all the records I had. You could do it like that, or just write the destroy queries in your seed file, then run db:seed on fly and the records will go away. – def avi Dec 05 '22 at 14:43
  • Thanks, I guess I'll take the seeds route you suggest (it's a little tedious for a large db, but might be the only option available). Incidentally, it's super easy on heroku with one line `heroku pg:reset DATABASE --confirm myappname && heroku run rake db:migrate db:seed` – stevec Dec 05 '22 at 15:15