MacPorts not working after power outage during build (sqlite disk I/O error)

macportssqlite

I was building packages in MacPorts on a Mac Mini when the power went out. Now anything I try to do in MacPorts (install, clean, selfupdate) results in the following error:

sqlite error: disk I/O error (3850) while executing query: ATTACH DATABASE '/opt/local/var/macports/registry/registry.db' AS registry
    while executing
"registry::open $db_path"
    (procedure "mportinit" line 610)
    invoked from within
"mportinit ui_options global_options global_variations"
Error: /opt/local/bin/port: Failed to initialize MacPorts, sqlite error: disk I/O error (3850) while executing query: ATTACH DATABASE '/opt/local/var/macports/registry/registry.db' AS registry

How can I fix this?

Best Answer

The fix for this is actually quite simple, and is mentioned in this MacPorts issue. From a comment on the issue by "pixilla@…":

$ cd /opt/local/var/macports/registry
$ ls registry.db-journal                                                                                                                                                                             
$ sudo sqlite3 registry.db
sqlite> .tables
sqlite> SELECT id FROM ports;
sqlite> .quit

Based on another comment by "ecronin@…", I conclude that, had I run any port command as root, it would have been fixed automatically and saved me all this trouble. But I always forget to use sudo...

EDIT Wait, I did run a port command with sudo. Weird. Oh, well, at least it works now.