Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to create trigger with custom name #22

Closed
mothra opened this issue Dec 13, 2013 · 4 comments
Closed

unable to create trigger with custom name #22

mothra opened this issue Dec 13, 2013 · 4 comments

Comments

@mothra
Copy link
Contributor

mothra commented Dec 13, 2013

Hello,

I am unable to use the 'name' option to create a trigger with a custom name. Here is an example of the trigger in my model:

  trigger.after(:update).name('contacts_trigger_custom_name') do |t|
    t.where("OLD.foo != NEW.foo") do
      "Bar"
    end
  end

The auto-generated migration looks like this:

class CreateTriggerContactsUpdate < ActiveRecord::Migration
  def up
    create_trigger("contacts_trigger_custom_name", :generated => true, :compatibility => 1).
        on("contacts").
        after(:update).
        name("contacts_trigger_custom_name") do |t|
      t.where("OLD.foo != NEW.foo") do
        "Bar;"
      end
    end
  end

  def down
    drop_trigger("contacts_trigger_custom_name", "contacts", :generated => true)

    drop_trigger("contacts_after_update_row_when_old_foo_new_foo_tr", "contacts", :generated => true)
  end
end

When I run that migration, the trigger has the name contacts_after_update_row_when_old_foo_new_foo_tr instead of my custom name. I am trying to create several update triggers with similar conditions, and the result is that only one trigger is created with the auto-generated name. So far my workaround has been to add bogus conditions to the triggers so that they are created with different names:

t.where("1 = 1 AND OLD.foo != NEW.foo")

I am using Rails 3.2.13, hairtrigger 0.2.4, pg 0.17.0, and PostgreSQL 9.2.

Thanks.

@jenseng
Copy link
Owner

jenseng commented Dec 15, 2013

@mothra I wonder if the issue is because you are using a trigger group? The name is set on the outer group, not on the inner trigger definition. When generating the actual trigger SQL, that name would only be used in MySQL, since it creates a single trigger for the whole group. PostgreSQL (and SQLite) will have a separate trigger for each definition within the group, thus it will infer names for them (unless they have explicit ones set).

So either of these approaches may be helpful:

  1. Just do a simple trigger w/o the group notation, e.g.
trigger.after(:update).name('contacts_trigger_custom_name').where("OLD.foo != NEW.foo") do
  "Bar"
end
  1. Keep the trigger group (especially if you are doing multiple triggers in it), but move the name(s) to the individual trigger definition(s), e.g.
trigger.after(:update) do |t|
  t.name('contacts_trigger_custom_name').where("OLD.foo != NEW.foo") do
    "Bar"
  end
end

@mothra
Copy link
Contributor Author

mothra commented Dec 17, 2013

Thanks, @jenseng. I ended up using your first option. I had originally tried using a trigger group as in your second option, but got the following error:

$ rake db:generate_trigger_migration
Connecting to database specified by database.yml
rake aborted!
tried to create Proc object without a block
(eval):14:in `new'
(eval):14:in `set_name'
(eval):7:in `name'
/Users/mthompson/src/Oceans/app/models/contact.rb:97:in `block in <class:Contact>'
/Users/mthompson/.rvm/gems/ruby-2.0.0-p353@oceans/gems/hairtrigger-0.2.4/lib/hair_trigger/builder.rb:282:in `call'
/Users/mthompson/.rvm/gems/ruby-2.0.0-p353@oceans/gems/hairtrigger-0.2.4/lib/hair_trigger/builder.rb:282:in `maybe_execute'
(eval):17:in `set_after'
(eval):7:in `after'
/Users/mthompson/src/Oceans/app/models/contact.rb:96:in `<class:Contact>'

@jenseng
Copy link
Owner

jenseng commented Dec 17, 2013

Interesting, I'll see about fixing that error.

Also we can leave this ticket open... Aa a minimum the docs should be updated so the behavior is more clear. Maybe hairtrigger could also issue a warning if the group has an explicit name but the triggers in it do not.

@mothra
Copy link
Contributor Author

mothra commented Apr 1, 2014

Terrific, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants