Martin Fowler’s article “Using the Rake Build Language”
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
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:
- The data is inserted into the database and the LOB field is initialized with an empty lob.
- 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.
-
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
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?
Open!
Greetings, and welcome to ThoughtHook! This is the obligatory here-I am post - But you deserve more than just an announcement, don't you?
What is this place about?
ThoughtHook is a place where I can share my thoughts and ideas about surviving and thriving in the jungle that is software development. I relish programming in Ruby, so much of what I post about will be ruby-related (not necessarily Rails-related.) You will also see posts about Programming Languages, Oracle, PL/SQL, general coding, etc.; Whatever interests me and/or is relevant to my current work.
So now you have a better idea of what I am trying to do here. Hopefully, we can all learn from each other.