aboutsummaryrefslogtreecommitdiffstats
path: root/db/migrate/052_include_event_foreign_references.rb
blob: 5dfcce4a7ef15b27f141ae8df5392c5c0a8fc192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class IncludeEventForeignReferences < ActiveRecord::Migration
    def self.up
        add_column :info_request_events, :incoming_message_id, :integer
        add_column :info_request_events, :outgoing_message_id, :integer
        for event in InfoRequestEvent.find(:all)
            incoming_message = event.incoming_message_via_params
            if not incoming_message.nil?
                event.incoming_message_id = incoming_message.id
            end
            outgoing_message = event.outgoing_message_via_params
            if not outgoing_message.nil?
                event.outgoing_message_id = outgoing_message.id
            end
            event.save!
        end
        if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
            execute "ALTER TABLE info_request_events ADD CONSTRAINT fk_info_request_events_incoming_message_id FOREIGN KEY (incoming_message_id) REFERENCES incoming_messages(id)"
            execute "ALTER TABLE info_request_events ADD CONSTRAINT fk_info_request_events_outgoing_message_id FOREIGN KEY (outgoing_message_id) REFERENCES outgoing_messages(id)"
        end
    end

    def self.down
        remove_column :info_request_events, :outgoing_message_id
        remove_column :info_request_events, :incoming_message_id
    end
end