3

I'm going through the Ruby on Rails tutorial by Michael Hartl, and in chapter 6 I am instructed to create a new migration to add an index to the email column in the users table.

Here is my migration:

def self.up
  add_index :users, :email, :unique => true
end

def self.down
  remove_index :users, :email
end

When I run rake db:migrate it thinks for a second, then throws a BusyException and says the database is locked. The database is a sqlite3 database stored on my local machine in my user folder; nothing special.

Any and all help is greatly appreciated.

user229044
  • 232,980
  • 40
  • 330
  • 338
YuKagi
  • 2,451
  • 3
  • 21
  • 26
  • That migration is trying to add an index on the `users.email` column but you're talking about adding a new column. Confused? – mu is too short Mar 27 '11 at 23:59
  • You're right, I'm trying to add an index to the email column in the users table, not a new column. Thank you for pointing that out. – YuKagi Mar 28 '11 at 04:20

7 Answers7

7

I get this all the time, it lies in the fact that sqlite can only be accessed by one process at a time and that the database is locked by that process. Be sure you have no servers or consoles running in another terminal. If you continue to get this, and you are sure there is nothing else accessing that sqlite database (including zombie processes), you can follow the advice here:

How do I unlock a SQLite database?

Community
  • 1
  • 1
Christopher Maujean
  • 1,321
  • 10
  • 10
  • Perfect. That's exactly what was going on. I checked to see if any servers were running, and couldn't think of any other process that would have been accessing it, but I just killed Terminal and everything works. Thank you so much Christopher. – YuKagi Mar 28 '11 at 02:05
2

Almost the same has happended to me. The, simple, solution was exiting from my rails console. It was locking SQLite.

Eduardo Dx
  • 36
  • 2
2

I had the same problem. For me I had the SQLite Database Browser open with the same database which was locking it.

rlawrenz
  • 146
  • 3
1

/agree with Christopher

For those of us on unix-based machines, a simple

$: ps -a | grep ruby
$: kill -s 9 XXXX

of ruby processes from terminals I suspected as the culprit did it for me. It was always caused by a console or server that hung in a way that made me kill it and then have to find the 'zombie' process to unlock the db.

On windows it can't be harder than killing a suspect process tree, surely.

winfred
  • 3,053
  • 1
  • 25
  • 16
0

The solution to my BusyException problem, "obvious" in retrospect, was that I did not have write access to SQLite3 database. I had git clone'd a rails project to a windows-served file-system that I was accessing from a Ubuntu box. For whatever reason, Sqlite3 databases were created with +x permission which Sqlite3 must interpret as busy.

It was only after lots of work confirming versions, gems, rubies, etc, that I noticed files permissions were +x.

Granted, an arcane set of circumstances, but I struggled with the problem on/off for two weeks, without finding any help from searching Google, that I thought I would add solution to knowledge base.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
rkgordon3
  • 141
  • 1
  • 4
0

For me, Nothing was working. I deleted all *.sqlite3 files and then ran

rake db:create
rake db:migrate

Then it worked.

PS: sqlite3 files are located in db folder of your root directory of rails project

Ojas Kale
  • 2,067
  • 2
  • 24
  • 39
0

Just had this same issue but the solution was slightly different than above.

$ pgrep -l rails
XXXX ruby
$ kill -s 9 XXXX
James
  • 1