Ruby and Oracle OCI8

Ruby comes handy for little nifty scripty stuff to be done before running unit tests. I use it in particular to do some database cleanups before running a whole suite of tests — it is far more efficient than dbUnit for simple tasks. ruby-oci8 is dead easy to install and to use, so that’s what I’m using and describing here.

(I did the following on Windows, still have to try on Ubuntu)

  1. Install the ruby-oci8 gem:
    >gem install ruby-oci8
    Successfully installed ruby-oci8-2.0.2-x86-mswin32-60
    1 gem installed
    Installing ri documentation for ruby-oci8-2.0.2-x86-mswin32-60...
    Installing RDoc documentation for ruby-oci8-2.0.2-x86-mswin32-60...
  2. Make sure the Oracle client folder is in the PATH.
  3. Create a sample file:
    require 'oci8'
    conn ='user', 'password', 'tnsname')
    conn.exec('select table_name from user_tables') { |r| puts r }

Make sure you replace tnsname with the value from your tnsname.ora file. You can then run your nice test with:

ruby oracle.rb

Or call it from ant with an exec task. The API is there, and quite straightforward.