Thursday, 5 July 2007

Deploy Rails on Windows: Part 2

This post is part of a guide to deploying a small Rails application on a windows server using Oracle, Mongrel & Pen. My thinking: get it working with a simple app first, to minimise headaches later. The other posts can be found here:
  1. Installing Rails
  2. My-first-app with Oracle
  3. Pen of pesky Mongrels
This part heavily references a tutorial provided by Oracle.

Part 2: My-first-app with Oracle


This guide assumes you are either:
  • connecting to an Oracle Database installed on your local machine.
  • connecting to a remote database using Oracle Instant Client installed on your local machine.
If you do not have at least the client software installed, the Ruby-Oracle driver will complain that it can’t find files such as ‘oci.dll’.


Install Oracle Database Driver

Download the driver and install with:

ruby ruby-oci8-1.0.0-rc3-mswin32.rb

You can test your connection to the database with the following one-liner:

ruby -r oci8 -e "OCI8.new('username', 'password', '127.0.0.1:1521/orcl').exec('SELECT * FROM test_table') do |r| puts r.join('|'); end"

Of course, make sure the schema & table that you are testing exists!


Create a User

Using SQL*Plus, create a user with DBA privileges that you can use for this application.

GRANT dba TO ruby IDENTIFIED BY ruby;
ALTER USER ruby DEFAULT TABLESPACE users TEMPORARY TABLESPACE temp;
EXIT


Create a Table

CREATE TABLE comics (
id NUMBER(10) NOT NULL,
title VARCHAR2(60),
issue NUMBER(4),
publisher VARCHAR2(60),
PRIMARY KEY (id)
);
CREATE SEQUENCE comics_seq;

Alternatively use this SQL file to create the application table, COMICS.

sqlplus ruby/ruby@rails @comics.sql


Note, in this case the database SID is ‘rails’. The default SID for Oracle Enterprise Edition 10g is ‘orcl’, for Express Edition it’s ‘XE’


Create a dinky Application

Your application skeleton:

rails comics_catalog
cd comics_catalog


Edit ‘comics_catalog/config/databases.yml’ – rails needs to know your login & password info.

Within your project directory, there is a directory called config and in it is a file named database.yml. You need to edit database.yml using your favorite text editor. Initially, the file will look like this:

development:
adapter: mysql
database: rails_development
host: localhost
username: root
password:

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: rails_test
host: localhost
username: root
password:

production:
adapter: mysql
database: rails_production
host: localhost
username: root
password:

Change the development properties as follows:

development:
adapter: oci
username: ruby
password: ruby
host: 127.0.0.1:1234/orcl

host: takes a connect string of the form <ip_address>:<port>/<database> for the machine that your Oracle instance resides on.

Use the magical scaffold command to build a web based CRUD interface to the table we created above...

ruby script/generate scaffold Comic


Now you can access your Rails Comic Catalog application on your own development machine, using your favorite Web browser. Just access the following URL: http://localhost:3000/comics/list

Phew! Done. Last step is Part 3: Pen of pesky Mongrels...

3 comments:

Anonymous said...

Granting DBA access is not advised. Does this advice come from an old Oracle article? Only the necessary privileges (CREATE TABLE, CREATE SEQUENCE) etc should be used.

Barry said...
This comment has been removed by the author.
Barry said...

Thanks for that! Yes, as mentioned above the advice comes from here. I hope that the guide will evolve as I receive more comments (read: corrections) from others.