UPDATE! – Ruby ActiveRecord and Oracle: Auto Incrementing Primary Keys using Triggers

Posted by Jamie on July 11, 2008

Well, it looks like this has been fixed in the latest versions of the Oracle enhanced adapter for ActiveRecord. It looks like there was a reorganization of the different components of the ActiveRecord/Adapter relationship that had originally led to this problem, according to the documentation submitted with the fix. Thanks to whomever was able to provide the patch; You just made my life a little bit easier.

Martin Fowler’s article “Using the Rake Build Language”

Posted by Jamie on March 27, 2008

I just got through reading Martin Fowler’s excellent article on using the Rake build Language. It is a very easy to understand and fairly complete initiation into basic and intermediate build/automation techniques using Rake. It is old (Aug 10th 05) but, in my opinion, still quite relevant.

You can find the article here, and his main site (excellent!) is here: http://www.martinfowler.com

I stumbled across the article while exploring deployment/system administration options utilizing Ruby. Because I am using RAC for my database instances, I have multiple servers that I need to consistently update with scripts, configuration, data, etc. I have been looking into Capistrano for this task (even though it is an Oracle system) and it looks promising, if a bit kludgy to implement without SCM in place and accessible from the server.

Once I have the deployment solution figured out, I’ll write it up.

Ruby ActiveRecord and Oracle: Auto Incrementing Primary Keys using Triggers

Posted by Jamie on January 30, 2008

One of the most obvious limitations that I have faced when using ActiveRecord is it’s ability to adapt to legacy systems, especially Oracle databases. Many of the Oracle databases that I have used employ triggers to populate numeric primary key columns when data is inserted into the database tables. While this is a very nice and easy way to hide the mechanisms used to identify the data from the application developers when they are using straight DML, when you try to use ActiveRecord on these tables, ESPECIALLY when dealing with large object (LOB) fields, you get into some real hair-pulling situations.

ActiveRecord handles LOB inserts in two steps:

  1. The data is inserted into the database and the LOB field is initialized with an empty lob.
  2. The record is updated with the provided LOB data, replacing the empty lob.

As part of step 1 above, ActiveRecord pre-fetches the sequence that is used to populate the primary key field of the table. If you then have a trigger on that table that updates the incoming primary key column with the next sequence value, your Ruby object and the data inserted into the database are out of sync. Therefore, in step
2 of the LOB insert process described above, ActiveRecord is attempting to update a row with a primary key value that does not exist and no LOB values are ever populated.

To prevent this, I created the following code that alters ActiveRecord::Base and ActiveRecord::ConnectionAdapters::OracleAdapter to use the trigger-generated key (via the Oracle ‘RETURNING’ clause) in the AR object.
[ruby]

module ActiveRecord
module ConnectionAdapters #:nodoc:
class OracleAdapter < AbstractAdapter
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
cursor = @connection.parse(sql)
log(sql, name) { cursor.exec(0) }
# return the primary key value for the record just inserted
cursor[1]
end
end
end

class Base
# Creates a record with values matching those of the instance attributes and
# returns its id.
def create
if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
self.id = connection.next_sequence_value(self.class.sequence_name)
end

quoted_attributes = attributes_with_quotes

statement = if quoted_attributes.empty?
connection.empty_insert_statement(self.class.table_name)
else
“INSERT INTO #{self.class.quoted_table_name} ” +
“(#{quoted_column_names.join(‘, ‘)}) ” +
“VALUES(#{quoted_attributes.values.join(‘, ‘)}) returning #{self.class.primary_key} into :1″
end

self.id = connection.insert(statement, “#{self.class.name} Create”,
self.class.primary_key, self.id, self.class.sequence_name)

@new_record = false
id
end
end
end
[/ruby]

Now, this is really, really rough. I am using the 2.x version of ActiveRecord, and any updates to these sections in future releases will break my changes. It does, however, work for me right now. As I have time I will refactor this and increase its maintainablility.

Comments?