Note that there are some explanatory texts on larger screens.

plurals
  1. POAltering PostgreSQL table in Rails app disables trigger
    primarykey
    data
    text
    <p>In my Rails 3.1.3 app I have <code>subscriptions</code> table; operations on this table must be tracked which is crucial for billing. Since database can be accessed in many ways (API, console, client apps), simple ActiveRecord callbacks or observer is not enough to make sure all transactions on a table are recorded. Therefore, I create a database trigger on <code>subscriptions</code> table that inserts a record in "log" table anytime something changes. In order to do that I am using Rails migration like this: </p> <pre><code>def up execute &lt;&lt;-SQL CREATE OR REPLACE FUNCTION FUNCTION_Event_Type() RETURNS TRIGGER AS $TRIGGER_Event_Type$ BEGIN IF (TG_OP = 'DELETE') THEN INSERT INTO subscription_log (company_id, product_id, old_package_id, new_package_id, trigger_type, updated_at, created_at) SELECT OLD.company_id, OLD.product_id, OLD.package_id, NULL, 'Delete', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP; RETURN OLD; ELSIF (TG_OP = 'UPDATE') THEN INSERT INTO subscription_log (company_id, product_id, old_package_id, new_package_id, trigger_type, updated_at, created_at) SELECT OLD.company_id, OLD.product_id, OLD.package_id, NEW.package_id, 'Update', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP; RETURN NEW; ELSIF (TG_OP = 'INSERT') THEN INSERT INTO subscription_log (company_id, product_id, old_package_id, new_package_id, trigger_type, updated_at, created_at) SELECT NEW.company_id, NEW.product_id, NULL, NEW.package_id, 'Insert', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP; RETURN NEW; END IF; RETURN NULL; END; $TRIGGER_Event_Type$ LANGUAGE plpgsql; CREATE TRIGGER TRIGGER_Event_Type AFTER INSERT OR UPDATE OR DELETE ON subscriptions FOR EACH ROW EXECUTE PROCEDURE FUNCTION_Event_Type(); SQL end </code></pre> <p>The trigger works fine and logs as needed. I have rspec tests checking that record is inserted in "logging" table anytime something is done on <code>subscriptions</code> table. However, I continued working on the app, and had to add a column to <code>subscriptions</code> table using Rails migration:</p> <pre><code>def change add_column :subscriptions, :description, :text end </code></pre> <p>After I ran the migration, my tests that check trigger functionality like this: </p> <pre><code> lambda do FactoryGirl.create(:subscription) end.should change(SubscriptionLog, :count).by(1) </code></pre> <p>started failing.</p> <p><em>UPDATE</em>: development database still has trigger after adding a column. test database loses trigger after running the migration that adds a column... Weird</p> <p>Question:<br> Does altering the table kill a trigger? And if it does, what would be the way to make sure trigger persists?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload