sagarpandya82 sagarpandya82 - 5 months ago 30
Ruby Question

Rails Tutorial (M.Hartl) 3rd Edition, Chapter 8, How to unlock a database by closing a rogue `rails console`?

I am in chapter 8.4.1, after generating a migration I tried running:

$ bundle exec rake db:migrate


But was given the following error:

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::BusyException: database is locked:

#followed by a long list of paths/filenames and line numbers...


I know this is probably because I exited a terminal without properly exiting a
rails console --sandbox
session. But how do I find and close this
rails console
session?

Answer

In your terminal console type (making sure you are in your Rails app directory):

$ ps aux

Then you should see a list of processes, amongst them something similar to:

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
ubuntu     49527  0.0  0.0 482924   620 ?        Sl   Jan29   0:00 rails_console --sandbox

Take a note of the PID, in this case 49527, then run the following in the command line:

$ kill 49527

Now running your migration $ bundle exec rake db:migrate should work.