Developers don’t like databases (usually). Especially enterprise-y ones (read Oracle). I started my career as a DBA and shockingly enough, I happen to enjoy working on DBs. That said, this write-up is originally for my current project team and it works out for all developers ‘forced’ to work on Oracle for their clients. This one is NOT for DBAs, or anyone who can easily find their way around Oracle without frowning.
That said, let’s get to the meat. Recently, I set up an Ubuntu box with Oracle 11g to work with Rails 3.1. How I got the ruby-oci8 gem working is another story altogether. (which I’ve narrated
here) But (if and) when your ‘bundle install’ completes successfully and you come to the configurations further, you read this article and get those done with ease (hopefully).
To begin with, navigate to Oracle admin Web interface:
The username is always ‘admin‘ and the password is the one you set while installing Oracle. You would be prompted to change the password. Post that, you see an interface like this:
Now, the actual navigation begins. Go to:
Manage Workspaces -> Create Workspace -> Fill in a schema name, password, email etc. Hit Create.
It shows a confirmation for creation. A page with all the details is displayed. For the love of God, save it! Copy it and save it somewhere safe.
Now, you can (finally) proceed with the Rails database.yml configuration, if you are done with ruby-oci8 installation). It looks something like this (for development):
development:
adapter: oracle_enhanced
host: localhost
database: xe
username: <schema_name>
password: <workspace_password>
Try rake db:migrate (not create). It should work. If it doesn’t, comment below and maybe I can help you debug it.
That went well. But of course it isn’t over. You would want different ‘databases’ for development and test. Now, to create another ‘database’, you actually need to setup another schema. Lets go through that now. Go to the web admin again:
Manage Workspaces -> Manage Workspace to Schema Assignments -> Create
New -> Next -> (Select a workspace from existing workspaces) -> Next
Give a schema name, password, default tablespace (select from an existing) -> temporary tablespace (select ‘TEMP‘ from the list) -> Next -> Confirm
Again, in the database.yml:
test:
adapter: oracle_enhanced
host: localhost
database: xe
username: <new_schema_name>
password: <schema_password>
Try rake db:migrate RAILS_ENV=test. It should work. Proceed with creating as many ‘databases’ (schemas) as you need.
All this was fun and nice (well, it wasn’t that bad), but there are some known pitfalls which I can pre-inform you about. Invariably, you will end up needing the command line for some admin commands. Here’s how you get to it. Go to the path where Oracle is installed from the Terminal. If unsure, fire:
> echo $ORACLE_HOME
That’s where you ‘cd‘ to. Going further ahead , go to :
> cd <$ORACLE_HOME>/product/11.2.0/xe/bin
If you ‘ls’ into it, you can see ‘sqlplus’. Type in:
> sqlplus
user-name: / as sysdba
password: <password to login to web interface used with username ‘admin’> (if prompted)
SQL> grant dba to <schema_name>;
The above statement gives dba privileges to your ‘user’ (schema) so that commands like ‘create database link’ or ‘alter tablespace’ etc run smoothly for your user. Now, you can exit and login as your own user.
SQL> exit
sqlplus
user-name: <schema_name_you granted dba to>
password: <schema password>
Now you can simply fire away admin commands. For instance, if you want a new tablespace for say indices
- Creating the tablespace named ‘my_tblspc’:
CREATE TABLESPACE my_tblspc DATAFILE ‘path/to/datafile/my_tblspc.dbf’ size 1024M;
- If during the course of your work, you get an Oracle error crying for space:
alter database datafile ‘path/to/tablespace/datafile.dbf’ autoextend on;
* give the path of datafile for the tablespace. Incase there are multiple datafiles, use the last datafile path.
- If the above statement doesn’t solve it either, add a datafile:
alter tablespace tablespace_name add datafile ‘path/to/file/to/be/created/name.dbf’ size 8192M autoextend on;
Tips:
> Datafiles are located at: $ORACLE_HOME/oradata/XE
> Tablespace name can be found as:
Have fun! And remember, Databases don’t bite