Description

A library for generating an ActiveRecord model from an existing Oracle table. This will install an “omg” executable that you can use from the command line.

Synopsis

Using the command line tool:

omg -d your_database -t locations -u some_user -p some_password

Results in a file called “locations.rb”. This is an ActiveRecord model declaration, with all validations, primary keys, table name and belongs_to relationships defined.

If your locations table looks like this:

create table locations(
  location_id number(4,0) primary key,
  street_address varchar2(40), 
  postal_code varchar2(12),
  city varchar2(30) not null
  state_province varchar2(25),
  country_id CHAR(2),
  constraint "LOC_C_ID_FK" FOREIGN KEY (country_id)
    references COUNTRIES (country_id)
)

The omg library will generate this:

class Locations < ActiveRecord::Base
  set_table_name :locations
  set_primary_key :location_id

  # Table relationships

  belongs_to :countries

  # Validations

  validates :location_id, :presence => true, :numericality => {
    :less_than_or_equal_to => 9999, 
    :greater_than_or_equal_to => -9999,
    :only_integer => true
  }

  validates :street_address, :length => {:maximum => 40}
  validates :postal_code, :length => {:maximum => 12}
  validates :city, :length => {:maximum => 30}, :presence => true
  validates :state_province, :length => {:maximum => 25}
  validates :country_id, :length => {:maximum => 2}
end

Requirements

Must Have

  • ruby-oci8

  • getopt

Optional

If you want to be able to avoid specifying a username and password on the command line then you will need the dbi-dbrc library.

If you want your models to support multiple primary keys, then you will need to install the composite_primary_keys library.

If you want date format validations, then you will need to install the validates_timeliness library.

What this library doesn’t do

I do not attempt to set has_many or has_one relationships. There’s no good way to determine that relationship (one or many?). Besides, in practice I find that most people set custom has_xxx relationships that go over and above what’s set in the Oracle database anyway for purposes of their application.

I also do not go out of my way to get the model name correct with regards to singular vs plural. I do a simple guess that covers most cases, but complex cases will break it. It’s much easier for you to rename a class or file name than it is for me to get this 100% correct.

Author’s Comments

I chose not to patch legacy_data because I have no interest in supporting other vendors other than Oracle with this library. By focusing only on Oracle I could take advantage of ruby-oci8 features. In addition, I have no interest in making this a Rails plugin, and I needed the support of multiple primary keys.

Future Plans

Add support for views. Add automatic test suite generation. Explicitly set :foreign_key if using CPK in belongs_to relationships. The output could use a little formatting love.

Known Issues

None known. If you find any issues, please report them on the github project page at www.github.com/djberg96/oracle-model-generator.

Warranty

This package is provided “as is” and without any express or implied warranties, including, without limitation, the implied warranties of merchantability and fitness for a particular purpose.

Copyright

© 2010, 2011 Daniel J. Berger All Rights Reserved

License

Artistic 2.0

Author

Daniel J. Berger