MongoDB version number in the Mongo console

To print MongoDB’s version number when in the Mongo console, just run

db.version()

For slightly more information, run

db.serverBuildInfo()

This is pretty obvious, but for some reason, I seem to forget this all the time.

Posted in MongoDB Tagged

Rails 3 console and “no such file to load”

If you happen to load gems in your ~/.irbrc , make sure to add a gem dependency on them in your Rails apps’ Gemfile.

For example, I just started using Awesome Print in IRB. However, when I ran rails console , this error occurred:

no such file to load -- ap

After searching Google, I learned that Awesome Print needs to be listed in my Gemfile . Easy enough:

## ~/.irbrc
require 'rubygems'
require 'ap'

## ~/src/rails_3_app/Gemfile
group :development do
  gem 'awesome_print'
end

Posted in Ruby Tagged , ,

PostgreSQL grants on all tables

As much as I love PostgreSQL, its system of managing user privileges is a pain in the ass. PostgreSQL has no equivalent to MySQL’s method of granting a privilege on all of the tables in a database, including tables that you create in the future.

In MySQL, it’s as easy as:

GRANT SELECT ON db_name.* TO user@hostname;

The best that I’ve been able to achieve with PostgreSQL is this abomination:

$ cat generate_grants.txt
SELECT 'grant select on '||schemaname||'.'||tablename||' to user_name;'
FROM pg_tables
WHERE schemaname IN ('public')
ORDER BY schemaname, tablename;
$
$ cat generate_grants.txt | psql -U nickh -W -d database |
grep '^ grant' >grants.txt
$
$ head -2 grants.txt
 grant select on public.table1 to user_name;
 grant select on public.table2 to user_name;
$
$ cat grants.txt | psql -U nickh -W -d database

*shudder*

Thanks to Ben Williams’ blog post for the initial query to generate the GRANT statements.

Posted in GNU/Linux

Deploy a specific revision with Capistrano

Just about every website that I release, be it at work or at home, is stored in a Git repository, and deployed using Capistrano.

I’ve been debugging this PHP application at work that was written by a 3rd-party. After cleaning up the XSS and SQL injection vulnerabilities in it, we found a new bug. It’s minor, yet important.

While tracking down the offending line, I needed to deploy various revisions of the website. It turns out that it’s really easy to do this with Capistrano:

$ cap -S revision=3f30b6de3c55a8347e5f3de3b43193591e6c7322 beta deploy

One thing I noticed was the need to provide the full SHA-1 object name (AKA revision ID, commit ID). Normally with Git, you can provide the first few characters of a commit ID. For example:

$ git checkout 3f30b6de

instead of

$ git checkout 3f30b6de3c55a8347e5f3de3b43193591e6c7322

I guess Capistrano’s a bit more picky!

Posted in Coding Tagged ,

Answers are for losers

What do you want: questions, or answers? Most people want answers. There’s a problem you need to solve, and the answer’ll let you continue working. That’s fine. That’s valid.

But is it interesting?

I was talking with @ghuber about a new idea I had. Without going into details, it’d pigeon-hole me into That Answer Guy.

How do you solve X? Here you go! The answer’s ABC.

BOR-fucking-ING. If you’re That Answer Guy, you’re giving away valuable resources, but not actively facilitating much further discussion or innovation. Your community’ll appreciate the answer. Kudos to you. Thanks, Teach!The Answer Man movie poster

But it doesn’t make you interesting.

Instead, you want to ask questions. Lots of questions. Oddball questions. Edge-case questions. What happens if I turn this to 11? What if I mix lolcats with quantum theory?

Think of it this way: Who do you want presenting at a conference? Who do you want to shoot the shit with over a beer? Who thinks of wacky stuff? The guy who knows how to do X when you have Y, or the guy who asks interesting questions and ponders new ways of doing things?

The latter, damn it!

That Answer Guy is useful and appreciated. He definitely is. But the role’s limited. It confines you. It defines what questions you ask, and what problems you solve.

Ask more questions. Write more software. Pursue tangents. And talk about it.

Posted in Other

If Plasma dies, here’s what to do

Every now and then, the Plasma desktop in KDE 4 crashes on me. Usually, it restarts itself. Sometimes, it doesn’t.

If you’re cursed with the latter scenario, there’s an easy solution.

Note: As avocadohead kindly mentioned in the comments, KDE >= 4.3 has renamed “plasma” to “plasma-desktop”. So if you’re using KDE 4.3 or later, replace “plasma” with “plasma-desktop” in the commands below.

By default, KDE sets the keyboard shortcut for “Run Command” to ALT+F2. So hit ALT+F2, and type this, and hit enter:

kbuildsycoca4 && kquitapp plasma && kstart plasma

Plasma should pop back to life.

That command is actually three commands:

  1. kbuildsycoca4 rebuilds KDE’s system configuration cache.
  2. kquitapp plasma ensures that Plasma’s no longer running, instead of just, say, hung.
  3. kstart plasma starts Plasma.

It took me a while to figure this out. Thanks to this post, I learned about kstart. From what I’ve read, it seems that running kquitapp plasma and kstart plasma from the CLI works for some people. Unfortunately, they fail for me on the CLI:

[nickh@chameleon ~] kquitapp plasma
<unknown program name>(7282)/: "Application plasma could not be found using service org.kde.plasma and path /MainApplication."
[nickh@chameleon ~]
[nickh@chameleon ~] kstart plasma
Qt: Session management error: Could not open network socket
kstart(7394) main: Omitting both --window and --windowclass arguments is not recommended
[nickh@chameleon ~] <unknown program name>(7397)/ checkComposite: Plasma has an argb visual 0x9ca7300 71303169
<unknown program name>(7397)/ checkComposite: Plasma can use COMPOSITE for effects on 0x9ca6c18
plasma(739Smilie: 8): KUniqueApplication: Cannot find the D-Bus session server

plasma(7397): KUniqueApplication: Pipe closed unexpectedly.

[nickh@chameleon ~]

Posted in GNU/Linux Tagged

Matching Printable Characters

Who doesn’t love regular expressions? They’re fucking awesome. I use’em at least 10 times per day.

Sometimes, there’re restrictions on what patterns you can use. I wanted to change a regex for validating passwords. Originally, it allowed letters, numbers, and a seemingly random collection of special characters. How lame is this?:

^[A-Za-z0-9]{1}[A-Za-z0-9_\.\!@#-]{0,255}$

Not only are the allowed special characters arbitrary, but they’re escaping characters in a character class, and using “{1}”.

I tried changing the regex to this:

^[[:print:]]{0,255}$

Unfortunately, that wasn’t considered “valid” by the system in question. Luckily, there’s a fairly concise alternative:

^[x20-x7E]{0,255}$

If you find a system that lacks support for POSIX character classes, check out this Wikipedia article.

Posted in Coding, GNU/Linux, Uncategorized Tagged

Displaying Your ViM Colour Scheme

I posted this mostly because I’m always forgetting it, and also because it’s a pain in the ass to search for on Google. I usually try “:echo g:colorscheme”, which fails miserably.

If you’re playing around with colour schemes in ViM and want to figure out which one is being used at the moment, simply type:

:echo g:colors_name

Posted in GNU/Linux Tagged ,

When Gnome and Firefox Are Dead Slow

I’ve had Ubuntu Karmic Koala installed on my MacBook Pro Core2Duo for 2-3 weeks now. Almost everything’s been working perfectly. However, whenever I’d start a second Gnome session and run Firefox, the machine would slow to a crawl, and X’s CPU usage would skyrocket..we’re talking >80% here.

I scoured Google for all sorts of things:

  • Gnome second session is slow
  • Gnome X is slow
  • Gnome Xorg is slow
  • Gnome Firefox slow
  • X second session is slow
  • Etc.

I found several other reports of this, but no solutions.

Eventually, I stumbled upon this blog post, which purports to have solved the problem. I gave it a shot, and holy shit, it worked!

Just to be verbose, here’s what I did:

  1. Open /etc/X11/xorg.conf .
  2. Add this line to the “Device” section:
    Option "AccelMethod" "XAA"

    So my “Device” section now looks like this:

    Section "Device"
      Identifier  "Configured Video Device"
      Option      "AccelMethod" "XAA"
    EndSection
  3. Save the file and log-out of Gnome.
  4. Switch to tty1 by hitting CTRL+ALT+F1 .
  5. Restart GDM:
    $ sudo /etc/init.d/gdm restart

Posted in GNU/Linux Tagged ,

Copying Between GNU Screen buffers

GNU Screen is one of those tools that makes you think “How the %&@# did I live without this for so long?!” If you work on command lines regularly, GNU Screen is a must.

In my desktop environment, I run Konsole and Yakuake, with a different Screen session in each[1]. A few days ago, I noticed that I was using the mouse to copy text between these Screen sessions. How inefficient is that?

It turns out that there’s a really easy way to copy text between different Screen sessions.

Note: I use CTRL-J as my escape command.

Copy some text to the first Screen’s buffer, and type ^J:writebuf . Now switch to your other Screen session, and type ^J:readbuf . Your second Screen session has just grabbed what you copied in your first Screen session. Paste it wherever you want, and Bob’s your uncle.

[1] These Screen sessions are different on purpose. My Yakuake session is for “throw-away” tasks that don’t necessarily pertain to any tasks that I’m currently working on.

Posted in GNU/Linux Tagged