Trigger schema idea

3 views
Skip to first unread message

Daniel Berger

unread,
Sep 17, 2009, 11:00:10 AM9/17/09
to Oracle enhanced adapter for ActiveRecord
Hi,

I noticed that neither db:schema:dump nor db:structure:dump included
triggers in the dump. That got me thinking of a generic way to create
a trigger in a migration. I tried to come up with some syntax that I
thought might be reasonably cross platform. What do you think of
something like this?

# Based on example from Oracle 9i, The Complete Reference, p. 513
create_trigger(:name => 'bookshelf_bef_upd_row', :force => true) do |
t|
t.before => 'update'
t.on => 'bookshelf'
t.condition => 'new.rating < old.rating'
t.execute => %Q{
insert into bookshelf_audit
(title, publisher, categoryname, old_rating, new_rating,
audit_date)
values

(:old.title, :old.publisher, :old.category_name, :old.rating, :new.rating,
sysdate)
}
end

create_trigger could take 2 options, the 'name' and a 'force' option.
It would yield a trigger object, which would respond to the following
methods:

# These would accept 'insert', 'update' and 'delete', or any
combination of them
before
after
instead_of

# The table name
on

# The 'when' clause
condition

# The call procedure
execute

How does something like that look? And would it be of any interest to
you?

Regards,

Dan

Daniel Berger

unread,
Sep 17, 2009, 11:21:30 AM9/17/09
to Oracle enhanced adapter for ActiveRecord
BTW, the code above would generate:

create or replace trigger bookshelf_bef_upd_row
before update on bookshelf
for each row
when (new.rating < old.rating)
begin
insert into bookshelf_audit
(title, publisher, categoryname, old_rating, new_rating,
audit_date)
values

(:old.title, :old.publisher, :old.category_name, :old.rating, :new.rating,
sysdate)
end;

Raimonds Simanovskis

unread,
Sep 18, 2009, 3:09:36 AM9/18/09
to Oracle enhanced adapter for ActiveRecord
I think that as trigger syntax is quite Oracle specific there is no
big need to create Ruby DSL around it. You can just include

execute <<-SQL
create or replace trigger bookshelf_bef_upd_row
before update on bookshelf
for each row
when (new.rating < old.rating)
begin
insert into bookshelf_audit
(title, publisher, categoryname, old_rating, new_rating,
audit_date)
values
(:old.title, :old.publisher, :old.category_name, :old.rating, :new.rating,
sysdate)
end;
SQL

in your migration.

Raimonds
Reply all
Reply to author
Forward
0 new messages